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 1831

Summary: Memory leak issue in SDL_image-1.2.12/IMG_xpm.c file
Product: SDL_image Reporter: Nitz <nitin.j4>
Component: miscAssignee: Sam Lantinga <slouken>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: major    
Priority: P2    
Version: 1.2.12   
Hardware: x86   
OS: Linux   

Description Nitz 2013-05-02 04:36:23 UTC
static struct color_hash *create_colorhash(int maxnum)
{
     int bytes, s;
     struct color_hash *hash;

     /* we know how many entries we need, so we can allocate
        everything here */
     hash = malloc(sizeof *hash);
     if(!hash)
      return NULL;

     /* use power-of-2 sized hash table for decoding speed */
     for(s = STARTING_HASH_SIZE; s < maxnum; s <<= 1)
          ;
     hash->size = s;
     hash->maxnum = maxnum;
     bytes = hash->size * sizeof(struct hash_entry **);
     hash->entries = NULL; /* in case malloc fails */
     hash->table = malloc(bytes);
     if(!hash->table)

     {

       free(hash);  // 1) This line is added
       return NULL;

     }
     memset(hash->table, 0, bytes);
     hash->entries = malloc(maxnum * sizeof(struct hash_entry));
     if(!hash->entries) {
      free(hash->table);

      free(hash);  // 2) This line is added
      return NULL;
     }
     hash->next_free = hash->entries;
     return hash;
}  

In this code hash memory is dynamically allocated, but its getting leak at 2 points.

So its important to free this hash memory, Patch is applied at 2 points.

Thanks & Regards,
Nitz
Comment 1 Sam Lantinga 2013-05-22 00:36:10 UTC
Fixed, thanks!
http://hg.libsdl.org/SDL_image/rev/35beff028453