From bd1248ca3988edb230174ff34c3ff79bedcf559e Mon Sep 17 00:00:00 2001 From: Simon Wilkinson Date: Wed, 16 May 2012 06:44:30 +0100 Subject: [PATCH] Use strdup to copy strings Where we have newStr = malloc(strlen(oldStr)+1); strcpy(newStr, oldStr); replace these with newStr = strdup(oldStr); It's shorter, clearer, and gets rid of a load of occurences of strcpy, which some compilers are now warning is unsafe (although it isn't in this context) Get rid of a number of custom duplicate string functions and replace them with strdup where the behaviour is identical Change-Id: If800343a7d13b1ba6362d4570a2a324fa3525250 Reviewed-on: http://gerrit.openafs.org/7450 Tested-by: BuildBot Reviewed-by: Derrick Brashear --- src/afsweb/apache_afs_plugin.c | 3 +-- src/bozo/bnode.c | 12 ++++-------- src/bozo/bosoprocs.c | 6 ++---- src/bozo/bosprototypes.h | 3 --- src/bozo/cronbnodeops.c | 2 +- src/bozo/fsbnodeops.c | 9 --------- src/bucoord/commands.c | 8 ++------ src/bucoord/config.c | 7 ++----- src/bucoord/dsvs.c | 15 +++++---------- src/bucoord/dump_sched.c | 3 +-- src/bucoord/restore.c | 13 +++---------- src/bucoord/tape_hosts.c | 3 +-- src/bucoord/vol_sets.c | 12 ++++-------- src/budb/server.c | 9 ++------- src/butc/tcmain.c | 25 ++++++++----------------- src/butc/tcprocs.c | 8 ++------ src/cmd/cmd.c | 27 +++++++++------------------ src/comerr/error_table.y | 24 +++++++----------------- src/comerr/error_table_nt.c | 22 ++++++---------------- src/comerr/et_lex.lex.l | 4 ++-- src/comerr/et_lex.lex_nt.c | 4 ++-- src/config/mc.c | 6 ++---- src/gtx/keymap.c | 7 +------ src/kauth/admin_tools.c | 3 +-- src/libadmin/cfg/cfgclient.c | 8 ++------ src/libadmin/cfg/cfgdb.c | 6 ++---- src/libadmin/cfg/cfghost.c | 13 +++---------- src/libadmin/test/bos.c | 3 +-- src/rx/bulktest/bulk_client.c | 13 ++----------- src/tools/dumpscan/directory.c | 3 +-- src/tools/dumpscan/pathname.c | 3 +-- src/tools/dumpscan/repair.c | 6 ++---- src/tools/dumpscan/stagehdr.c | 9 +++------ src/ubik/ubik.c | 3 +-- src/update/utils.c | 3 +-- src/usd/usd_file.c | 3 +-- src/usd/usd_nt.c | 3 +-- src/uss/uss_procs.c | 6 ++---- src/uss/uss_vol.c | 6 ++---- src/util/dirpath.c | 14 ++++---------- src/util/ktime.c | 3 +-- src/venus/fstrace.c | 6 ++---- src/vfsck/main.c | 10 +++------- src/viced/afsfileprocs.c | 9 ++------- src/vol/partition.c | 6 ++---- src/vol/test/listVicepx.c | 4 +--- src/vol/test/utilities.c | 3 +-- src/vol/vol-salvage.c | 8 ++------ 48 files changed, 111 insertions(+), 275 deletions(-) diff --git a/src/afsweb/apache_afs_plugin.c b/src/afsweb/apache_afs_plugin.c index 42798de..7bc3c54 100644 --- a/src/afsweb/apache_afs_plugin.c +++ b/src/afsweb/apache_afs_plugin.c @@ -91,14 +91,13 @@ afs_plugin_init(int tokenExpiration, char *weblogPath, char *error_fname, FILE *fp; /* for pid_fname */ char *afs_weblog_pidfile; - char *httpd_pid_fname = (char *)malloc(strlen(pf) + 1); + char *httpd_pid_fname = strdup(pf); if (httpd_pid_fname == NULL) { fprintf(stderr, "%s: malloc failed - out of memory while allocating space for httpd_pid_fname\n", module_name); exit(-1); } - strcpy(httpd_pid_fname, pf); afs_weblog_pidfile = (char *)malloc(strlen(httpd_pid_fname) + 5); if (httpd_pid_fname == NULL) { fprintf(stderr, diff --git a/src/bozo/bnode.c b/src/bozo/bnode.c index 629d492..1d080ab 100644 --- a/src/bozo/bnode.c +++ b/src/bozo/bnode.c @@ -60,10 +60,8 @@ RememberProcName(struct bnode_proc *ap) free(tbnodep->lastErrorName); tbnodep->lastErrorName = NULL; } - if (ap->coreName) { - tbnodep->lastErrorName = (char *)malloc(strlen(ap->coreName) + 1); - strcpy(tbnodep->lastErrorName, ap->coreName); - } + if (ap->coreName) + tbnodep->lastErrorName = strdup(ap->coreName); } /* utility for use by BOP_HASCORE functions to determine where a core file might @@ -503,10 +501,9 @@ bnode_InitBnode(struct bnode *abnode, struct bnode_ops *abnodeops, /* format the bnode properly */ memset(abnode, 0, sizeof(struct bnode)); abnode->ops = abnodeops; - abnode->name = (char *)malloc(strlen(aname) + 1); + abnode->name = strdup(aname); if (!abnode->name) return ENOMEM; - strcpy(abnode->name, aname); abnode->flags = BNODE_ACTIVE; abnode->fileGoal = BSTAT_NORMAL; abnode->goal = BSTAT_SHUTDOWN; @@ -921,8 +918,7 @@ bnode_ParseLine(char *aline, struct bnode_token **alist) ttok = (struct bnode_token *)malloc(sizeof(struct bnode_token)); ttok->next = (struct bnode_token *)0; - ttok->key = (char *)malloc(strlen(tbuffer) + 1); - strcpy(ttok->key, tbuffer); + ttok->key = strdup(tbuffer); if (last) { last->next = ttok; last = ttok; diff --git a/src/bozo/bosoprocs.c b/src/bozo/bosoprocs.c index 4510660..9c765e5 100644 --- a/src/bozo/bosoprocs.c +++ b/src/bozo/bosoprocs.c @@ -399,8 +399,7 @@ SBOZO_GetCellName(struct rx_call *acall, char **aname) *aname = (char *)malloc(1); **aname = 0; } else { - *aname = (char *)malloc(strlen(tname) + 1); - strcpy(*aname, tname); + *aname = strdup(tname); } return code; @@ -1486,8 +1485,7 @@ SBOZO_GetInstanceStrings(struct rx_call *acall, char *abnodeName, /* now, return the appropriate error string, if any */ if (tb->lastErrorName) { - *as1 = (char *)malloc(strlen(tb->lastErrorName) + 1); - strcpy(*as1, tb->lastErrorName); + *as1 = strdup(tb->lastErrorName); } else { *as1 = (char *)malloc(1); **as1 = 0; diff --git a/src/bozo/bosprototypes.h b/src/bozo/bosprototypes.h index 116f989..4d2132d 100644 --- a/src/bozo/bosprototypes.h +++ b/src/bozo/bosprototypes.h @@ -52,9 +52,6 @@ void *bozo_ShutdownAndExit(void *arock /* really int asignal */); int initBosEntryStats(void); int DirAccessOK(void); -/* fsbnodeops.c */ -char *copystr(char *a); - /* inline functions */ static_inline struct bozo_key * ktc_to_bozoptr(struct ktc_encryptionKey *key) { diff --git a/src/bozo/cronbnodeops.c b/src/bozo/cronbnodeops.c index ad2f641..3346033 100644 --- a/src/bozo/cronbnodeops.c +++ b/src/bozo/cronbnodeops.c @@ -171,7 +171,7 @@ cron_create(char *ainstance, char *acommand, char *awhen, } te->when = ktime_next(&te->whenToRun, 0); te->command = cmdpath; - te->whenString = copystr(awhen); + te->whenString = strdup(awhen); return (struct bnode *)te; } diff --git a/src/bozo/fsbnodeops.c b/src/bozo/fsbnodeops.c index ce2f446..7e569a3 100644 --- a/src/bozo/fsbnodeops.c +++ b/src/bozo/fsbnodeops.c @@ -324,15 +324,6 @@ RestoreSalFlag(struct fsbnode *abnode) return 0; } -char * -copystr(char *a) -{ - char *b; - b = (char *)malloc(strlen(a) + 1); - strcpy(b, a); - return b; -} - static int fs_delete(struct bnode *bn) { diff --git a/src/bucoord/commands.c b/src/bucoord/commands.c index 46e8dd6..b4de994 100644 --- a/src/bucoord/commands.c +++ b/src/bucoord/commands.c @@ -729,18 +729,15 @@ bc_FloatATOI(char *anum) char * bc_CopyString(char *astring) { - afs_int32 tlen; char *tp; if (!astring) return (NULL); /* propagate null strings easily */ - tlen = strlen(astring); - tp = (char *)malloc(tlen + 1); /* don't forget the terminating null */ + tp = strdup(astring); if (!tp) { afs_com_err(whoami, BC_NOMEM, NULL); return (tp); } - strcpy(tp, astring); return tp; } @@ -1851,12 +1848,11 @@ bc_DumpCmd(struct cmd_syndesc *as, void *arock) * global variables so this can take place in main. */ if (loadfile) { - loadFile = (char *)malloc(strlen(as->parms[6].items->data) + 1); + loadFile = strdup(as->parms[6].items->data); if (!loadFile) { afs_com_err(whoami, BC_NOMEM, NULL); return BC_NOMEM; } - strcpy(loadFile, as->parms[6].items->data); return 0; } diff --git a/src/bucoord/config.c b/src/bucoord/config.c index f88ea29..e184c49 100644 --- a/src/bucoord/config.c +++ b/src/bucoord/config.c @@ -63,14 +63,12 @@ bc_InitConfig(char *apath) bc_globalConfig = tb; memset(tb, 0, sizeof(struct bc_config)); - tb->path = (char *)malloc(strlen(apath) + 1); + tb->path = strdup(apath); if (!tb->path) { free(tb); return (BC_NOMEM); } - strcpy(tb->path, apath); - /* now read the important config files; no file means empty list during system init */ return 0; @@ -99,8 +97,7 @@ HostAdd(struct bc_hostEntry **alist, char *aname, afs_int32 aport) /* tlast now points to the next pointer (or head pointer) we should overwrite */ tentry = (struct bc_hostEntry *)malloc(sizeof(struct bc_hostEntry)); memset(tentry, 0, sizeof(*tentry)); - tentry->name = (char *)malloc(strlen(aname) + 1); - strcpy(tentry->name, aname); + tentry->name = strdup(aname); *tlast = tentry; tentry->next = (struct bc_hostEntry *)0; tentry->addr.sin_family = AF_INET; diff --git a/src/bucoord/dsvs.c b/src/bucoord/dsvs.c index 0ec3714..08743a3 100644 --- a/src/bucoord/dsvs.c +++ b/src/bucoord/dsvs.c @@ -193,8 +193,7 @@ bc_CreateVolumeSet(struct bc_config *aconfig, char *avolName, nset = (struct bc_volumeSet *)malloc(sizeof(struct bc_volumeSet)); memset(nset, 0, sizeof(*nset)); nset->flags = aflags; - nset->name = (char *)malloc(strlen(avolName) + 1); - strcpy(nset->name, avolName); + nset->name = strdup(avolName); if (aflags & VSFLAG_TEMPORARY) { /* Add to beginning of list */ nset->next = aconfig->vset; @@ -306,12 +305,9 @@ bc_AddVolumeItem(struct bc_config *aconfig, char *avolName, char *ahost, for (tentry = *tlast; tentry; tlast = &tentry->next, tentry = *tlast); tentry = (struct bc_volumeEntry *)malloc(sizeof(struct bc_volumeEntry)); memset(tentry, 0, sizeof(*tentry)); - tentry->serverName = (char *)malloc(strlen(ahost) + 1); - strcpy(tentry->serverName, ahost); - tentry->partname = (char *)malloc(strlen(apart) + 1); - strcpy(tentry->partname, apart); - tentry->name = (char *)malloc(strlen(avol) + 1); - strcpy(tentry->name, avol); + tentry->serverName = strdup(ahost); + tentry->partname = strdup(apart); + tentry->name = strdup(avol); code = bc_ParseHost(tentry->serverName, &tentry->server); if (code) @@ -378,8 +374,7 @@ bc_CreateDumpSchedule(struct bc_config *aconfig, char *adumpName, aconfig->dsched = tdump; /* save the name of this dump node */ - tdump->name = (char *)malloc(strlen(adumpName) + 1); - strcpy(tdump->name, adumpName); + tdump->name = strdup(adumpName); /* expiration information */ tdump->expDate = expDate; diff --git a/src/bucoord/dump_sched.c b/src/bucoord/dump_sched.c index 62f9342..9af5713 100644 --- a/src/bucoord/dump_sched.c +++ b/src/bucoord/dump_sched.c @@ -429,8 +429,7 @@ bc_ParseDumpSchedule(void) memset(tds, 0, sizeof(*tds)); tds->next = (struct bc_dumpSchedule *)0; - tds->name = (char *)malloc(strlen(dsname) + 1); - strcpy(tds->name, dsname); + tds->name = strdup(dsname); tds->expDate = expDate; tds->expType = expType; diff --git a/src/bucoord/restore.c b/src/bucoord/restore.c index ee11a8d..d6acc8c 100644 --- a/src/bucoord/restore.c +++ b/src/bucoord/restore.c @@ -299,14 +299,13 @@ bc_Restorer(afs_int32 aindex) } memset(vi, 0, sizeof(struct volinfo)); - vi->volname = (char *)malloc(strlen(vname) + 1); + vi->volname = strdup(vname); if (!vi->volname) { free(vi); afs_com_err(whoami, BC_NOMEM, NULL); ERROR(BC_NOMEM); } - strcpy(vi->volname, vname); if (serverAll) { vi->server = serverAll; vi->partition = partitionAll; @@ -480,16 +479,13 @@ bc_Restorer(afs_int32 aindex) } memset(tle, 0, sizeof(struct bc_tapeList)); - tle->tapeName = - (char *)malloc(strlen(volumeEntries[ve].tape) - + 1); + tle->tapeName = strdup(volumeEntries[ve].tape); if (!tle->tapeName) { free(tle); afs_com_err(whoami, BC_NOMEM, NULL); return (BC_NOMEM); } - strcpy(tle->tapeName, volumeEntries[ve].tape); tle->dumpID = dlevels[lv].DumpId; tle->initialDumpID = dlevels[lv].initialDumpId; tle->tapeNumber = tapeseq; @@ -535,16 +531,13 @@ bc_Restorer(afs_int32 aindex) } memset(ti, 0, sizeof(struct bc_tapeItem)); - ti->volumeName = - (char *)malloc(strlen(volumeEntries[ve].name) - + 1); + ti->volumeName = strdup(volumeEntries[ve].name); if (!ti->volumeName) { free(ti); afs_com_err(whoami, BC_NOMEM, NULL); return (BC_NOMEM); } - strcpy(ti->volumeName, volumeEntries[ve].name); ti->server = vi->server; ti->partition = vi->partition; ti->oid = volumeEntries[ve].id; diff --git a/src/bucoord/tape_hosts.c b/src/bucoord/tape_hosts.c index 1da0bfb..3fd3b4b 100644 --- a/src/bucoord/tape_hosts.c +++ b/src/bucoord/tape_hosts.c @@ -266,8 +266,7 @@ bc_ParseHosts(void) tfirst = tlast = the; } the->next = (struct bc_hostEntry *)0; - the->name = (char *)malloc(strlen(hostName) + 1); - strcpy(the->name, hostName); + the->name = strdup(hostName); the->portOffset = port; if (th) { memcpy(&the->addr.sin_addr.s_addr, th->h_addr, 4); diff --git a/src/bucoord/vol_sets.c b/src/bucoord/vol_sets.c index fafdcf7..e4824c7 100644 --- a/src/bucoord/vol_sets.c +++ b/src/bucoord/vol_sets.c @@ -486,8 +486,7 @@ bc_ParseVolumeSet(void) */ tvs = (struct bc_volumeSet *)malloc(sizeof(struct bc_volumeSet)); memset(tvs, 0, sizeof(*tvs)); - tvs->name = (char *)malloc(strlen(vsname) + 1); - strcpy(tvs->name, vsname); + tvs->name = strdup(vsname); /* append to the end */ for (ppvs = &bc_globalConfig->vset, pvs = *ppvs; pvs; @@ -527,32 +526,29 @@ bc_ParseVolumeSet(void) /* The above code has filled in the server sockaddr, now fill in * the rest of the fields. */ - tve->serverName = (char *)malloc(strlen(serverName) + 1); + tve->serverName = strdup(serverName); if (!tve->serverName) { afs_com_err(whoami, 0, "Can't malloc() a new volume spec server name field!"); return (-1); } - strcpy(tve->serverName, serverName); - tve->partname = (char *)malloc(strlen(partName) + 1); + tve->partname = strdup(partName); if (!tve->partname) { afs_com_err(whoami, 0, "Can't malloc() a new volume spec partition pattern field!"); return (-1); } - strcpy(tve->partname, partName); code = bc_GetPartitionID(partName, &tve->partition); if (code) { afs_com_err(whoami, 0, "Can't parse partition '%s'", partName); return -1; } - tp = (char *)malloc(strlen(vsname) + 1); + tp = strdup(vsname); if (!tp) { afs_com_err(whoami, 0, "Can't malloc() a new volume spec volume pattern field!"); return (-1); } - strcpy(tp, vsname); tve->name = tp; /* Now, thread it onto the list of other volume spec entries for diff --git a/src/budb/server.c b/src/budb/server.c index 11cb757..8a9c92f 100644 --- a/src/budb/server.c +++ b/src/budb/server.c @@ -183,22 +183,17 @@ argHandler(struct cmd_syndesc *as, void *arock) /* database directory */ if (as->parms[0].items != 0) { - globalConfPtr->databaseDirectory = - (char *)malloc(strlen(as->parms[0].items->data) + 1); + globalConfPtr->databaseDirectory = strdup(as->parms[0].items->data); if (globalConfPtr->databaseDirectory == 0) BUDB_EXIT(-1); - strcpy(globalConfPtr->databaseDirectory, as->parms[0].items->data); } /* -cellservdb, cell configuration directory */ if (as->parms[1].items != 0) { - globalConfPtr->cellConfigdir = - (char *)malloc(strlen(as->parms[1].items->data) + 1); + globalConfPtr->cellConfigdir = strdup(as->parms[1].items->data); if (globalConfPtr->cellConfigdir == 0) BUDB_EXIT(-1); - strcpy(globalConfPtr->cellConfigdir, as->parms[1].items->data); - globalConfPtr->debugFlags |= DF_RECHECKNOAUTH; } diff --git a/src/butc/tcmain.c b/src/butc/tcmain.c index d58bf30..c082840 100644 --- a/src/butc/tcmain.c +++ b/src/butc/tcmain.c @@ -514,8 +514,7 @@ GetConfigParams(char *filename, afs_int32 port) continue; } - opencallout = (char *)malloc(strlen(value) + 1); - strcpy(opencallout, value); + opencallout = strdup(value); printf("Tape mount callout routine is %s\n", opencallout); } @@ -527,8 +526,7 @@ GetConfigParams(char *filename, afs_int32 port) continue; } - closecallout = (char *)malloc(strlen(value) + 1); - strcpy(closecallout, value); + closecallout = strdup(value); printf("Tape unmount callout routine is %s\n", closecallout); } @@ -661,8 +659,7 @@ GetConfigParams(char *filename, afs_int32 port) cmd); continue; } - xbsaObjectOwner = malloc(strlen(value) + 1); - strcpy(xbsaObjectOwner, value); + xbsaObjectOwner = strdup(value); printf("XBSA node is %s\n", xbsaObjectOwner); } @@ -673,8 +670,7 @@ GetConfigParams(char *filename, afs_int32 port) cmd); continue; } - adsmServerName = malloc(strlen(value) + 1); - strcpy(adsmServerName, value); + adsmServerName = strdup(value); printf("XBSA server is %s\n", adsmServerName); } @@ -692,8 +688,7 @@ GetConfigParams(char *filename, afs_int32 port) continue; } - xbsaSecToken = malloc(strlen(value) + 1); - strcpy(xbsaSecToken, value); + xbsaSecToken = strdup(value); printf("XBSA Password has been read\n"); } @@ -736,8 +731,7 @@ GetConfigParams(char *filename, afs_int32 port) cmd); continue; } - xbsalGName = malloc(strlen(value) + 1); - strcpy(xbsalGName, value); + xbsalGName = strdup(value); printf("XBSA management class is %s\n", xbsalGName); } #endif @@ -771,8 +765,7 @@ GetConfigParams(char *filename, afs_int32 port) } else if (!strcmp(cmd, "CENTRALLOG")) { - centralLogFile = malloc(strlen(value) + 1); - strcpy(centralLogFile, value); + centralLogFile = strdup(value); printf("Central log file is %s\n", centralLogFile); } @@ -903,9 +896,7 @@ WorkerBee(struct cmd_syndesc *as, void *arock) } if (as->parms[6].items) { /* -restoretofile */ - int s = strlen(as->parms[6].items->data); - restoretofile = malloc(s + 1); - strncpy(restoretofile, as->parms[6].items->data, s + 1); + restoretofile = strdup(as->parms[6].items->data); printf("Restore to file '%s'\n", restoretofile); } diff --git a/src/butc/tcprocs.c b/src/butc/tcprocs.c index f5299f9..44976c9 100644 --- a/src/butc/tcprocs.c +++ b/src/butc/tcprocs.c @@ -226,12 +226,8 @@ STC_PerformDump(struct rx_call *rxCallId, struct tc_dumpInterface *tcdiPtr, tc_d /*set up the parameters in the node, to be used by LWP */ strcpy(newNode->dumpSetName, tcdiPtr->dumpName); - newNode->dumpName = (char *)malloc(strlen(tcdiPtr->dumpPath) + 1); - strcpy(newNode->dumpName, tcdiPtr->dumpPath); - - newNode->volumeSetName = - (char *)malloc(strlen(tcdiPtr->volumeSetName) + 1); - strcpy(newNode->volumeSetName, tcdiPtr->volumeSetName); + newNode->dumpName = strdup(tcdiPtr->dumpPath); + newNode->volumeSetName = strdup(tcdiPtr->volumeSetName); CopyTapeSetDesc(&(newNode->tapeSetDesc), &tcdiPtr->tapeSet); diff --git a/src/cmd/cmd.c b/src/cmd/cmd.c index 89de6ca..903dc94 100644 --- a/src/cmd/cmd.c +++ b/src/cmd/cmd.c @@ -450,9 +450,8 @@ cmd_CreateSyntax(char *aname, /* copy in name, etc */ if (aname) { - td->name = malloc(strlen(aname) + 1); + td->name = strdup(aname); assert(td->name); - strcpy(td->name, aname); } else { td->name = NULL; noOpcodes = 1; @@ -462,9 +461,8 @@ cmd_CreateSyntax(char *aname, if (ahelp == (char *)CMD_HIDDEN) { td->flags |= CMD_HIDDEN; } else { - td->help = malloc(strlen(ahelp) + 1); + td->help = strdup(ahelp); assert(td->help); - strcpy(td->help, ahelp); } } else td->help = NULL; @@ -488,9 +486,8 @@ cmd_CreateAlias(struct cmd_syndesc *as, char *aname) td = malloc(sizeof(struct cmd_syndesc)); assert(td); memcpy(td, as, sizeof(struct cmd_syndesc)); - td->name = malloc(strlen(aname) + 1); + td->name = strdup(aname); assert(td->name); - strcpy(td->name, aname); td->flags |= CMD_ALIAS; /* if ever free things, make copy of help string, too */ @@ -543,16 +540,14 @@ cmd_AddParmAtOffset(struct cmd_syndesc *as, int ref, char *aname, int atype, return CMD_EXCESSPARMS; tp = &as->parms[ref]; - tp->name = malloc(strlen(aname) + 1); + tp->name = strdup(aname); assert(tp->name); - strcpy(tp->name, aname); tp->type = atype; tp->flags = aflags; tp->items = NULL; if (ahelp) { - tp->help = malloc(strlen(ahelp) + 1); + tp->help = strdup(ahelp); assert(tp->help); - strcpy(tp->help, ahelp); } else tp->help = NULL; @@ -607,9 +602,8 @@ AddItem(struct cmd_parmdesc *aparm, char *aval, char *pname) ti = calloc(1, sizeof(struct cmd_item)); assert(ti); - ti->data = malloc(strlen(aval) + 1); + ti->data = strdup(aval); assert(ti->data); - strcpy(ti->data, aval); /* now put ti at the *end* of the list */ if ((ni = aparm->items)) { for (; ni; ni = ni->next) @@ -716,13 +710,12 @@ InsertInitOpcode(int *aargc, char **aargv) } /* Create space for the initial opcode & fill it in */ - pinitopcode = malloc(sizeof(initcmd_opcode)); + pinitopcode = strdup(initcmd_opcode); if (!pinitopcode) { fprintf(stderr, "%s: Can't malloc initial opcode space\n", aargv[0]); free(newargv); return (NULL); } - strcpy(pinitopcode, initcmd_opcode); /* Move all the items in the old argv into the new argv, in their * proper places */ @@ -1144,9 +1137,8 @@ CopyBackArgs(struct cmd_token *alist, char **argv, count = 0; if (amaxn <= 1) return CMD_TOOMANY; - *argv = (char *)malloc(strlen(INITSTR) + 1); + *argv = strdup(INITSTR); assert(*argv); - strcpy(*argv, INITSTR); amaxn--; argv++; count++; @@ -1211,9 +1203,8 @@ cmd_ParseLine(char *aline, char **argv, afs_int32 * an, afs_int32 amaxn) ttok = malloc(sizeof(struct cmd_token)); assert(ttok); ttok->next = NULL; - ttok->key = malloc(strlen(tbuffer) + 1); + ttok->key = strdup(tbuffer); assert(ttok->key); - strcpy(ttok->key, tbuffer); if (last) { last->next = ttok; last = ttok; diff --git a/src/comerr/error_table.y b/src/comerr/error_table.y index cdb2702..5b4c770 100644 --- a/src/comerr/error_table.y +++ b/src/comerr/error_table.y @@ -49,7 +49,6 @@ char *gensym(const char *x); char *current_token = (char *)NULL; extern char *table_name; -char *ds(const char *string); char *quote(const char *string); void set_table_1num(char *string); int char_to_1num(char c); @@ -75,7 +74,7 @@ extern int yylex (void); %% error_table : ERROR_TABLE header error_codes END - { table_name = ds($2); + { table_name = strdup($2); current_token = table_name; put_ecs(); } ; @@ -85,7 +84,7 @@ header : table_fun table_id $$ = $2; } | table_id { current_token = $1; - set_table_fun(ds("1")); + set_table_fun(strdup("1")); $$ = $1; } ; @@ -120,12 +119,12 @@ ec_entry : ERROR_CODE_ENTRY ec_name ',' description ; ec_name : STRING - { $$ = ds($1); + { $$ = strdup($1); current_token = $$; } ; description : QUOTED_STRING - { $$ = ds($1); + { $$ = strdup($1); current_token = $$; } ; @@ -160,19 +159,10 @@ char *gensym(const char *x) } char * -ds(const char *string) -{ - char *rv; - rv = (char *)malloc(strlen(string)+1); - strcpy(rv, string); - return(rv); -} - -char * quote(const char *string) { char *rv; - rv = (char *)malloc(strlen(string)+3); + rv = malloc(strlen(string)+3); strcpy(rv, "\""); strcat(rv, string); strcat(rv, "\""); @@ -201,7 +191,7 @@ void add_ec(const char *name, const char *description) } error_codes = (char **)realloc((char *)error_codes, (current + 2)*sizeof(char *)); - error_codes[current++] = ds(name); + error_codes[current++] = strdup(name); error_codes[current] = (char *)NULL; } @@ -235,7 +225,7 @@ void add_ec_val(const char *name, const char *val, const char *description) } error_codes = (char **)realloc((char *)error_codes, (current + 2)*sizeof(char *)); - error_codes[current++] = ds(name); + error_codes[current++] = strdup(name); error_codes[current] = (char *)NULL; } diff --git a/src/comerr/error_table_nt.c b/src/comerr/error_table_nt.c index 06eb3e8..c5ea743 100755 --- a/src/comerr/error_table_nt.c +++ b/src/comerr/error_table_nt.c @@ -37,7 +37,6 @@ char *current_token = (char *)NULL; extern char *table_name; -char *ds(const char *string); char *quote(const char *string); void set_table_1num(char *string); int char_to_1num(char c); @@ -670,7 +669,7 @@ yyparse(YYPARSE_PARAM) case 1: #line 51 "error_table.y" { - table_name = ds(yyvsp[-2].dynstr); + table_name = strdup(yyvsp[-2].dynstr); current_token = table_name; put_ecs();; break; @@ -686,7 +685,7 @@ yyparse(YYPARSE_PARAM) #line 60 "error_table.y" { current_token = yyvsp[0].dynstr; - set_table_fun(ds("1")); + set_table_fun(strdup("1")); yyval.dynstr = yyvsp[0].dynstr; ; break; @@ -728,14 +727,14 @@ yyparse(YYPARSE_PARAM) case 10: #line 96 "error_table.y" { - yyval.dynstr = ds(yyvsp[0].dynstr); + yyval.dynstr = strdup(yyvsp[0].dynstr); current_token = yyval.dynstr;; break; } case 11: #line 101 "error_table.y" { - yyval.dynstr = ds(yyvsp[0].dynstr); + yyval.dynstr = strdup(yyvsp[0].dynstr); current_token = yyval.dynstr;; break; } @@ -976,15 +975,6 @@ gensym(const char *x) } char * -ds(const char *string) -{ - char *rv; - rv = (char *)malloc(strlen(string) + 1); - strcpy(rv, string); - return (rv); -} - -char * quote(const char *string) { char *rv; @@ -1018,7 +1008,7 @@ add_ec(const char *name, const char *description) } error_codes = (char **)realloc((char *)error_codes, (current + 2) * sizeof(char *)); - error_codes[current++] = ds(name); + error_codes[current++] = strdup(name); error_codes[current] = (char *)NULL; } @@ -1052,7 +1042,7 @@ add_ec_val(const char *name, const char *val, const char *description) } error_codes = (char **)realloc((char *)error_codes, (current + 2) * sizeof(char *)); - error_codes[current++] = ds(name); + error_codes[current++] = strdup(name); error_codes[current] = (char *)NULL; } diff --git a/src/comerr/et_lex.lex.l b/src/comerr/et_lex.lex.l index 1be8805..11aa37b 100644 --- a/src/comerr/et_lex.lex.l +++ b/src/comerr/et_lex.lex.l @@ -10,12 +10,12 @@ end return END; [\t\n\r ] ; -\"{PC}*\" { char *p; yylval.dynstr = ds((char *)yytext+1); +\"{PC}*\" { char *p; yylval.dynstr = strdup((char *)yytext+1); if ((p=strrchr(yylval.dynstr, '"'))) *p='\0'; return QUOTED_STRING; } -{AN}* { yylval.dynstr = ds((char *)yytext); return STRING; } +{AN}* { yylval.dynstr = strdup((char *)yytext); return STRING; } #.*\n ; diff --git a/src/comerr/et_lex.lex_nt.c b/src/comerr/et_lex.lex_nt.c index 91ddcae..910e91a 100644 --- a/src/comerr/et_lex.lex_nt.c +++ b/src/comerr/et_lex.lex_nt.c @@ -677,7 +677,7 @@ YY_RULE_SETUP case 7: YY_RULE_SETUP #line 13 "et_lex.lex.l" -{ char *p; yylval.dynstr = ds((char *)yytext+1); +{ char *p; yylval.dynstr = strdup((char *)yytext+1); if (p=strrchr(yylval.dynstr, '"')) *p='\0'; return QUOTED_STRING; } @@ -685,7 +685,7 @@ YY_RULE_SETUP case 8: YY_RULE_SETUP #line 18 "et_lex.lex.l" -{ yylval.dynstr = ds((char *)yytext); return STRING; } +{ yylval.dynstr = strdup((char *)yytext); return STRING; } YY_BREAK case 9: YY_RULE_SETUP diff --git a/src/config/mc.c b/src/config/mc.c index b457e7d..1233e57 100644 --- a/src/config/mc.c +++ b/src/config/mc.c @@ -66,13 +66,11 @@ ParseLine(char *aline, struct token **alist) ttok = (struct token *)malloc(sizeof(struct token)); ttok->next = NULL; if (dontUse) { - ttok->key = (char *)malloc(strlen(tbuffer)); - strcpy(ttok->key, tbuffer + 1); + ttok->key = strdup(tbuffer + 1); /* Skip first char */ ttok->flags = TOK_DONTUSE; dontUse = 0; } else { - ttok->key = (char *)malloc(strlen(tbuffer) + 1); - strcpy(ttok->key, tbuffer); + ttok->key = strdup(tbuffer); ttok->flags = 0; } if (last) { diff --git a/src/gtx/keymap.c b/src/gtx/keymap.c index db4a99d..6658136 100644 --- a/src/gtx/keymap.c +++ b/src/gtx/keymap.c @@ -29,14 +29,9 @@ keymap_Create(void) char * gtx_CopyString(char *aval) { - char *tp; - if (!aval) return NULL; /* propagate null strings around */ - tp = (char *)malloc(strlen(aval) + 1); - if (tp != NULL) - strcpy(tp, aval); - return (tp); + return strdup(aval); } static int diff --git a/src/kauth/admin_tools.c b/src/kauth/admin_tools.c index 98579ed..a4a4ce6 100644 --- a/src/kauth/admin_tools.c +++ b/src/kauth/admin_tools.c @@ -1523,9 +1523,8 @@ MyBeforeProc(struct cmd_syndesc *as, void *arock) return code; } ip = (struct cmd_item *)malloc(sizeof(struct cmd_item)); - ip->data = (char *)malloc(strlen(password) + 1); + ip->data = strdup(password); ip->next = 0; - strcpy(ip->data, password); as->parms[i].items = ip; } } diff --git a/src/libadmin/cfg/cfgclient.c b/src/libadmin/cfg/cfgclient.c index c9bb930..5f3b57c 100644 --- a/src/libadmin/cfg/cfgclient.c +++ b/src/libadmin/cfg/cfgclient.c @@ -194,13 +194,9 @@ cfg_ClientQueryStatus(const char *hostName, /* name of host */ if (tst == 0 && clientSt == 0) { /* everything looks good; malloc cell name buffer to return */ - clientCellName = - (char *)malloc(strlen(cellentry->cellInfo.name) + 1); - if (clientCellName == NULL) { + clientCellName = strdup(cellentry->cellInfo.name); + if (clientCellName == NULL) tst = ADMNOMEM; - } else { - strcpy(clientCellName, cellentry->cellInfo.name); - } } (void)afsconf_Close(confdir); diff --git a/src/libadmin/cfg/cfgdb.c b/src/libadmin/cfg/cfgdb.c index 95238d0..9f946b9 100644 --- a/src/libadmin/cfg/cfgdb.c +++ b/src/libadmin/cfg/cfgdb.c @@ -307,14 +307,12 @@ cfg_CellServDbEnumerate(const char *fsDbHost, /* fileserver or database host */ /* return cell name to caller */ if (tst == 0) { - *cellName = (char *)malloc(strlen(dbhostCell) + 1); + *cellName = strdup(dbhostCell); if (*cellName == NULL) { free(*cellDbHosts); - *cellDbHosts = NULL; + *cellDbHosts = NULL; tst = ADMNOMEM; - } else { - strcpy(*cellName, dbhostCell); } } } diff --git a/src/libadmin/cfg/cfghost.c b/src/libadmin/cfg/cfghost.c index 9fbdb33..bdf152e 100644 --- a/src/libadmin/cfg/cfghost.c +++ b/src/libadmin/cfg/cfghost.c @@ -177,13 +177,9 @@ cfg_HostQueryStatus(const char *hostName, /* name of host */ if (tst == 0 && serverSt == 0) { /* everything looks good; malloc cell name buffer to return */ - serverCellName = - (char *)malloc(strlen(cellentry->cellInfo.name) + 1); - if (serverCellName == NULL) { + serverCellName = strdup(cellentry->cellInfo.name); + if (serverCellName == NULL) tst = ADMNOMEM; - } else { - strcpy(serverCellName, cellentry->cellInfo.name); - } } (void)afsconf_Close(confdir); @@ -262,8 +258,7 @@ cfg_HostOpen(void *cellHandle, /* cell handle */ if ((cfg_host = (cfg_host_p) malloc(sizeof(cfg_host_t))) == NULL) { tst = ADMNOMEM; - } else if ((localHostName = (char *)malloc(strlen(fullHostName) + 1)) - == NULL) { + } else if ((localHostName = strdup(fullHostName)) == NULL) { free(cfg_host); tst = ADMNOMEM; } else { @@ -276,8 +271,6 @@ cfg_HostOpen(void *cellHandle, /* cell handle */ cfg_host->bosHandle = NULL; cfg_host->end_magic = END_MAGIC; - strcpy(localHostName, fullHostName); - if (!afsclient_CellNameGet (cfg_host->cellHandle, &cfg_host->cellName, &tst2)) { tst = tst2; diff --git a/src/libadmin/test/bos.c b/src/libadmin/test/bos.c index 22cf926..9d2681f 100644 --- a/src/libadmin/test/bos.c +++ b/src/libadmin/test/bos.c @@ -106,8 +106,7 @@ LocalParseLine(char *aline, struct token **alist) *tptr++ = 0; ttok = (struct token *)malloc(sizeof(struct token)); ttok->next = NULL; - ttok->key = (char *)malloc(strlen(tbuffer) + 1); - strcpy(ttok->key, tbuffer); + ttok->key = strdup(tbuffer); if (last) { last->next = ttok; last = ttok; diff --git a/src/rx/bulktest/bulk_client.c b/src/rx/bulktest/bulk_client.c index 87129d0..de7cc73 100644 --- a/src/rx/bulktest/bulk_client.c +++ b/src/rx/bulktest/bulk_client.c @@ -67,15 +67,6 @@ struct async_work { int async_nProcs = 0; -char * -allocString(s) - char *s; -{ - char *new = (char *)malloc(strlen(s) + 1); - strcpy(new, s); - return new; -} - void async_BulkProc(data) char *data; @@ -140,8 +131,8 @@ async_BulkTest(host, conn, store, count, verbose, file) name++; /* sprintf(tempfile, "/usr/tmp/%s.%s", myHostName, name);*/ sprintf(tempfile, "/usr/tmp/%s", name); - work->local = allocString(store ? file : tempfile); - work->remote = allocString(store ? tempfile : file); + work->local = strdup(store ? file : tempfile); + work->remote = strdup(store ? tempfile : file); async_nProcs += 1; LWP_CreateProcess(async_BulkProc, 3000, RX_PROCESS_PRIORITY, (void *)work, "bulk", &pid); diff --git a/src/tools/dumpscan/directory.c b/src/tools/dumpscan/directory.c index 2f2005c..f9ffac2 100644 --- a/src/tools/dumpscan/directory.c +++ b/src/tools/dumpscan/directory.c @@ -181,9 +181,8 @@ static afs_uint32 dirlookup_cb(afs_vnode *v, afs_dir_entry *de, } else if (s->vnode) { /* Search by vnode */ if (de->vnode != s->vnode[0]) return 0; /* Not it! */ if (s->name) { - s->name[0] = (char *)malloc(strlen(de->name) + 1); + s->name[0] = strdup(de->name); if (!s->name[0]) return ENOMEM; - strcpy(s->name[0], de->name); } if (s->vuniq) s->vuniq[0] = de->uniq; } diff --git a/src/tools/dumpscan/pathname.c b/src/tools/dumpscan/pathname.c index 0057df8..1afdb8e 100644 --- a/src/tools/dumpscan/pathname.c +++ b/src/tools/dumpscan/pathname.c @@ -279,14 +279,13 @@ Path_Build(XFILE * X, path_hashinfo * phi, afs_uint32 vnode, char **his_path, int nl, pl = 0; if (vnode == 1) { - *his_path = (char *)malloc(2); + *his_path = strdup("/"); if (!his_path) { if (phi->p->cb_error) (phi->p->cb_error) (ENOMEM, 1, phi->p->err_refcon, "No memory for pathname of vnode 1"); return ENOMEM; } - strcpy(*his_path, "/"); return 0; } diff --git a/src/tools/dumpscan/repair.c b/src/tools/dumpscan/repair.c index e4d80d5..96aa27c 100644 --- a/src/tools/dumpscan/repair.c +++ b/src/tools/dumpscan/repair.c @@ -67,10 +67,9 @@ repair_dumphdr_cb(afs_dump_header * hdr, XFILE * X, void *refcon) fprintf(stderr, ">>> Will use RESTORED.%d\n", hdr->volid); } sprintf(volname, "RESTORED.%d", hdr->volid); - hdr->volname = (unsigned char *)malloc(strlen(volname) + 1); + hdr->volname = (unsigned char *) strdup(volname); if (!hdr->volname) return ENOMEM; - strcpy((char *)hdr->volname, volname); hdr->field_mask |= F_DUMPHDR_VOLNAME; } if (!(field_mask & F_DUMPHDR_FROM)) { @@ -120,10 +119,9 @@ repair_volhdr_cb(afs_vol_header * hdr, XFILE * X, void *refcon) fprintf(stderr, ">>> Will use RESTORED.%d\n", hdr->volid); } sprintf(volname, "RESTORED.%d", hdr->volid); - hdr->volname = (unsigned char *)malloc(strlen(volname) + 1); + hdr->volname = (unsigned char *)strdup(volname); if (!hdr->volname) return ENOMEM; - strcpy((char *)hdr->volname, volname); hdr->field_mask |= F_VOLHDR_VOLNAME; } if (!(field_mask & F_VOLHDR_INSERV)) { diff --git a/src/tools/dumpscan/stagehdr.c b/src/tools/dumpscan/stagehdr.c index 4ef3c4b..7ddb49a 100644 --- a/src/tools/dumpscan/stagehdr.c +++ b/src/tools/dumpscan/stagehdr.c @@ -109,9 +109,9 @@ ParseStageHdr(XFILE * X, unsigned char *tag, backup_system_header * hdr) hdr->magic = ntohl(bckhdr->c_magic); hdr->cksum = ntohl(bckhdr->c_checksum); hdr->flags = ntohl(bckhdr->c_flags); - hdr->server = malloc(strlen(bckhdr->c_host) + 1); - hdr->part = malloc(strlen(bckhdr->c_disk) + 1); - hdr->volname = malloc(strlen(bckhdr->c_name) + 1); + hdr->server = (unsigned char *) strdup(bckhdr->c_host); + hdr->part = (unsigned char *) strdup(bckhdr->c_disk); + hdr->volname = (unsigned char *) strdup(bckhdr->c_name); if (!hdr->server || !hdr->part || !hdr->volname) { if (hdr->server) @@ -122,9 +122,6 @@ ParseStageHdr(XFILE * X, unsigned char *tag, backup_system_header * hdr) free(hdr->volname); return ENOMEM; } - strcpy((char *)hdr->server, bckhdr->c_host); - strcpy((char *)hdr->part, bckhdr->c_disk); - strcpy((char *)hdr->volname, bckhdr->c_name); } if (tag) diff --git a/src/ubik/ubik.c b/src/ubik/ubik.c index 1956bac..5895103 100644 --- a/src/ubik/ubik.c +++ b/src/ubik/ubik.c @@ -404,8 +404,7 @@ ubik_ServerInitCommon(afs_uint32 myHost, short myPort, initialize_U_error_table(); tdb = (struct ubik_dbase *)malloc(sizeof(struct ubik_dbase)); - tdb->pathName = (char *)malloc(strlen(pathName) + 1); - strcpy(tdb->pathName, pathName); + tdb->pathName = strdup(pathName); tdb->activeTrans = (struct ubik_trans *)0; memset(&tdb->version, 0, sizeof(struct ubik_version)); memset(&tdb->cachedVersion, 0, sizeof(struct ubik_version)); diff --git a/src/update/utils.c b/src/update/utils.c index cfa3991..ef2712f 100644 --- a/src/update/utils.c +++ b/src/update/utils.c @@ -30,8 +30,7 @@ AddToList(struct filestr **ah, char *aname) tf = (struct filestr *)malloc(sizeof(struct filestr)); tf->next = *ah; *ah = tf; - tf->name = (char *)malloc(strlen(aname) + 1); - strcpy(tf->name, aname); + tf->name = strdup(aname); return 0; } diff --git a/src/usd/usd_file.c b/src/usd/usd_file.c index 7c51c95..fb20661 100644 --- a/src/usd/usd_file.c +++ b/src/usd/usd_file.c @@ -333,8 +333,7 @@ usd_FileOpen(const char *path, int flags, int mode, usd_handle_t * usdP) usd->seek = usd_FileSeek; usd->ioctl = usd_FileIoctl; usd->close = usd_FileClose; - usd->fullPathName = (char *)malloc(strlen(path) + 1); - strcpy(usd->fullPathName, path); + usd->fullPathName = strdup(path); usd->openFlags = flags; code = 0; diff --git a/src/usd/usd_nt.c b/src/usd/usd_nt.c index 8574fb9..41786b5 100644 --- a/src/usd/usd_nt.c +++ b/src/usd/usd_nt.c @@ -479,8 +479,7 @@ usd_DeviceOpen(const char *path, int oflag, int pmode, usd_handle_t * usdP) usd->ioctl = usd_DeviceIoctl; usd->close = usd_DeviceClose; - usd->fullPathName = (char *)malloc(strlen(path) + 1); - strcpy(usd->fullPathName, path); + usd->fullPathName = strdup(path); usd->openFlags = oflag; /* For devices, this is the first real reference, so many errors show up diff --git a/src/uss/uss_procs.c b/src/uss/uss_procs.c index a5be915..ffaabf3 100644 --- a/src/uss/uss_procs.c +++ b/src/uss/uss_procs.c @@ -146,10 +146,8 @@ uss_procs_BuildDir(char *a_path, char *a_mode, char *a_owner, char *a_access) */ new_dir = (struct uss_subdir *)malloc(sizeof(struct uss_subdir)); new_dir->previous = uss_currentDir; - new_dir->path = (char *)malloc(strlen(a_path) + 1); - strcpy(new_dir->path, a_path); - new_dir->finalACL = (char *)malloc(strlen(a_access) + 1); - strcpy(new_dir->finalACL, a_access); + new_dir->path = strdup(a_path); + new_dir->finalACL = strdup(a_access); uss_currentDir = new_dir; /* diff --git a/src/uss/uss_vol.c b/src/uss/uss_vol.c index 3a5f3f7..41abffb 100644 --- a/src/uss/uss_vol.c +++ b/src/uss/uss_vol.c @@ -824,10 +824,8 @@ uss_vol_CreateVol(char *a_volname, char *a_server, char *a_partition, */ new_dir = (struct uss_subdir *)malloc(sizeof(struct uss_subdir)); new_dir->previous = uss_currentDir; - new_dir->path = (char *)malloc(strlen(a_mpoint) + 1); - strcpy(new_dir->path, a_mpoint); - new_dir->finalACL = (char *)malloc(strlen(a_acl) + 1); - strcpy(new_dir->finalACL, a_acl); + new_dir->path = strdup(a_mpoint); + new_dir->finalACL = strdup(a_acl); uss_currentDir = new_dir; sprintf(tmp_str, "%s %s all", a_mpoint, uss_AccountCreator); diff --git a/src/util/dirpath.c b/src/util/dirpath.c index a2162ba..5d09e82 100644 --- a/src/util/dirpath.c +++ b/src/util/dirpath.c @@ -556,12 +556,9 @@ ConstructLocalPath(const char *cpath, const char *relativeTo, status = EINVAL; } else { /* fully qualified path; just make a copy */ - newPath = (char *)malloc(strlen(cpath) + 1); - if (!newPath) { + newPath = strdup(cpath); + if (!newPath) status = ENOMEM; - } else { - (void)strcpy(newPath, cpath); - } } } else { @@ -645,12 +642,9 @@ ConstructLocalPath(const char *cpath, const char *relativeTo, LocalizePathHead(&cpath, &relativeTo); if (*cpath == '/') { - newPath = (char *)malloc(strlen(cpath) + 1); - if (!newPath) { + newPath = strdup(cpath); + if (!newPath) status = ENOMEM; - } else { - strcpy(newPath, cpath); - } } else { newPath = (char *)malloc(strlen(relativeTo) + 1 + strlen(cpath) + 1); if (!newPath) { diff --git a/src/util/ktime.c b/src/util/ktime.c index b331b19..20f9de8 100644 --- a/src/util/ktime.c +++ b/src/util/ktime.c @@ -79,8 +79,7 @@ LocalParseLine(char *aline, struct token **alist) *tptr++ = 0; ttok = malloc(sizeof(struct token)); ttok->next = NULL; - ttok->key = malloc(strlen(tbuffer) + 1); - strcpy(ttok->key, tbuffer); + ttok->key = strdup(tbuffer); if (last) { last->next = ttok; last = ttok; diff --git a/src/venus/fstrace.c b/src/venus/fstrace.c index a9c9d32..11c560b 100644 --- a/src/venus/fstrace.c +++ b/src/venus/fstrace.c @@ -678,8 +678,7 @@ icl_DumpKernel(FILE *outFilep, char *setname) memset(lip, 0, sizeof(*lip)); lip->nextp = allInfo; allInfo = lip; - lip->name = (char *)malloc(strlen(tname) + 1); - strcpy(lip->name, tname); + lip->name = strdup(tname); } i = found; } else { @@ -696,8 +695,7 @@ icl_DumpKernel(FILE *outFilep, char *setname) memset(lip, 0, sizeof(*lip)); lip->nextp = allInfo; allInfo = lip; - lip->name = (char *)malloc(strlen(tname) + 1); - strcpy(lip->name, tname); + lip->name = strdup(tname); } } diff --git a/src/vfsck/main.c b/src/vfsck/main.c index 5485da3..408f8f7 100644 --- a/src/vfsck/main.c +++ b/src/vfsck/main.c @@ -561,10 +561,8 @@ finddisk(name) if ((*dkp = (struct disk *)malloc(sizeof(struct disk))) == NULL) errexit("out of memory"); dk = *dkp; - if ((dk->name = malloc((unsigned int)len + 1)) == NULL) + if ((dk->name = strdup(name)) == NULL) errexit("out of memory"); - strncpy(dk->name, name, len); - dk->name[len] = '\0'; dk->part = NULL; dk->next = NULL; dk->pid = 0; @@ -586,12 +584,10 @@ addpart(name, fsname) if ((*ppt = (struct part *)malloc(sizeof(struct part))) == NULL) errexit("out of memory"); pt = *ppt; - if ((pt->name = malloc((unsigned int)strlen(name) + 1)) == NULL) + if ((pt->name = strdup(name)) == NULL) errexit("out of memory"); - strcpy(pt->name, name); - if ((pt->fsname = malloc((unsigned int)strlen(fsname) + 1)) == NULL) + if ((pt->fsname = strdup(fsname)) == NULL) errexit("out of memory"); - strcpy(pt->fsname, fsname); pt->next = NULL; } diff --git a/src/viced/afsfileprocs.c b/src/viced/afsfileprocs.c index 69aff57..4aeae1a 100644 --- a/src/viced/afsfileprocs.c +++ b/src/viced/afsfileprocs.c @@ -1996,7 +1996,6 @@ static afs_int32 RXGetVolumeStatus(AFSFetchVolumeStatus * status, char **name, char **offMsg, char **motd, Volume * volptr) { - int temp; status->Vid = V_id(volptr); status->ParentId = V_parentId(volptr); @@ -2015,18 +2014,14 @@ RXGetVolumeStatus(AFSFetchVolumeStatus * status, char **name, char **offMsg, status->PartMaxBlocks = RoundInt64ToInt31(volptr->partition->totalUsable); /* now allocate and copy these things; they're freed by the RXGEN stub */ - temp = strlen(V_name(volptr)) + 1; - *name = malloc(temp); + *name = strdup(V_name(volptr)); if (!*name) { ViceLogThenPanic(0, ("Failed malloc in RXGetVolumeStatus\n")); } - strcpy(*name, V_name(volptr)); - temp = strlen(V_offlineMessage(volptr)) + 1; - *offMsg = malloc(temp); + *offMsg = strdup(V_offlineMessage(volptr)); if (!*offMsg) { ViceLogThenPanic(0, ("Failed malloc in RXGetVolumeStatus\n")); } - strcpy(*offMsg, V_offlineMessage(volptr)); *motd = malloc(1); if (!*motd) { ViceLogThenPanic(0, ("Failed malloc in RXGetVolumeStatus\n")); diff --git a/src/vol/partition.c b/src/vol/partition.c index 0d2a5fc..6d0d122 100644 --- a/src/vol/partition.c +++ b/src/vol/partition.c @@ -201,8 +201,7 @@ VInitPartition_r(char *path, char *devname, Device dev) else DiskPartitionList = dp; dp->next = 0; - dp->name = (char *)malloc(strlen(path) + 1); - strncpy(dp->name, path, strlen(path) + 1); + dp->name = strdup(path); dp->index = volutil_GetPartitionID(path); #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV) /* Create a lockfile for the partition, of the form /vicepa/Lock/vicepa */ @@ -215,8 +214,7 @@ VInitPartition_r(char *path, char *devname, Device dev) close(afs_open(dp->devName, O_RDWR | O_CREAT, 0600)); dp->device = dp->index; #else - dp->devName = (char *)malloc(strlen(devname) + 1); - strncpy(dp->devName, devname, strlen(devname) + 1); + dp->devName = strdup(devname); dp->device = dev; #endif dp->lock_fd = INVALID_FD; diff --git a/src/vol/test/listVicepx.c b/src/vol/test/listVicepx.c index b834339..1044a30 100644 --- a/src/vol/test/listVicepx.c +++ b/src/vol/test/listVicepx.c @@ -343,10 +343,8 @@ createDirEnt(dirEntry, fileName, vnode, unique) dirEntry->numEntries * sizeof(VnodeName))); dirEntry->vnodeName[dirEntry->numEntries - 1].vnode = vnode; dirEntry->vnodeName[dirEntry->numEntries - 1].vunique = unique; - dirEntry->vnodeName[dirEntry->numEntries - 1].name = - (char *)malloc(strlen(fileName) + 1); + dirEntry->vnodeName[dirEntry->numEntries - 1].name = strdup(fileName); assert(dirEntry->vnodeName[dirEntry->numEntries - 1].name); - strcpy(dirEntry->vnodeName[dirEntry->numEntries - 1].name, fileName); } diff --git a/src/vol/test/utilities.c b/src/vol/test/utilities.c index 21858ec..6b0b869 100644 --- a/src/vol/test/utilities.c +++ b/src/vol/test/utilities.c @@ -105,8 +105,7 @@ pushStack(name) char *name; { assert(stackSize < MAX_STACK_SIZE); - assert(stack[stackSize] = (char *)malloc(strlen(name) + 1)); - strcpy(stack[stackSize], name); + assert(stack[stackSize] = strdup(name)); stackSize++; } diff --git a/src/vol/vol-salvage.c b/src/vol/vol-salvage.c index 5a4dbfa..7146692 100644 --- a/src/vol/vol-salvage.c +++ b/src/vol/vol-salvage.c @@ -3218,10 +3218,7 @@ JudgeEntry(void *arock, char *name, afs_int32 vnodeNumber, Log("FOUND root file: %s" OS_DIRSEP "%s (%u.%u %05o) author %u (vnode %u dir %u)\n", dir->name ? dir->name : "??", name, vnodeEssence->owner, vnodeEssence->group, vnodeEssence->modeBits, vnodeEssence->author, vnodeNumber, dir->vnodeNumber); if (vnodeIdToClass(vnodeNumber) == vLarge && vnodeEssence->name == NULL) { - char *n; - if ((n = (char *)malloc(strlen(name) + 1))) - strcpy(n, name); - vnodeEssence->name = n; + vnodeEssence->name = strdup(name); } /* The directory entry points to the vnode. Check to see if the @@ -4855,9 +4852,8 @@ char * ToString(const char *s) { char *p; - p = (char *)malloc(strlen(s) + 1); + p = strdup(s); osi_Assert(p != NULL); - strcpy(p, s); return p; } -- 1.9.4