We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 2295

Summary: Memory leak in IMG_LoadWEBP_RW (SDL2_image-2.0.0)
Product: SDL_image Reporter: Timur <m_t>
Component: miscAssignee: Sam Lantinga <slouken>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2    
Version: unspecified   
Hardware: x86_64   
OS: Windows 7   
Attachments: fix html report
Patch

Description Timur 2013-12-11 09:19:08 UTC
Created attachment 1484 [details]
fix html report

SDL_Surface *IMG_LoadWEBP_RW(SDL_RWops *src)
{
    ...

    // buffer for data
    raw_data = (uint8_t*) SDL_malloc( raw_data_size );
    ...
    
    // read data from src into raw_data
    r = SDL_RWread(src, raw_data, 1, raw_data_size );
    ....
    
    // create surface and allocate memory for sdl surface
    surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
            features.width, features.height,
            features.has_alpha?32:24, Rmask,Gmask,Bmask,Amask);
    ...

    // decode raw_data, put result into surface
    ret = lib.webp_decode_rgba_into( raw_data, raw_data_size, (uint8_t 
    ...
    return surface;
    
    // raw_data never be freed
Comment 1 Timur 2013-12-11 09:47:44 UTC
Created attachment 1485 [details]
Patch
Comment 2 Sam Lantinga 2013-12-12 05:28:10 UTC
I fixed a memory leak on error in your patch.
https://hg.libsdl.org/SDL_image/rev/7a360f7dd99b
Thanks!
Comment 3 Timur 2013-12-12 08:25:49 UTC
If you look at an attached html report (or my patch) you will see that i put "SDL_free(raw_data)" call before "goto error" condition. So raw_data will be  freed on error too. Like this

<code>
        ret = lib.webp_decode_rgb_into( raw_data, raw_data_size, (uint8_t *)surface->pixels, surface->pitch * surface->h,  surface->pitch );
    }

    if ( raw_data ) {
        SDL_free( raw_data );
    }

    if ( !ret ) {
        error = "Failed to decode WEBP";
        goto error;
    }

    return surface;

error:
    if ( surface ) {
        SDL_FreeSurface( surface );
    }

    if ( error ) {
        IMG_SetError( error );
    }

    SDL_RWseek(src, start, RW_SEEK_SET);
    return(NULL);
}
</code>

Anyway your solution will work too.