Ignore:
Timestamp:
Nov 13, 2003, 5:45:25 PM (20 years ago)
Author:
Sam Hocevar
Message:
  • libee/ee.c: + Error checking in ee_init(). + Pre-generate the empty line for ee_clear().
  • libee/sprite.c: + Better error checking in ee_sprite_load().
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/libee/sprite.c

    r159 r167  
    5555
    5656    sprite = malloc(sizeof(struct ee_sprite));
     57    if(sprite == NULL)
     58        goto sprite_alloc_failed;
     59
    5760    sprite->nf = 0;
    5861    sprite->frames = NULL;
     
    7275            break;
    7376
    74         if(sprite->nf++)
    75             sprite->frames = realloc(sprite->frames,
    76                                      sprite->nf * sizeof(struct ee_frame));
     77        if(sprite->nf)
     78        {
     79            void *tmp = realloc(sprite->frames,
     80                                (sprite->nf + 1) * sizeof(struct ee_frame));
     81            if(tmp == NULL)
     82                goto frame_failed;
     83            sprite->frames = tmp;
     84            sprite->nf++;
     85        }
    7786        else
    78             sprite->frames = malloc(sprite->nf * sizeof(struct ee_frame));
     87        {
     88            sprite->frames = malloc((sprite->nf + 1) * sizeof(struct ee_frame));
     89            if(sprite->frames == NULL)
     90                goto sprite_failed;
     91            sprite->nf++;
     92        }
     93
    7994        frame = &sprite->frames[sprite->nf - 1];
    8095
     
    8499        frame->dy = dy;
    85100        frame->chars = malloc(w * h * sizeof(char));
     101        if(frame->chars == NULL)
     102        {
     103            sprite->nf--;
     104            goto frame_failed;
     105        }
    86106        frame->color = malloc(w * h * sizeof(int));
     107        if(frame->color == NULL)
     108        {
     109            free(frame->chars);
     110            sprite->nf--;
     111            goto frame_failed;
     112        }
    87113
    88114        for(y = 0; y < h; y++)
    89115        {
    90116            if(!fgets(buf, BUFSIZ, fd))
    91                 goto failed;
     117                goto frame_failed;
    92118
    93119            for(x = 0; x < w && buf[x] && buf[x] != '\r' && buf[x] != '\n'; x++)
     
    101127        {
    102128            if(!fgets(buf, BUFSIZ, fd))
    103                 goto failed;
     129                goto frame_failed;
    104130
    105131            for(x = 0; x < w && buf[x] && buf[x] != '\r' && buf[x] != '\n'; x++)
     
    111137
    112138        continue;
    113 
    114     failed:
     139    }
     140
     141    if(sprite->nf == 0)
     142        goto sprite_failed;
     143
     144    fclose(fd);
     145    return sprite;
     146
     147frame_failed:
     148    while(sprite->nf)
     149    {
     150        free(sprite->frames[sprite->nf - 1].color);
    115151        free(sprite->frames[sprite->nf - 1].chars);
    116         free(sprite->frames[sprite->nf - 1].color);
    117152        sprite->nf--;
    118         break;
    119     }
    120 
     153    }
     154sprite_failed:
     155    free(sprite);
     156sprite_alloc_failed:
    121157    fclose(fd);
    122 
    123     if(sprite->nf == 0)
    124     {
    125         free(sprite);
    126         return NULL;
    127     }
    128 
    129     return sprite;
     158    return NULL;
    130159}
    131160
Note: See TracChangeset for help on using the changeset viewer.