[Mesa-dev] libutils/mipmap.c: Fixed possible memory leak

Submitted by Amarnath Valluri on June 10, 2016, 11:30 a.m.

Details

Message ID 1465558217-19338-2-git-send-email-amarnath.valluri@intel.com
State New
Headers show
Series "libutils/mipmap.c: Fixed possible memory leak" ( rev: 1 ) in Mesa

Not browsing as part of any series.

Commit Message

Amarnath Valluri June 10, 2016, 11:30 a.m.
In case of malloc() failure memory allocated for both 'srcImage' and 'dstImage'
is leaked.

Signed-off-by: Amarnath Valluri <amarnath.valluri@intel.com>
---
 src/libutil/mipmap.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

Patch hide | download patch | download mbox

diff --git a/src/libutil/mipmap.c b/src/libutil/mipmap.c
index c475c96..1f718a6 100644
--- a/src/libutil/mipmap.c
+++ b/src/libutil/mipmap.c
@@ -4509,7 +4509,11 @@  static int gluBuild2DMipmapLevelsCore(GLenum target, GLint internalFormat,
 	     glPixelStorei(GL_UNPACK_SKIP_PIXELS, psm.unpack_skip_pixels);
 	     glPixelStorei(GL_UNPACK_ROW_LENGTH, psm.unpack_row_length);
 	     glPixelStorei(GL_UNPACK_SWAP_BYTES, psm.unpack_swap_bytes);
-	     return GLU_OUT_OF_MEMORY;
+       free(srcImage); /*if you get to here, a srcImage has always been malloc'ed*/
+       if (dstImage) { /* if it's non-rectangular and only 1 level */
+          free(dstImage);
+       }
+       return GLU_OUT_OF_MEMORY;
 	  }
 
 	  /* copy image from srcImage into newMipmapImage by rows */

Comments

On 06/10/2016 05:30 AM, Amarnath Valluri wrote:
> In case of malloc() failure memory allocated for both 'srcImage' and 'dstImage'
> is leaked.
>
> Signed-off-by: Amarnath Valluri <amarnath.valluri@intel.com>
> ---
>   src/libutil/mipmap.c | 6 +++++-
>   1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/src/libutil/mipmap.c b/src/libutil/mipmap.c
> index c475c96..1f718a6 100644
> --- a/src/libutil/mipmap.c
> +++ b/src/libutil/mipmap.c
> @@ -4509,7 +4509,11 @@ static int gluBuild2DMipmapLevelsCore(GLenum target, GLint internalFormat,
>   	     glPixelStorei(GL_UNPACK_SKIP_PIXELS, psm.unpack_skip_pixels);
>   	     glPixelStorei(GL_UNPACK_ROW_LENGTH, psm.unpack_row_length);
>   	     glPixelStorei(GL_UNPACK_SWAP_BYTES, psm.unpack_swap_bytes);
> -	     return GLU_OUT_OF_MEMORY;
> +       free(srcImage); /*if you get to here, a srcImage has always been malloc'ed*/
> +       if (dstImage) { /* if it's non-rectangular and only 1 level */
> +          free(dstImage);
> +       }
> +       return GLU_OUT_OF_MEMORY;
>   	  }
>
>   	  /* copy image from srcImage into newMipmapImage by rows */
>

I'll push this patch with a few minor changes:
1. fix indentation to match surrounding code.
2. remove if-test around the second free().  free(NULL) is a no-op.
3. remove unneeded comments.
4. List the bug URL in the check-in comment.

Sound OK?

Thanks for the patch!

-Brian
Thanks Brian, for quick review.

I am obsoletely fine with the changes.

- Amarnath

On Friday 10 June 2016 05:20 PM, Brian Paul wrote:
> On 06/10/2016 05:30 AM, Amarnath Valluri wrote:
>> In case of malloc() failure memory allocated for both 'srcImage' and 
>> 'dstImage'
>> is leaked.
>>
>> Signed-off-by: Amarnath Valluri <amarnath.valluri@intel.com>
>> ---
>>   src/libutil/mipmap.c | 6 +++++-
>>   1 file changed, 5 insertions(+), 1 deletion(-)
>>
>> diff --git a/src/libutil/mipmap.c b/src/libutil/mipmap.c
>> index c475c96..1f718a6 100644
>> --- a/src/libutil/mipmap.c
>> +++ b/src/libutil/mipmap.c
>> @@ -4509,7 +4509,11 @@ static int gluBuild2DMipmapLevelsCore(GLenum 
>> target, GLint internalFormat,
>>            glPixelStorei(GL_UNPACK_SKIP_PIXELS, psm.unpack_skip_pixels);
>>            glPixelStorei(GL_UNPACK_ROW_LENGTH, psm.unpack_row_length);
>>            glPixelStorei(GL_UNPACK_SWAP_BYTES, psm.unpack_swap_bytes);
>> -         return GLU_OUT_OF_MEMORY;
>> +       free(srcImage); /*if you get to here, a srcImage has always 
>> been malloc'ed*/
>> +       if (dstImage) { /* if it's non-rectangular and only 1 level */
>> +          free(dstImage);
>> +       }
>> +       return GLU_OUT_OF_MEMORY;
>>         }
>>
>>         /* copy image from srcImage into newMipmapImage by rows */
>>
>
> I'll push this patch with a few minor changes:
> 1. fix indentation to match surrounding code.
> 2. remove if-test around the second free().  free(NULL) is a no-op.
> 3. remove unneeded comments.
> 4. List the bug URL in the check-in comment.
>
> Sound OK?
>
> Thanks for the patch!
>
> -Brian
>

---------------------------------------------------------------------
Intel Finland Oy
Registered Address: PL 281, 00181 Helsinki 
Business Identity Code: 0357606 - 4 
Domiciled in Helsinki 

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.