From: Greg Hudson Date: Mon, 20 Oct 2008 21:14:47 +0000 (+0000) Subject: Use asprintf instead of malloc/strcpy/strcat in many places X-Git-Tag: krb5-1.7-alpha1~282 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=ae423f53214830de1367627180031283de998746;p=krb5.git Use asprintf instead of malloc/strcpy/strcat in many places ticket: 6200 status: open git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@20901 dc483132-0cff-0310-8789-dd5450dbe970 --- diff --git a/src/appl/bsd/kcmd.c b/src/appl/bsd/kcmd.c index 604bcaf20..7d22d5905 100644 --- a/src/appl/bsd/kcmd.c +++ b/src/appl/bsd/kcmd.c @@ -424,13 +424,10 @@ kcmd(sock, ahost, rport, locuser, remuser, cmd, fd2p, service, realm, enum kcmd_proto protonum = *protonump; int addrfamily = /* AF_INET */0; - if ((cksumbuf = malloc(strlen(cmd)+strlen(remuser)+64)) == 0 ) { + if (asprintf(&cksumbuf, "%u:%s%s", ntohs(rport), cmd, remuser) < 0) { fprintf(stderr, "Unable to allocate memory for checksum buffer.\n"); return(-1); } - sprintf(cksumbuf, "%u:", ntohs(rport)); - strcat(cksumbuf, cmd); - strcat(cksumbuf, remuser); cksumdat.data = cksumbuf; cksumdat.length = strlen(cksumbuf); diff --git a/src/appl/bsd/krlogind.c b/src/appl/bsd/krlogind.c index cd362a4aa..2fe4c0410 100644 --- a/src/appl/bsd/krlogind.c +++ b/src/appl/bsd/krlogind.c @@ -1478,16 +1478,12 @@ recvauth(valid_checksum) if (authenticator->checksum) { struct sockaddr_in adr; socklen_t adr_length = sizeof(adr); - char * chksumbuf = (char *) malloc(strlen(term)+strlen(lusername)+32); + char * chksumbuf; if (getsockname(netf, (struct sockaddr *) &adr, &adr_length) != 0) goto error_cleanup; - if (chksumbuf == 0) + if (asprintf(&chksumbuf, "%u:%s%s", ntohs(adr.sin_port), term, lusername) < 0) goto error_cleanup; - sprintf(chksumbuf,"%u:", ntohs(adr.sin_port)); - strcat(chksumbuf,term); - strcat(chksumbuf,lusername); - status = krb5_verify_checksum(bsd_context, authenticator->checksum->checksum_type, authenticator->checksum, diff --git a/src/appl/gssftp/ftp/glob.c b/src/appl/gssftp/ftp/glob.c index 272e50305..6134798ef 100644 --- a/src/appl/gssftp/ftp/glob.c +++ b/src/appl/gssftp/ftp/glob.c @@ -57,6 +57,8 @@ static char sccsid[] = "@(#)glob.c 5.9 (Berkeley) 2/25/91"; #include #endif +#include + #include "ftp_var.h" #ifdef ARG_MAX @@ -258,10 +260,7 @@ matchdir(pattern) char *base = *gpath ? gpath : "."; char *buffer = 0; - buffer = malloc(strlen(base) + strlen("\\*") + 1); - if (!buffer) return; - strcpy(buffer, base); - strcat(buffer, "\\*"); + if (asprintf(&buffer, "%s\\*", base) < 0) return; hFile = FindFirstFile(buffer, &file_data); if (hFile == INVALID_HANDLE_VALUE) { if (!globbed) @@ -732,12 +731,10 @@ char * strspl(cp, dp) register char *cp, *dp; { - register char *ep = malloc((unsigned)(strlen(cp) + strlen(dp) + 1)); + char *ep; - if (ep == (char *)0) + if (asprintf(&ep, "%s%s", cp, dp) < 0) fatal("Out of memory"); - (void) strcpy(ep, cp); - (void) strcat(ep, dp); return (ep); } diff --git a/src/clients/kpasswd/ksetpwd.c b/src/clients/kpasswd/ksetpwd.c index 41500dd03..45f782f08 100644 --- a/src/clients/kpasswd/ksetpwd.c +++ b/src/clients/kpasswd/ksetpwd.c @@ -3,6 +3,7 @@ #include #include #include +#include #define TKTTIMELEFT 60*10 /* ten minutes */ @@ -69,14 +70,11 @@ static kbrccache_t userinitcontext( krb5_unparse_name( kcontext, kme, &pName ); if( cachename ) { - pCacheName = malloc( strlen( pName ) + strlen( cachename ) + 1 ); - if( pCacheName == NULL ) + if (asprintf(&pCacheName, "%s%s", cachename, pName) < 0) { kres = KRB5_CC_NOMEM; goto fail; } - strcpy( pCacheName, cachename ); - strcat( pCacheName, pName ); kres = krb5_cc_resolve( kcontext, pCacheName, &kcache ); if( kres ) { diff --git a/src/kadmin/cli/kadmin.c b/src/kadmin/cli/kadmin.c index b3f2b3f21..e5a336aa0 100644 --- a/src/kadmin/cli/kadmin.c +++ b/src/kadmin/cli/kadmin.c @@ -279,14 +279,9 @@ char *kadmin_startup(argc, argv) break; case 'd': /* now db_name is not a seperate argument. It has to be passed as part of the db_args */ - if (!db_name) { - db_name = malloc(strlen(optarg) + sizeof("dbname=")); - } else { - db_name = realloc(db_name, strlen(optarg) + sizeof("dbname=")); - } - - strcpy(db_name, "dbname="); - strcat(db_name, optarg); + if (db_name) + free(db_name); + asprintf(&db_name, "dbname=%s", optarg); db_args_size++; { @@ -437,43 +432,27 @@ char *kadmin_startup(argc, argv) } if (cp != NULL) *cp = '\0'; - princstr = (char*)malloc(strlen(canon) + 6 /* "/admin" */ + - (realm ? 1 + strlen(realm) : 0) + 1); - if (princstr == NULL) { + if (asprintf(&princstr, "%s/admin%s%s", canon, + (realm) ? "@" : "", + (realm) ? realm : "") < 0) { fprintf(stderr, "%s: out of memory\n", whoami); exit(1); } - strcpy(princstr, canon); - strcat(princstr, "/admin"); - if (realm) { - strcat(princstr, "@"); - strcat(princstr, realm); - } free(canon); krb5_free_principal(context, princ); freeprinc++; } else if ((luser = getenv("USER"))) { - princstr = (char *) malloc(strlen(luser) + 7 /* "/admin@" */ - + strlen(def_realm) + 1); - if (princstr == NULL) { + if (asprintf(&princstr, "%s/admin@%s", luser, def_realm) < 0) { fprintf(stderr, "%s: out of memory\n", whoami); exit(1); } - strcpy(princstr, luser); - strcat(princstr, "/admin"); - strcat(princstr, "@"); - strcat(princstr, def_realm); freeprinc++; } else if ((pw = getpwuid(getuid()))) { - princstr = (char *) malloc(strlen(pw->pw_name) + 7 /* "/admin@" */ - + strlen(def_realm) + 1); - if (princstr == NULL) { + if (asprintf(&princstr, "%s/admin@%s", pw->pw_name, + def_realm) < 0) { fprintf(stderr, "%s: out of memory\n", whoami); exit(1); } - strcpy(princstr, pw->pw_name); - strcat(princstr, "/admin@"); - strcat(princstr, def_realm); freeprinc++; } else { fprintf(stderr, "%s: unable to figure out a principal name\n", diff --git a/src/kadmin/dbutil/dump.c b/src/kadmin/dbutil/dump.c index fbb8fd21e..7c7bf3203 100644 --- a/src/kadmin/dbutil/dump.c +++ b/src/kadmin/dbutil/dump.c @@ -330,15 +330,12 @@ void update_ok_file (file_name) int fd; static char ok[]=".dump_ok"; - if ((file_ok = (char *)malloc(strlen(file_name) + strlen(ok) + 1)) - == NULL) { + if (asprintf(&file_ok, "%s%s", file_name, ok) < 0) { com_err(progname, ENOMEM, "while allocating filename for update_ok_file"); exit_status++; return; } - strcpy(file_ok, file_name); - strcat(file_ok, ok); if ((fd = open(file_ok, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0) { com_err(progname, errno, "while creating 'ok' file, '%s'", file_ok); @@ -2282,14 +2279,11 @@ load_db(argc, argv) } dumpfile = argv[aindex]; - if (!(dbname_tmp = (char *) malloc(strlen(dbname)+ - strlen(dump_tmptrail)+1))) { + if (asprintf(&dbname_tmp, "%s%s", dbname, dump_tmptrail) < 0) { fprintf(stderr, no_name_mem_fmt, progname); exit_status++; return; } - strcpy(dbname_tmp, dbname); - strcat(dbname_tmp, dump_tmptrail); /* * Initialize the Kerberos context and error tables. diff --git a/src/kadmin/dbutil/kdb5_util.c b/src/kadmin/dbutil/kdb5_util.c index ff6bcc995..a61469a17 100644 --- a/src/kadmin/dbutil/kdb5_util.c +++ b/src/kadmin/dbutil/kdb5_util.c @@ -215,16 +215,12 @@ int main(argc, argv) global_params.dbname = koptarg; global_params.mask |= KADM5_CONFIG_DBNAME; - db_name_tmp = malloc( strlen(global_params.dbname) + sizeof("dbname=")); - if( db_name_tmp == NULL ) + if (asprintf(&db_name_tmp, "dbname=%s", global_params.dbname) < 0) { com_err(progname, ENOMEM, "while parsing command arguments"); exit(1); } - strcpy( db_name_tmp, "dbname="); - strcat( db_name_tmp, global_params.dbname ); - if (!add_db_arg(db_name_tmp)) { com_err(progname, ENOMEM, "while parsing command arguments\n"); exit(1); diff --git a/src/kadmin/dbutil/loadv4.c b/src/kadmin/dbutil/loadv4.c index 4c3591ea0..91b31fe0c 100644 --- a/src/kadmin/dbutil/loadv4.c +++ b/src/kadmin/dbutil/loadv4.c @@ -267,16 +267,11 @@ load_v4db(argc, argv) } tempdbname = dbname; } else { - size_t dbnamelen = strlen(dbname); - tempdbname = malloc(dbnamelen + 2); - if (tempdbname == 0) { + if (asprintf(&tempdbname, "%s~", dbname) < 0) com_err(PROGNAME, ENOMEM, "allocating temporary filename"); krb5_free_context(context); return; } - strcpy(tempdbname, dbname); - tempdbname[dbnamelen] = '~'; - tempdbname[dbnamelen+1] = 0; (void) krb5_db_destroy(context, tempdbname); } diff --git a/src/lib/kdb/kdb5.c b/src/lib/kdb/kdb5.c index 67ceacb99..eb68f22fd 100644 --- a/src/lib/kdb/kdb5.c +++ b/src/lib/kdb/kdb5.c @@ -1678,23 +1678,14 @@ krb5_db_setup_mkey_name(krb5_context context, char **fullname, krb5_principal * principal) { krb5_error_code retval; - size_t keylen; - size_t rlen = strlen(realm); char *fname; if (!keyname) keyname = KRB5_KDB_M_NAME; /* XXX external? */ - keylen = strlen(keyname); - - fname = malloc(keylen + rlen + strlen(REALM_SEP_STRING) + 1); - if (!fname) + if (asprintf(&fname, "%s%s%s", keyname, REALM_SEP_STRING, realm) < 0) return ENOMEM; - strcpy(fname, keyname); - strcat(fname, REALM_SEP_STRING); - strcat(fname, realm); - if ((retval = krb5_parse_name(context, fname, principal))) return retval; if (fullname) diff --git a/src/lib/krb5/rcache/rc_io.c b/src/lib/krb5/rcache/rc_io.c index 0309ade5f..1043c30ed 100644 --- a/src/lib/krb5/rcache/rc_io.c +++ b/src/lib/krb5/rcache/rc_io.c @@ -169,11 +169,8 @@ krb5_rc_io_creat(krb5_context context, krb5_rc_iostuff *d, char **fn) GETDIR; if (fn && *fn) { - if (!(d->fn = malloc(strlen(*fn) + dirlen + 1))) + if (asprintf(&d->fn, "%s%s%s", dir, PATH_SEPARATOR, *fn) < 0) return KRB5_RC_IO_MALLOC; - (void) strcpy(d->fn, dir); - (void) strcat(d->fn, PATH_SEPARATOR); - (void) strcat(d->fn, *fn); unlink(d->fn); d->fd = THREEPARAMOPEN(d->fn, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL | O_BINARY, 0600); @@ -236,11 +233,8 @@ krb5_rc_io_open_internal(krb5_context context, krb5_rc_iostuff *d, char *fn, if (!(d->fn = strdup(full_pathname))) return KRB5_RC_IO_MALLOC; } else { - if (!(d->fn = malloc(strlen(fn) + dirlen + 1))) + if (asprintf(&d->fn, "%s%s%s", dir, PATH_SEPARATOR, fn) < 0) return KRB5_RC_IO_MALLOC; - (void) strcpy(d->fn, dir); - (void) strcat(d->fn, PATH_SEPARATOR); - (void) strcat(d->fn, fn); } #ifdef NO_USERID diff --git a/src/plugins/kdb/db2/kdb_db2.c b/src/plugins/kdb/db2/kdb_db2.c index 0aa3c4fd8..1e67ea034 100644 --- a/src/plugins/kdb/db2/kdb_db2.c +++ b/src/plugins/kdb/db2/kdb_db2.c @@ -219,11 +219,8 @@ gen_dbsuffix(char *db_name, char *sfx) if (sfx == NULL) return ((char *) NULL); - dbsuffix = malloc(strlen(db_name) + strlen(sfx) + 1); - if (!dbsuffix) + if (asprintf(&dbsuffix, "%s%s", db_name, sfx) < 0) return (0); - (void) strcpy(dbsuffix, db_name); - (void) strcat(dbsuffix, sfx); return dbsuffix; } diff --git a/src/slave/kprop.c b/src/slave/kprop.c index f47b2b74d..a2542363e 100644 --- a/src/slave/kprop.c +++ b/src/slave/kprop.c @@ -492,13 +492,10 @@ open_database(context, data_fn, size) data_fn); exit(1); } - if ((data_ok_fn = (char *) malloc(strlen(data_fn)+strlen(ok)+1)) - == NULL) { + if (asprintf(&data_ok_fn, "%s%s", data_fn, ok) < 0) { com_err(progname, ENOMEM, "while trying to malloc data_ok_fn"); exit(1); } - strcpy(data_ok_fn, data_fn); - strcat(data_ok_fn, ok); if (stat(data_ok_fn, &stbuf_ok)) { com_err(progname, errno, "while trying to stat %s", data_ok_fn); @@ -730,17 +727,12 @@ void update_last_prop_file(hostname, file_name) int fd; static char last_prop[]=".last_prop"; - if ((file_last_prop = (char *)malloc(strlen(file_name) + - strlen(hostname) + 1 + - strlen(last_prop) + 1)) == NULL) { + if (asprintf(&file_last_prop, "%s.%s%s", file_name, hostname, + last_prop) < 0) { com_err(progname, ENOMEM, "while allocating filename for update_last_prop_file"); return; } - strcpy(file_last_prop, file_name); - strcat(file_last_prop, "."); - strcat(file_last_prop, hostname); - strcat(file_last_prop, last_prop); if ((fd = THREEPARAMOPEN(file_last_prop, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0) { com_err(progname, errno, "while creating 'last_prop' file, '%s'", diff --git a/src/slave/kpropd.c b/src/slave/kpropd.c index 959dd4ab5..123b2fe66 100644 --- a/src/slave/kpropd.c +++ b/src/slave/kpropd.c @@ -1082,14 +1082,11 @@ void PRS(argv) /* * Construct the name of the temporary file. */ - if ((temp_file_name = (char *) malloc(strlen(file) + - strlen(tmp) + 1)) == NULL) { + if (asprintf(&temp_file_name, "%s%s", file, tmp) < 0) { com_err(progname, ENOMEM, "while allocating filename for temp file"); exit(1); } - strcpy(temp_file_name, file); - strcat(temp_file_name, tmp); retval = kadm5_get_config_params(kpropd_context, 1, ¶ms, ¶ms); if (retval) { diff --git a/src/tests/shlib/t_loader.c b/src/tests/shlib/t_loader.c index 70cd6d13b..cdc255055 100644 --- a/src/tests/shlib/t_loader.c +++ b/src/tests/shlib/t_loader.c @@ -5,6 +5,7 @@ #include "autoconf.h" #include "krb5.h" #include "gssapi/gssapi.h" +#include "k5-platform.h" #define HAVE_DLOPEN 1 static int verbose = 1; @@ -53,28 +54,20 @@ static void *do_open_1(const char *libname, const char *rev, { void *p; char *namebuf; - size_t sz; + int r; if (verbose) printf("from line %d: do_open(%s)...%*s", line, libname, HORIZ-strlen(libname), ""); - sz = strlen(SHLIB_SUFFIX) + strlen(libname) + 4; #ifdef _AIX - sz += strlen(rev) + 8; + r = asprintf(&namebuf, "lib%s%s", libname, SHLIB_SUFFIX); +#else + r = asprintf(&namebuf, "lib%s%s(shr.o.%s)", libname, SHLIB_SUFFIX, rev); #endif - namebuf = malloc(sz); - if (namebuf == 0) { - perror("malloc"); + if (r < 0) { + perror("asprintf"); exit(1); } - strcpy(namebuf, "lib"); - strcat(namebuf, libname); - strcat(namebuf, SHLIB_SUFFIX); -#ifdef _AIX - strcat(namebuf, "(shr.o."); - strcat(namebuf, rev); - strcat(namebuf, ")"); -#endif #ifndef RTLD_MEMBER #define RTLD_MEMBER 0 diff --git a/src/util/et/error_table.y b/src/util/et/error_table.y index 6162d61b5..750e9d973 100644 --- a/src/util/et/error_table.y +++ b/src/util/et/error_table.y @@ -108,10 +108,7 @@ quote(string) char const *string; { char *rv; - rv = malloc(strlen(string)+3); - strcpy(rv, "\""); - strcat(rv, string); - strcat(rv, "\""); + asprintf(&rv, "\"%s\"", string); return(rv); } diff --git a/src/util/et/internal.h b/src/util/et/internal.h index 57b5cd58e..f3e3abef3 100644 --- a/src/util/et/internal.h +++ b/src/util/et/internal.h @@ -5,6 +5,8 @@ #include +#include "k5-platform.h" + #ifndef SYS_ERRLIST_DECLARED extern char const * const sys_errlist[]; extern const int sys_nerr; diff --git a/src/util/profile/prof_file.c b/src/util/profile/prof_file.c index 13d8860e8..4851788e6 100644 --- a/src/util/profile/prof_file.c +++ b/src/util/profile/prof_file.c @@ -198,7 +198,6 @@ errcode_t profile_open_file(const_profile_filespec_t filespec, prf_file_t prf; errcode_t retval; char *home_env = 0; - unsigned int len; prf_data_t data; char *expanded_filename; @@ -214,7 +213,6 @@ errcode_t profile_open_file(const_profile_filespec_t filespec, memset(prf, 0, sizeof(struct _prf_file_t)); prf->magic = PROF_MAGIC_FILE; - len = strlen(filespec)+1; if (filespec[0] == '~' && filespec[1] == '/') { home_env = getenv("HOME"); #ifdef HAVE_PWD_H @@ -229,19 +227,17 @@ errcode_t profile_open_file(const_profile_filespec_t filespec, home_env = pw->pw_dir; } #endif - if (home_env) - len += strlen(home_env); } - expanded_filename = malloc(len); + if (home_env) { + if (asprintf(&expanded_filename, "%s%s", home_env, + filespec + 1) < 0) + expanded_filename = 0; + } else + expanded_filename = strdup(filespec); if (expanded_filename == 0) { free(prf); return ENOMEM; } - if (home_env) { - strcpy(expanded_filename, home_env); - strcat(expanded_filename, filespec+1); - } else - memcpy(expanded_filename, filespec, len); retval = k5_mutex_lock(&g_shared_trees_mutex); if (retval) { diff --git a/src/util/ss/utils.c b/src/util/ss/utils.c index c57800157..7f4e9214a 100644 --- a/src/util/ss/utils.c +++ b/src/util/ss/utils.c @@ -56,26 +56,11 @@ char * generate_rqte(func_name, info_string, cmds, options) char const *cmds; int options; { - int size; - char *string, *var_name, numbuf[16]; + char *string, *var_name; var_name = generate_cmds_string(cmds); generate_function_definition(func_name); - size = 6; /* " { " */ - size += strlen(var_name)+8; /* "quux, " */ - size += strlen(func_name)+8; /* "foo, " */ - size += strlen(info_string)+8; /* "\"Info!\", " */ - sprintf(numbuf, "%d", options); - size += strlen(numbuf)+5; /* " }," + NL + NUL */ - string = malloc(size); - strcpy(string, " { "); - strcat(string, var_name); - strcat(string, ",\n "); - strcat(string, func_name); - strcat(string, ",\n "); - strcat(string, info_string); - strcat(string, ",\n "); - strcat(string, numbuf); - strcat(string, " },\n"); + asprintf(&string, " { %s,\n %s,\n %s,\n %d },\n", + var_name, func_name, info_string, options); return(string); } @@ -96,14 +81,8 @@ char *str_concat3(a, b, c) register char *a, *b, *c; { char *result; - int size_a = strlen(a); - int size_b = strlen(b); - int size_c = strlen(c); - result = malloc((size_a + size_b + size_c + 2)*sizeof(char)); - strcpy(result, a); - strcpy(&result[size_a], c); - strcpy(&result[size_a+size_c], b); + asprintf(&result, "%s%s%s", a, c, b); return(result); } @@ -112,13 +91,8 @@ char *quote(string) register char *string; { register char *result; - int len; - len = strlen(string)+1; - result = malloc(len+2); - result[0] = '"'; - strncpy(&result[1], string, len-1); - result[len] = '"'; - result[len+1] = '\0'; + + asprintf(&result, "\"%s\"", string); return(result); }