]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
fixed some allocation bugs
authorAndrew Tridgell <tridge@samba.org>
Sat, 30 Mar 2002 12:03:04 +0000 (13:03 +0100)
committerAndrew Tridgell <tridge@samba.org>
Sat, 30 Mar 2002 12:03:04 +0000 (13:03 +0100)
cleanup.c
util.c

index 4d0d649d49ece9c1b23bdb66e2f538d813673d56..a37a71c2ea6d5d446aa364efa84255d55d9ae603 100644 (file)
--- a/cleanup.c
+++ b/cleanup.c
@@ -64,7 +64,7 @@ static void traverse_fn(const char *fname, struct stat *st)
                files = x_realloc(files, sizeof(struct files *)*allocated);
        }
 
-       files[num_files] = x_malloc(sizeof(struct files *));
+       files[num_files] = x_malloc(sizeof(struct files));
        files[num_files]->fname = x_strdup(fname);
        files[num_files]->mtime = st->st_mtime;
        files[num_files]->size = file_size(st) / 1024;
@@ -108,6 +108,9 @@ void cleanup_dir(const char *dir, size_t maxfiles, size_t maxsize)
        size_threshold = maxsize * LIMIT_MULTIPLE;
        files_threshold = maxfiles * LIMIT_MULTIPLE;
 
+       num_files = 0;
+       total_size = 0;
+
        /* build a list of files */
        traverse(dir, traverse_fn);
 
@@ -120,7 +123,11 @@ void cleanup_dir(const char *dir, size_t maxfiles, size_t maxsize)
        for (i=0;i<num_files;i++) {
                free(files[i]->fname);
                free(files[i]);
+               files[i] = NULL;
        }
+       if (files) free(files);
+       allocated = 0;
+       files = NULL;
 
        num_files = 0;
        total_size = 0;
diff --git a/util.c b/util.c
index d15c91e2402e54823a393420e8287871fa71546f..7ef5baee4e8c5468d66fd2e7d86c7dc89952bfc5 100644 (file)
--- a/util.c
+++ b/util.c
@@ -156,16 +156,21 @@ void *x_malloc(size_t size)
 }
 
 /*
-  this is like strdup() but dies if the malloc fails
+  this is like realloc() but dies if the malloc fails
 */
 void *x_realloc(void *ptr, size_t size)
 {
+       void *p2;
        if (!ptr) return x_malloc(size);
-       ptr = realloc(ptr, size);
-       if (!ptr) {
+       p2 = malloc(size);
+       if (!p2) {
                fatal("out of memory in x_realloc");
        }
-       return ptr;
+       if (ptr) {
+               memcpy(p2, ptr, size);
+               free(ptr);
+       }
+       return p2;
 }