[Mesa-dev] [PATCH 3/3] mesa: Verify memory allocations success in _mesa_PushAttrib

Juha-Pekka Heikkilä juhapekka.heikkila at gmail.com
Tue Dec 3 12:43:55 PST 2013


On Tue, Dec 3, 2013 at 6:51 PM, Ian Romanick <idr at freedesktop.org> wrote:
> On 12/02/2013 01:39 AM, Juha-Pekka Heikkila wrote:
>> Check if any of the callocs fail and report it with _mesa_error
>> if needed.
>>
>> Signed-off-by: Juha-Pekka Heikkila <juhapekka.heikkila at gmail.com>
>> ---
>>  src/mesa/main/attrib.c | 106 ++++++++++++++++++++++++++++++++++++++++++++++++-
>>  1 file changed, 104 insertions(+), 2 deletions(-)
>>
>> diff --git a/src/mesa/main/attrib.c b/src/mesa/main/attrib.c
>> index 2418fb0..037cd64 100644
>> --- a/src/mesa/main/attrib.c
>> +++ b/src/mesa/main/attrib.c
>> @@ -222,6 +222,11 @@ _mesa_PushAttrib(GLbitfield mask)
>>     if (mask & GL_ACCUM_BUFFER_BIT) {
>>        struct gl_accum_attrib *attr;
>>        attr = MALLOC_STRUCT( gl_accum_attrib );
>> +      if (attr == NULL) {
>> +         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
>> +         goto end;
>> +      }
>> +
>>        memcpy( attr, &ctx->Accum, sizeof(struct gl_accum_attrib) );
>>        save_attrib_data(&head, GL_ACCUM_BUFFER_BIT, attr);
>
> And what if save_attrib_data fails to allocate memory for head?

I see I read save_attrib_data function too quick. Looks this would
leak the memory allocated in attr if save_attrib_data fails, I'll add
few bits here. Thanks Ian.

>
>>     }
>> @@ -230,6 +235,11 @@ _mesa_PushAttrib(GLbitfield mask)
>>        GLuint i;
>>        struct gl_colorbuffer_attrib *attr;
>>        attr = MALLOC_STRUCT( gl_colorbuffer_attrib );
>> +      if (attr == NULL) {
>> +         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
>> +         goto end;
>> +      }
>> +
>>        memcpy( attr, &ctx->Color, sizeof(struct gl_colorbuffer_attrib) );
>>        /* push the Draw FBO's DrawBuffer[] state, not ctx->Color.DrawBuffer[] */
>>        for (i = 0; i < ctx->Const.MaxDrawBuffers; i ++)
>> @@ -241,6 +251,11 @@ _mesa_PushAttrib(GLbitfield mask)
>>        struct gl_current_attrib *attr;
>>        FLUSH_CURRENT( ctx, 0 );
>>        attr = MALLOC_STRUCT( gl_current_attrib );
>> +      if (attr == NULL) {
>> +         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
>> +         goto end;
>> +      }
>> +
>>        memcpy( attr, &ctx->Current, sizeof(struct gl_current_attrib) );
>>        save_attrib_data(&head, GL_CURRENT_BIT, attr);
>>     }
>> @@ -248,6 +263,11 @@ _mesa_PushAttrib(GLbitfield mask)
>>     if (mask & GL_DEPTH_BUFFER_BIT) {
>>        struct gl_depthbuffer_attrib *attr;
>>        attr = MALLOC_STRUCT( gl_depthbuffer_attrib );
>> +      if (attr == NULL) {
>> +         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
>> +         goto end;
>> +      }
>> +
>>        memcpy( attr, &ctx->Depth, sizeof(struct gl_depthbuffer_attrib) );
>>        save_attrib_data(&head, GL_DEPTH_BUFFER_BIT, attr);
>>     }
>> @@ -256,6 +276,11 @@ _mesa_PushAttrib(GLbitfield mask)
>>        struct gl_enable_attrib *attr;
>>        GLuint i;
>>        attr = MALLOC_STRUCT( gl_enable_attrib );
>> +      if (attr == NULL) {
>> +         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
>> +         goto end;
>> +      }
>> +
>>        /* Copy enable flags from all other attributes into the enable struct. */
>>        attr->AlphaTest = ctx->Color.AlphaEnabled;
>>        attr->AutoNormal = ctx->Eval.AutoNormal;
>> @@ -331,6 +356,11 @@ _mesa_PushAttrib(GLbitfield mask)
>>     if (mask & GL_EVAL_BIT) {
>>        struct gl_eval_attrib *attr;
>>        attr = MALLOC_STRUCT( gl_eval_attrib );
>> +      if (attr == NULL) {
>> +         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
>> +         goto end;
>> +      }
>> +
>>        memcpy( attr, &ctx->Eval, sizeof(struct gl_eval_attrib) );
>>        save_attrib_data(&head, GL_EVAL_BIT, attr);
>>     }
>> @@ -338,6 +368,11 @@ _mesa_PushAttrib(GLbitfield mask)
>>     if (mask & GL_FOG_BIT) {
>>        struct gl_fog_attrib *attr;
>>        attr = MALLOC_STRUCT( gl_fog_attrib );
>> +      if (attr == NULL) {
>> +         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
>> +         goto end;
>> +      }
>> +
>>        memcpy( attr, &ctx->Fog, sizeof(struct gl_fog_attrib) );
>>        save_attrib_data(&head, GL_FOG_BIT, attr);
>>     }
>> @@ -345,6 +380,11 @@ _mesa_PushAttrib(GLbitfield mask)
>>     if (mask & GL_HINT_BIT) {
>>        struct gl_hint_attrib *attr;
>>        attr = MALLOC_STRUCT( gl_hint_attrib );
>> +      if (attr == NULL) {
>> +         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
>> +         goto end;
>> +      }
>> +
>>        memcpy( attr, &ctx->Hint, sizeof(struct gl_hint_attrib) );
>>        save_attrib_data(&head, GL_HINT_BIT, attr);
>>     }
>> @@ -353,6 +393,11 @@ _mesa_PushAttrib(GLbitfield mask)
>>        struct gl_light_attrib *attr;
>>        FLUSH_CURRENT(ctx, 0); /* flush material changes */
>>        attr = MALLOC_STRUCT( gl_light_attrib );
>> +      if (attr == NULL) {
>> +         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
>> +         goto end;
>> +      }
>> +
>>        memcpy( attr, &ctx->Light, sizeof(struct gl_light_attrib) );
>>        save_attrib_data(&head, GL_LIGHTING_BIT, attr);
>>     }
>> @@ -360,6 +405,11 @@ _mesa_PushAttrib(GLbitfield mask)
>>     if (mask & GL_LINE_BIT) {
>>        struct gl_line_attrib *attr;
>>        attr = MALLOC_STRUCT( gl_line_attrib );
>> +      if (attr == NULL) {
>> +         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
>> +         goto end;
>> +      }
>> +
>>        memcpy( attr, &ctx->Line, sizeof(struct gl_line_attrib) );
>>        save_attrib_data(&head, GL_LINE_BIT, attr);
>>     }
>> @@ -367,6 +417,11 @@ _mesa_PushAttrib(GLbitfield mask)
>>     if (mask & GL_LIST_BIT) {
>>        struct gl_list_attrib *attr;
>>        attr = MALLOC_STRUCT( gl_list_attrib );
>> +      if (attr == NULL) {
>> +         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
>> +         goto end;
>> +      }
>> +
>>        memcpy( attr, &ctx->List, sizeof(struct gl_list_attrib) );
>>        save_attrib_data(&head, GL_LIST_BIT, attr);
>>     }
>> @@ -374,6 +429,11 @@ _mesa_PushAttrib(GLbitfield mask)
>>     if (mask & GL_PIXEL_MODE_BIT) {
>>        struct gl_pixel_attrib *attr;
>>        attr = MALLOC_STRUCT( gl_pixel_attrib );
>> +      if (attr == NULL) {
>> +         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
>> +         goto end;
>> +      }
>> +
>>        memcpy( attr, &ctx->Pixel, sizeof(struct gl_pixel_attrib) );
>>        /* push the Read FBO's ReadBuffer state, not ctx->Pixel.ReadBuffer */
>>        attr->ReadBuffer = ctx->ReadBuffer->ColorReadBuffer;
>> @@ -383,6 +443,11 @@ _mesa_PushAttrib(GLbitfield mask)
>>     if (mask & GL_POINT_BIT) {
>>        struct gl_point_attrib *attr;
>>        attr = MALLOC_STRUCT( gl_point_attrib );
>> +      if (attr == NULL) {
>> +         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
>> +         goto end;
>> +      }
>> +
>>        memcpy( attr, &ctx->Point, sizeof(struct gl_point_attrib) );
>>        save_attrib_data(&head, GL_POINT_BIT, attr);
>>     }
>> @@ -390,6 +455,11 @@ _mesa_PushAttrib(GLbitfield mask)
>>     if (mask & GL_POLYGON_BIT) {
>>        struct gl_polygon_attrib *attr;
>>        attr = MALLOC_STRUCT( gl_polygon_attrib );
>> +      if (attr == NULL) {
>> +         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
>> +         goto end;
>> +      }
>> +
>>        memcpy( attr, &ctx->Polygon, sizeof(struct gl_polygon_attrib) );
>>        save_attrib_data(&head, GL_POLYGON_BIT, attr);
>>     }
>> @@ -397,6 +467,11 @@ _mesa_PushAttrib(GLbitfield mask)
>>     if (mask & GL_POLYGON_STIPPLE_BIT) {
>>        GLuint *stipple;
>>        stipple = malloc( 32*sizeof(GLuint) );
>> +      if (stipple == NULL) {
>> +         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
>> +         goto end;
>> +      }
>> +
>>        memcpy( stipple, ctx->PolygonStipple, 32*sizeof(GLuint) );
>>        save_attrib_data(&head, GL_POLYGON_STIPPLE_BIT, stipple);
>>     }
>> @@ -404,6 +479,11 @@ _mesa_PushAttrib(GLbitfield mask)
>>     if (mask & GL_SCISSOR_BIT) {
>>        struct gl_scissor_attrib *attr;
>>        attr = MALLOC_STRUCT( gl_scissor_attrib );
>> +      if (attr == NULL) {
>> +         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
>> +         goto end;
>> +      }
>> +
>>        memcpy( attr, &ctx->Scissor, sizeof(struct gl_scissor_attrib) );
>>        save_attrib_data(&head, GL_SCISSOR_BIT, attr);
>>     }
>> @@ -411,6 +491,11 @@ _mesa_PushAttrib(GLbitfield mask)
>>     if (mask & GL_STENCIL_BUFFER_BIT) {
>>        struct gl_stencil_attrib *attr;
>>        attr = MALLOC_STRUCT( gl_stencil_attrib );
>> +      if (attr == NULL) {
>> +         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
>> +         goto end;
>> +      }
>> +
>>        memcpy( attr, &ctx->Stencil, sizeof(struct gl_stencil_attrib) );
>>        save_attrib_data(&head, GL_STENCIL_BUFFER_BIT, attr);
>>     }
>> @@ -457,6 +542,11 @@ _mesa_PushAttrib(GLbitfield mask)
>>     if (mask & GL_TRANSFORM_BIT) {
>>        struct gl_transform_attrib *attr;
>>        attr = MALLOC_STRUCT( gl_transform_attrib );
>> +      if (attr == NULL) {
>> +         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
>> +         goto end;
>> +      }
>> +
>>        memcpy( attr, &ctx->Transform, sizeof(struct gl_transform_attrib) );
>>        save_attrib_data(&head, GL_TRANSFORM_BIT, attr);
>>     }
>> @@ -464,6 +554,11 @@ _mesa_PushAttrib(GLbitfield mask)
>>     if (mask & GL_VIEWPORT_BIT) {
>>        struct gl_viewport_attrib *attr;
>>        attr = MALLOC_STRUCT( gl_viewport_attrib );
>> +      if (attr == NULL) {
>> +         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
>> +         goto end;
>> +      }
>> +
>>        memcpy( attr, &ctx->Viewport, sizeof(struct gl_viewport_attrib) );
>>        save_attrib_data(&head, GL_VIEWPORT_BIT, attr);
>>     }
>> @@ -472,13 +567,20 @@ _mesa_PushAttrib(GLbitfield mask)
>>     if (mask & GL_MULTISAMPLE_BIT_ARB) {
>>        struct gl_multisample_attrib *attr;
>>        attr = MALLOC_STRUCT( gl_multisample_attrib );
>> +      if (attr == NULL) {
>> +         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushAttrib");
>> +         goto end;
>> +      }
>> +
>>        memcpy( attr, &ctx->Multisample, sizeof(struct gl_multisample_attrib) );
>>        save_attrib_data(&head, GL_MULTISAMPLE_BIT_ARB, attr);
>>     }
>>
>>  end:
>> -   ctx->AttribStack[ctx->AttribStackDepth] = head;
>> -   ctx->AttribStackDepth++;
>> +   if (head != NULL) {
>> +       ctx->AttribStack[ctx->AttribStackDepth] = head;
>> +       ctx->AttribStackDepth++;
>> +   }
>>  }
>>
>>
>>
>


More information about the mesa-dev mailing list