Changeset 157 in libcf for trunk/src/cf_file.c


Ignore:
Timestamp:
11/26/13 09:06:48 (10 years ago)
Author:
cheese
Message:

#1 fix memory leakage

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/cf_file.c

    r154 r157  
    234234CF_File_GetSize (const char * path)
    235235{
    236     int length = 0;
    237236    int result = 0;
    238237
     
    241240    ASSERT_ARGS (path == NULL);
    242241
    243     result = CF_File_Open ((cf_ctx *)&context, path, CF_FILE_READ);
    244     if (result < 0)
    245         return CF_ERROR_FILE_OPEN;
    246 
    247     length = (int) lseek (context->fd, 0, SEEK_END);
    248     if (length < 0)
    249         return CF_ERROR_FILE_GET_SIZE;
     242    TRY
     243    {
     244        result = CF_File_Open ((cf_ctx *)&context, path, CF_FILE_READ);
     245        if (result < 0)
     246        {
     247            result = CF_ERROR_FILE_OPEN;
     248            TRY_BREAK;
     249        }
     250
     251        result = (int) lseek (context->fd, 0, SEEK_END);
     252        if (result < 0)
     253        {
     254            result = CF_ERROR_FILE_GET_SIZE;
     255            TRY_BREAK;
     256        }
     257    }
     258    NO_CATCH;
    250259
    251260    CF_File_Close ((cf_ctx)context);
    252261
    253     return length;
     262    return result;
    254263}
    255264
     
    287296    ASSERT_ARGS (path == NULL);
    288297
    289     length = strlen (path);
    290     fullpath = (char *) calloc (length + 2, 1);
    291     if (!fullpath)
    292         return CF_ERROR_FILE_ALLOCATE_BUFFER;
    293 
    294     f = fullpath;
    295     d = stepPath;
    296 
    297     snprintf (fullpath, length + 1, "%s%c", path, DELIMITER);
    298 
    299     for (*d++ = *f++ ; *f ; *d++ = *f++)
     298    TRY
    300299    {
    301         if (*f != DELIMITER)
    302             continue;
    303 
    304         if (CF_File_Exists (stepPath))
    305             continue;
     300        length = strlen (path);
     301        fullpath = (char *) calloc (length + 2, 1);
     302        if (!fullpath)
     303        {
     304            result = CF_ERROR_FILE_ALLOCATE_BUFFER;
     305            TRY_BREAK;
     306        }
     307
     308        f = fullpath;
     309        d = stepPath;
     310
     311        snprintf (fullpath, length + 1, "%s%c", path, DELIMITER);
     312
     313        for (*d++ = *f++ ; *f ; *d++ = *f++)
     314        {
     315            if (*f != DELIMITER)
     316                continue;
     317
     318            if (CF_File_Exists (stepPath))
     319                continue;
    306320
    307321#define DIRECTORY_MODE  (S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
    308         result = mkdir (stepPath, DIRECTORY_MODE);
    309         if (result && errno != EEXIST)
    310             return CF_ERROR_FILE_MAKE_DIRECTORY;
     322            result = mkdir (stepPath, DIRECTORY_MODE);
     323            if (result && errno != EEXIST)
     324            {
     325                result = CF_ERROR_FILE_MAKE_DIRECTORY;
     326                break;
     327            }
     328        }
    311329    }
    312 
    313     return CF_OK;
     330    NO_CATCH;
     331
     332    if (fullpath)   free (fullpath);
     333
     334    return result;
    314335}
    315336
Note: See TracChangeset for help on using the changeset viewer.