diff --git a/src/afsd/afsd_fuse.c b/src/afsd/afsd_fuse.c index 6ce22d48dc..23653080cb 100644 --- a/src/afsd/afsd_fuse.c +++ b/src/afsd/afsd_fuse.c @@ -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; } diff --git a/src/afsweb/apache_afs_plugin.c b/src/afsweb/apache_afs_plugin.c index 7bc3c54644..8ead201c08 100644 --- a/src/afsweb/apache_afs_plugin.c +++ b/src/afsweb/apache_afs_plugin.c @@ -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, diff --git a/src/aklog/aklog.c b/src/aklog/aklog.c index 6c5a3bed61..d78ff7b2be 100644 --- a/src/aklog/aklog.c +++ b/src/aklog/aklog.c @@ -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; } diff --git a/src/bozo/bnode.c b/src/bozo/bnode.c index 1d080abf46..a79558cfdc 100644 --- a/src/bozo/bnode.c +++ b/src/bozo/bnode.c @@ -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; } diff --git a/src/bozo/bosoprocs.c b/src/bozo/bosoprocs.c index 9c765e568a..54dda55bd2 100644 --- a/src/bozo/bosoprocs.c +++ b/src/bozo/bosoprocs.c @@ -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: diff --git a/src/bozo/bosserver.c b/src/bozo/bosserver.c index 5782c4eeb3..77c86f5cb2 100644 --- a/src/bozo/bosserver.c +++ b/src/bozo/bosserver.c @@ -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; } diff --git a/src/budb/procs.c b/src/budb/procs.c index f53368f68f..ebc6b690f5 100644 --- a/src/budb/procs.c +++ b/src/budb/procs.c @@ -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); diff --git a/src/budb/server.c b/src/budb/server.c index 8a9c92f33f..d78b2822fc 100644 --- a/src/budb/server.c +++ b/src/budb/server.c @@ -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) { diff --git a/src/ptserver/db_verify.c b/src/ptserver/db_verify.c index 2a95e349c3..6a04ab16cb 100644 --- a/src/ptserver/db_verify.c +++ b/src/ptserver/db_verify.c @@ -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; diff --git a/src/ptserver/ptutils.c b/src/ptserver/ptutils.c index a5971215f7..cfbe83f043 100644 --- a/src/ptserver/ptutils.c +++ b/src/ptserver/ptutils.c @@ -405,10 +405,7 @@ CreateEntry(struct ubik_trans *at, char aname[PR_MAXNAMELEN], afs_int32 *aid, af /* To create the user @ the group AUTHUSER_GROUP@ * 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, ¢ry); free(cellGroup); if (!pos) diff --git a/src/util/dirpath.c b/src/util/dirpath.c index 5d09e82a37..b307184068 100644 --- a/src/util/dirpath.c +++ b/src/util/dirpath.c @@ -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);