queryfile: more efficient allocation
authorErez_Zadok <ezk@cs.sunysb.edu>
Sun, 23 Sep 2007 17:36:43 +0000 (13:36 -0400)
committerErez_Zadok <ezk@cs.sunysb.edu>
Sun, 23 Sep 2007 17:36:43 +0000 (13:36 -0400)
Also avoids repeated calls to realloc(), which appear to tickle a possible
glibc bug.

progs/queryfile.c

index ac09a1a1ebac5204f8615195eab218c0c8256a89..b3ad21816f72e2e77886d7132a0b73e094215dc8 100644 (file)
@@ -325,7 +325,7 @@ int unionfs_query(const char *file_path, struct unionfs_branch **ufs_branches)
   }
 
   ret = 0;
-  *ufs_branches = malloc(sizeof(struct unionfs_branch));
+  *ufs_branches = malloc(sizeof(struct unionfs_branch)*(len+1));
   if (!(*ufs_branches)) {
     errno = ENOMEM;
     return -1;
@@ -334,10 +334,7 @@ int unionfs_query(const char *file_path, struct unionfs_branch **ufs_branches)
 
   for (i = 0; i <= len; i++) {
     if (FD_ISSET(i, &branchlist)) {
-      *ufs_branches = realloc(*ufs_branches,
-                             sizeof(struct unionfs_branch)*(ret+1));
-      (*ufs_branches)[ret].path = malloc(strlen(branches[ret]+1));
-      strcpy((*ufs_branches)[ret].path, branches[i]);
+      (*ufs_branches)[ret].path = strdup(branches[i]);
       (*ufs_branches)[ret].perms = branchperms[i];
       ret++;
     }