Use asprintf for string construction

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>
This commit is contained in:
Simon Wilkinson 2012-05-16 20:23:41 +01:00 committed by Derrick Brashear
parent bd1248ca39
commit 044785d587
11 changed files with 25 additions and 72 deletions

View 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;
}

View 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,

View 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;
}

View 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;
}

View 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:

View 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;
}

View 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);

View 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) {

View 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;

View 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)

View 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);