Use asprintf for string construction
authorSimon Wilkinson <sxw@your-file-system.com>
Wed, 16 May 2012 19:23:41 +0000 (20:23 +0100)
committerDerrick Brashear <shadow@dementix.org>
Thu, 24 May 2012 15:48:35 +0000 (08:48 -0700)
Rather than using something along the lines of

    strOut = malloc(strlen(strA) + strlen(strB) + strlen(strC) + 1);
    strcpy(strOut, strA);
    strcat(strOut, strB);
    strcat(strOut, strC);

use asprintf for string construction, so we can just write

    asprintf(&strOut, "%s%s%s", strA, strB, strC);

roken provides an implementation of asprintf for platforms which are
missing one.

Change-Id: Ieef9f4b65f72260c0d372cdf3865daab98733ad9
Reviewed-on: http://gerrit.openafs.org/7451
Tested-by: BuildBot <buildbot@rampaginggeek.com>
Reviewed-by: Derrick Brashear <shadow@dementix.org>

src/afsd/afsd_fuse.c
src/afsweb/apache_afs_plugin.c
src/aklog/aklog.c
src/bozo/bnode.c
src/bozo/bosoprocs.c
src/bozo/bosserver.c
src/budb/procs.c
src/budb/server.c
src/ptserver/db_verify.c
src/ptserver/ptutils.c
src/util/dirpath.c

index 6ce22d4..2365308 100644 (file)
@@ -74,15 +74,10 @@ static int stderr_save;
 static char *
 afs_path(const char *apath)
 {
-    size_t len;
     static const char prefix[] = "/afs/";
     char *path;
 
-    len = strlen(apath) + sizeof(prefix);
-
-    path = malloc(len);
-
-    sprintf(path, "%s%s", prefix, apath);
+    asprint(&path, "%s%s", prefix, apath);
 
     return path;
 }
index 7bc3c54..8ead201 100644 (file)
@@ -98,14 +98,13 @@ afs_plugin_init(int tokenExpiration, char *weblogPath, char *error_fname,
                module_name);
        exit(-1);
     }
-    afs_weblog_pidfile = (char *)malloc(strlen(httpd_pid_fname) + 5);
-    if (httpd_pid_fname == NULL) {
+    asprintf(&afs_weblog_pidfile, "%s.afs", httpd_pid_fname);
+    if (afs_weblog_pidfile == NULL) {
        fprintf(stderr,
                "%s: malloc failed - out of memory while allocating space for afs_weblog_pidfile\n",
                module_name);
        exit(-1);
     }
-    sprintf(afs_weblog_pidfile, "%s.afs", httpd_pid_fname);
 
     if (do_setpag()) {
        fprintf(stderr, "%s:Failed to set pag Error:%d\n", module_name,
index 6c5a3be..d78ff7b 100644 (file)
@@ -841,7 +841,6 @@ rxkad_get_token(krb5_context context, struct afsconf_cell *cell, char *realm,
     char *realmUsed = NULL;
     char *username = NULL;
     int status;
-    size_t len;
 
     *token = NULL;
     *authuser = NULL;
@@ -866,9 +865,7 @@ rxkad_get_token(krb5_context context, struct afsconf_cell *cell, char *realm,
        username = NULL;
        *foreign = 0;
     } else {
-       len = strlen(username)+strlen(realmUsed)+2;
-       *authuser = malloc(len);
-       snprintf(*authuser, len, "%s@%s", username, realmUsed);
+       asprintf(authuser, "%s@%s", username, realmUsed);
        *foreign = 1;
     }
 
index 1d080ab..a79558c 100644 (file)
@@ -110,7 +110,6 @@ SaveCore(struct bnode *abnode, struct bnode_proc
     if (code) {
         DIR *logdir;
         struct dirent *file;
-        size_t length;
         unsigned long pid;
        const char *coredir = AFSDIR_LOGS_DIR;
 
@@ -125,13 +124,11 @@ SaveCore(struct bnode *abnode, struct bnode_proc
                 continue;
             pid = atol(file->d_name + 5);
             if (pid == aproc->pid) {
-                length = strlen(coredir) + strlen(file->d_name) + 2;
-                corefile = malloc(length);
+                asprintf(&corefile, "%s/%s", coredir, file->d_name);
                 if (corefile == NULL) {
                     closedir(logdir);
                     return;
                 }
-                snprintf(corefile, length, "%s/%s", coredir, file->d_name);
                 code = 0;
                 break;
             }
index 9c765e5..54dda55 100644 (file)
@@ -425,13 +425,10 @@ SBOZO_GetCellHost(struct rx_call *acall, afs_uint32 awhich, char **aname)
     }
 
     tp = tcell.hostName[awhich];
-    *aname = (char *)malloc(strlen(tp) + 3);
     if (clones[awhich]) {
-       strcpy(*aname, "[");
-       strcat(*aname, tp);
-       strcat(*aname, "]");
+       asprintf(aname, "[%s]", tp);
     } else
-       strcpy(*aname, tp);
+       *aname = strdup(tp);
     goto done;
 
   fail:
index 5782c4e..77c86f5 100644 (file)
@@ -686,28 +686,18 @@ static char *
 make_pid_filename(char *ainst, char *aname)
 {
     char *buffer = NULL;
-    int length;
 
-    length = strlen(DoPidFiles) + strlen(ainst) + 6;
     if (aname && *aname) {
-       length += strlen(aname) + 1;
-    }
-    buffer = malloc(length * sizeof(char));
-    if (!buffer) {
-       if (aname) {
+       asprintf(&buffer, "%s/%s.%s.pid", DoPidFiles, ainst, aname);
+       if (buffer == NULL)
            bozo_Log("Failed to alloc pid filename buffer for %s.%s.\n",
                     ainst, aname);
-       } else {
-           bozo_Log("Failed to alloc pid filename buffer for %s.\n", ainst);
-       }
     } else {
-       if (aname && *aname) {
-           snprintf(buffer, length, "%s/%s.%s.pid", DoPidFiles, ainst,
-                    aname);
-       } else {
-           snprintf(buffer, length, "%s/%s.pid", DoPidFiles, ainst);
-       }
+       asprintf(&buffer, "%s/%s.pid", DoPidFiles, ainst);
+       if (buffer == NULL)
+           bozo_Log("Failed to alloc pid filename buffer for %s.\n", ainst);
     }
+
     return buffer;
 }
 
index f53368f..ebc6b69 100644 (file)
@@ -3534,12 +3534,10 @@ T_DumpDatabase(struct rx_call *call, char *filename)
     if (!callPermitted(call))
        return BUDB_NOTPERMITTED;
 
-    path = (char *)malloc(strlen(gettmpdir()) + 1 + strlen(filename) + 1);
+    asprintf(&path, "%s/%s", gettmpdir(), filename);
     if (!path)
        return (BUDB_INTERNALERROR);
 
-    sprintf(path, "%s/%s", gettmpdir(), filename);
-
     dumpfid = fopen(path, "w");
     if (!dumpfid)
        return (BUDB_BADARGUMENT);
index 8a9c92f..d78b282 100644 (file)
@@ -324,17 +324,13 @@ truncateDatabase(void)
     afs_int32 code = 0;
     int fd;
 
-    path = malloc(strlen(globalConfPtr->databaseDirectory) +
-                 strlen(globalConfPtr->databaseName) +
-                 strlen(globalConfPtr->databaseExtension) + 1);
+    asprintf(&path, "%s%s%s",
+            globalConfPtr->databaseDirectory,
+            globalConfPtr->databaseName,
+            globalConfPtr->databaseExtension);
     if (path == NULL)
        ERROR(-1);
 
-    /* construct the database name */
-    strcpy(path, globalConfPtr->databaseDirectory);
-    strcat(path, globalConfPtr->databaseName);
-    strcat(path, globalConfPtr->databaseExtension);
-
     fd = open(path, O_RDWR, 0755);
     if (!fd) {
        code = errno;
@@ -506,16 +502,11 @@ main(int argc, char **argv)
 
     LogError(0, "Will allocate %d ubik buffers\n", ubik_nBuffers);
 
-    dbNamePtr =
-       (char *)malloc(strlen(globalConfPtr->databaseDirectory) +
-                      strlen(globalConfPtr->databaseName) + 1);
+    asprintf(&dbNamePtr, "%s%s", globalConfPtr->databaseDirectory,
+            globalConfPtr->databaseName);
     if (dbNamePtr == 0)
        ERROR(-1);
 
-    /* construct the database name */
-    strcpy(dbNamePtr, globalConfPtr->databaseDirectory);
-    strcat(dbNamePtr, globalConfPtr->databaseName);    /* name prefix */
-
     rx_SetRxDeadTime(60);      /* 60 seconds inactive before timeout */
 
     if (rxBind) {
index 2a95e34..6a04ab1 100644 (file)
@@ -962,10 +962,7 @@ QuoteName(char *s)
 {
     char *qs;
     if (strpbrk(s, " \t")) {
-       qs = (char *)malloc(strlen(s) + 3);
-       strcpy(qs, "\"");
-       strcat(qs, s);
-       strcat(qs, "\"");
+       asprintf(&qs, "\"%s\"", s);
     } else
        qs = s;
     return qs;
index a597121..cfbe83f 100644 (file)
@@ -405,10 +405,7 @@ CreateEntry(struct ubik_trans *at, char aname[PR_MAXNAMELEN], afs_int32 *aid, af
        /* To create the user <name>@<cell> the group AUTHUSER_GROUP@<cell>
         * must exist.
         */
-       cellGroup =
-           (char *)malloc(strlen(AUTHUSER_GROUP) + strlen(atsign) + 1);
-       strcpy(cellGroup, AUTHUSER_GROUP);
-       strcat(cellGroup, atsign);
+       asprintf(&cellGroup, "%s%s", AUTHUSER_GROUP, atsign);
        pos = FindByName(at, cellGroup, &centry);
        free(cellGroup);
        if (!pos)
index 5d09e82..b307184 100644 (file)
@@ -643,16 +643,11 @@ ConstructLocalPath(const char *cpath, const char *relativeTo,
     LocalizePathHead(&cpath, &relativeTo);
     if (*cpath == '/') {
        newPath = strdup(cpath);
-       if (!newPath)
-           status = ENOMEM;
     } else {
-       newPath = (char *)malloc(strlen(relativeTo) + 1 + strlen(cpath) + 1);
-       if (!newPath) {
-           status = ENOMEM;
-       } else {
-           sprintf(newPath, "%s/%s", relativeTo, cpath);
-       }
+       asprintf(&newPath, "%s/%s", relativeTo, cpath);
     }
+    if (newPath == NULL)
+       status = ENOMEM;
 
     if (status == 0) {
        FilepathNormalize(newPath);