From: Greg Hudson Date: Mon, 20 Oct 2008 18:18:49 +0000 (+0000) Subject: Use strdup in place of malloc/strcpy in many places X-Git-Tag: krb5-1.7-alpha1~285 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=e4a2f122c3086c1179323c0e24edb3506d3f4758;p=krb5.git Use strdup in place of malloc/strcpy in many places ticket: 6200 status: open git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@20898 dc483132-0cff-0310-8789-dd5450dbe970 --- diff --git a/src/appl/bsd/kcmd.c b/src/appl/bsd/kcmd.c index 64da72f70..604bcaf20 100644 --- a/src/appl/bsd/kcmd.c +++ b/src/appl/bsd/kcmd.c @@ -1320,11 +1320,10 @@ strsave(sp) { register char *ret; - if((ret = (char *) malloc((unsigned) strlen(sp)+1)) == NULL) { + if((ret = strdup(sp)) == NULL) { fprintf(stderr, "no memory for saving args\n"); exit(1); } - (void) strcpy(ret,sp); return(ret); } #endif diff --git a/src/appl/bsd/krcp.c b/src/appl/bsd/krcp.c index a24dde0fb..c791aaac4 100644 --- a/src/appl/bsd/krcp.c +++ b/src/appl/bsd/krcp.c @@ -206,31 +206,28 @@ int main(argc, argv) argc--, argv++; if (argc == 0) usage(); - if(!(krb_realm = (char *)malloc(strlen(*argv) + 1))){ + if(!(krb_realm = strdup(*argv))){ fprintf(stderr, "rcp: Cannot malloc.\n"); exit(1); } - strcpy(krb_realm, *argv); goto next_arg; case 'c': /* Change default ccache file */ argc--, argv++; if (argc == 0) usage(); - if(!(krb_cache = (char *)malloc(strlen(*argv) + 1))){ + if(!(krb_cache = strdup(*argv))){ fprintf(stderr, "rcp: Cannot malloc.\n"); exit(1); } - strcpy(krb_cache, *argv); goto next_arg; case 'C': /* Change default config file */ argc--, argv++; if (argc == 0) usage(); - if(!(krb_config = (char *)malloc(strlen(*argv) + 1))){ + if(!(krb_config = strdup(*argv))){ fprintf(stderr, "rcp: Cannot malloc.\n"); exit(1); } - strcpy(krb_config, *argv); goto next_arg; case 'P': if (!strcmp (*argv, "O")) diff --git a/src/appl/bsd/krlogin.c b/src/appl/bsd/krlogin.c index 0a0f5919c..4aa3b242b 100644 --- a/src/appl/bsd/krlogin.c +++ b/src/appl/bsd/krlogin.c @@ -483,11 +483,10 @@ main(argc, argv) "rlogin: -k flag must be followed with a realm name.\n"); exit (1); } - if(!(krb_realm = (char *)malloc(strlen(*argv) + 1))){ + if(!(krb_realm = strdup(*argv))){ fprintf(stderr, "rlogin: Cannot malloc.\n"); exit(1); } - strcpy(krb_realm, *argv); argv++, argc--; goto another; } diff --git a/src/appl/bsd/krsh.c b/src/appl/bsd/krsh.c index 155223fab..b12e25325 100644 --- a/src/appl/bsd/krsh.c +++ b/src/appl/bsd/krsh.c @@ -202,11 +202,10 @@ main(argc, argv0) fprintf(stderr, "rsh(kerberos): -k flag must have a realm after it.\n"); exit (1); } - if(!(krb_realm = (char *)malloc(strlen(*argv) + 1))){ + if(!(krb_realm = strdup(*argv))){ fprintf(stderr, "rsh(kerberos): Cannot malloc.\n"); exit(1); } - strcpy(krb_realm, *argv); argv++, argc--; goto another; } diff --git a/src/appl/bsd/krshd.c b/src/appl/bsd/krshd.c index 5a9baa379..e780216fa 100644 --- a/src/appl/bsd/krshd.c +++ b/src/appl/bsd/krshd.c @@ -1513,12 +1513,11 @@ void doit(f, fromp) struct stat s2; int offst = 0; - copy = malloc(strlen(cmdbuf) + 1); + copy = strdup(cmdbuf); if (copy == NULL) { perror("malloc"); _exit(1); } - strcpy(copy, cmdbuf); if (do_encrypt && !strncmp(cmdbuf, "-x ", 3)) { offst = 3; } diff --git a/src/appl/bsd/login.c b/src/appl/bsd/login.c index 861b9a57a..f54511e48 100644 --- a/src/appl/bsd/login.c +++ b/src/appl/bsd/login.c @@ -2448,11 +2448,10 @@ char *strsave(sp) { register char *ret; - if ((ret = (char *) malloc((unsigned) strlen(sp)+1)) == NULL) { + if ((ret = strdup(sp)) == NULL) { fprintf(stderr, "no memory for saving args\n"); exit(1); } - (void) strcpy(ret,sp); return(ret); } #endif diff --git a/src/appl/gssftp/ftp/ruserpass.c b/src/appl/gssftp/ftp/ruserpass.c index acfabfa2d..3495dda4e 100644 --- a/src/appl/gssftp/ftp/ruserpass.c +++ b/src/appl/gssftp/ftp/ruserpass.c @@ -187,8 +187,7 @@ next: case LOGIN: if (token()) { if (*aname == 0) { - *aname = malloc((unsigned) strlen(tokval) + 1); - (void) strcpy(*aname, tokval); + *aname = strdup(tokval); } else { if (strcmp(*aname, tokval)) goto next; @@ -204,8 +203,7 @@ next: goto bad; } if (token() && *apass == 0) { - *apass = malloc((unsigned) strlen(tokval) + 1); - (void) strcpy(*apass, tokval); + *apass = strdup(tokval); } break; case ACCOUNT: @@ -216,8 +214,7 @@ next: goto bad; } if (token() && *aacct == 0) { - *aacct = malloc((unsigned) strlen(tokval) + 1); - (void) strcpy(*aacct, tokval); + *aacct = strdup(tokval); } break; case MACDEF: diff --git a/src/appl/gssftp/ftpd/ftpcmd.y b/src/appl/gssftp/ftpd/ftpcmd.y index 30bced06a..ae1d11d2f 100644 --- a/src/appl/gssftp/ftpd/ftpcmd.y +++ b/src/appl/gssftp/ftpd/ftpcmd.y @@ -1438,10 +1438,9 @@ copy(s) { char *p; - p = malloc((unsigned) strlen(s) + 1); + p = strdup(s); if (p == NULL) fatal("Ran out of memory."); - (void) strcpy(p, s); return (p); } diff --git a/src/appl/gssftp/ftpd/ftpd.c b/src/appl/gssftp/ftpd/ftpd.c index 9d332608c..485332d3b 100644 --- a/src/appl/gssftp/ftpd/ftpd.c +++ b/src/appl/gssftp/ftpd/ftpd.c @@ -572,14 +572,13 @@ static char * sgetsave(s) char *s; { - char *new = malloc((unsigned) strlen(s) + 1); + char *new = strdup(s); if (new == NULL) { perror_reply(421, "Local resource failure: malloc"); dologout(1); /* NOTREACHED */ } - (void) strcpy(new, s); return (new); } diff --git a/src/appl/telnet/libtelnet/kerberos5.c b/src/appl/telnet/libtelnet/kerberos5.c index 1ef6fbace..ff94d0112 100644 --- a/src/appl/telnet/libtelnet/kerberos5.c +++ b/src/appl/telnet/libtelnet/kerberos5.c @@ -266,12 +266,11 @@ kerberos5_send(ap) rdata.magic = 0; rdata.length = strlen(telnet_krb5_realm); - rdata.data = (char *) malloc(rdata.length + 1); + rdata.data = strdup(telnet_krb5_realm); if (rdata.data == NULL) { fprintf(stderr, "malloc failed\n"); return(0); } - strcpy(rdata.data, telnet_krb5_realm); krb5_princ_set_realm(telnet_context, creds.server, &rdata); } diff --git a/src/ccapi/lib/ccapi_string.c b/src/ccapi/lib/ccapi_string.c index 4acd9a89d..4f4db6f43 100644 --- a/src/ccapi/lib/ccapi_string.c +++ b/src/ccapi/lib/ccapi_string.c @@ -67,10 +67,8 @@ cc_int32 cci_string_new (cc_string_t *out_string, } if (!err) { - string->data = malloc (strlen (in_cstring) + 1); - if (string->data) { - strcpy ((char *)string->data, in_cstring); - } else { + string->data = strdup (in_cstring); + if (!string->data) { err = cci_check_error (ccErrNoMem); } diff --git a/src/kadmin/dbutil/ovload.c b/src/kadmin/dbutil/ovload.c index fd5120736..46036478f 100644 --- a/src/kadmin/dbutil/ovload.c +++ b/src/kadmin/dbutil/ovload.c @@ -131,11 +131,10 @@ int process_ov_principal(fname, kcontext, filep, verbose, linenop) goto done; } else { if(strcmp(cp, "")) { - if((rec->policy = (char *) malloc(strlen(cp)+1)) == NULL) { + if((rec->policy = strdup(cp)) == NULL) { ret = ENOMEM; goto done; } - strcpy(rec->policy, cp); } else rec->policy = NULL; } if((cp = nstrtok((char *) NULL, "\t")) == NULL) { diff --git a/src/kadmin/testing/util/tcl_kadm5.c b/src/kadmin/testing/util/tcl_kadm5.c index a852f26a5..df57a801f 100644 --- a/src/kadmin/testing/util/tcl_kadm5.c +++ b/src/kadmin/testing/util/tcl_kadm5.c @@ -961,8 +961,7 @@ static int parse_tl_data(Tcl_Interp *interp, const char *list, retcode = TCL_ERROR; goto finished; } - tl->tl_data_contents = (krb5_octet *) malloc(tmp+1); - strcpy((char *) tl->tl_data_contents, argv1[2]); + tl->tl_data_contents = (krb5_octet *) strdup(argv1[2]); Tcl_Free((char *) argv1); argv1 = NULL; diff --git a/src/kdc/do_as_req.c b/src/kdc/do_as_req.c index afd02109f..0da9b4ad9 100644 --- a/src/kdc/do_as_req.c +++ b/src/kdc/do_as_req.c @@ -553,9 +553,8 @@ prepare_error_as (krb5_kdc_req *request, int error, krb5_data *e_data, errpkt.server = request->server; errpkt.client = request->client; errpkt.text.length = strlen(status)+1; - if (!(errpkt.text.data = malloc(errpkt.text.length))) + if (!(errpkt.text.data = strdup(status))) return ENOMEM; - (void) strcpy(errpkt.text.data, status); if (!(scratch = (krb5_data *)malloc(sizeof(*scratch)))) { free(errpkt.text.data); diff --git a/src/kdc/do_tgs_req.c b/src/kdc/do_tgs_req.c index ae25eb41c..fd862722e 100644 --- a/src/kdc/do_tgs_req.c +++ b/src/kdc/do_tgs_req.c @@ -741,9 +741,8 @@ prepare_error_tgs (krb5_kdc_req *request, krb5_ticket *ticket, int error, else errpkt.client = 0; errpkt.text.length = strlen(status) + 1; - if (!(errpkt.text.data = malloc(errpkt.text.length))) + if (!(errpkt.text.data = strdup(status))) return ENOMEM; - (void) strcpy(errpkt.text.data, status); if (!(scratch = (krb5_data *)malloc(sizeof(*scratch)))) { free(errpkt.text.data); diff --git a/src/lib/gssapi/generic/util_canonhost.c b/src/lib/gssapi/generic/util_canonhost.c index f1bd93f2b..ef093e621 100644 --- a/src/lib/gssapi/generic/util_canonhost.c +++ b/src/lib/gssapi/generic/util_canonhost.c @@ -59,11 +59,9 @@ g_canonicalize_host(char *hostname) xfree(haddr); - if ((canon = (char *) xmalloc(strlen(hent->h_name)+1)) == NULL) + if ((canon = (char *) strdup(hent->h_name)) == NULL) return(NULL); - strcpy(canon, hent->h_name); - for (str = canon; *str; str++) if (isupper(*str)) *str = tolower(*str); diff --git a/src/lib/gssapi/generic/util_localhost.c b/src/lib/gssapi/generic/util_localhost.c index 2d3c27cb2..c014400fb 100644 --- a/src/lib/gssapi/generic/util_localhost.c +++ b/src/lib/gssapi/generic/util_localhost.c @@ -45,8 +45,5 @@ g_local_host_name(void) buf[sizeof(buf)-1] = '\0'; - if (! (ptr = xmalloc(strlen(buf) + 1))) - return 0; - - return strcpy(ptr, buf); + return strdup(buf); } diff --git a/src/lib/gssapi/krb5/gssapi_krb5.c b/src/lib/gssapi/krb5/gssapi_krb5.c index 64812a78b..12e553f2f 100644 --- a/src/lib/gssapi/krb5/gssapi_krb5.c +++ b/src/lib/gssapi/krb5/gssapi_krb5.c @@ -246,12 +246,11 @@ kg_set_ccache_name (OM_uint32 *minor_status, const char *name) krb5_error_code kerr; if (name) { - new_name = malloc(strlen(name) + 1); + new_name = strdup(name); if (new_name == NULL) { *minor_status = ENOMEM; return GSS_S_FAILURE; } - strcpy(new_name, name); } kg_ccache_name = k5_getspecific(K5_KEY_GSS_KRB5_CCACHE_NAME); diff --git a/src/lib/gssapi/mechglue/g_dsp_status.c b/src/lib/gssapi/mechglue/g_dsp_status.c index 78dac8a5c..cb779aa90 100644 --- a/src/lib/gssapi/mechglue/g_dsp_status.c +++ b/src/lib/gssapi/mechglue/g_dsp_status.c @@ -358,12 +358,11 @@ gss_buffer_t outStr; /* now copy the status code and return to caller */ outStr->length = strlen(errStr); - outStr->value = malloc((size_t)outStr->length+1); + outStr->value = strdup(errStr); if (outStr->value == NULL) { outStr->length = 0; return (GSS_S_FAILURE); } - (void) strcpy((char *)outStr->value, errStr); return (GSS_S_COMPLETE); } /* displayMajor */ diff --git a/src/lib/gssapi/spnego/spnego_mech.c b/src/lib/gssapi/spnego/spnego_mech.c index a62336139..54b9af5d1 100644 --- a/src/lib/gssapi/spnego/spnego_mech.c +++ b/src/lib/gssapi/spnego/spnego_mech.c @@ -2260,14 +2260,7 @@ negotiate_mech_type(OM_uint32 *minor_status, static spnego_token_t make_spnego_token(char *name) { - spnego_token_t token; - - token = (spnego_token_t)malloc(strlen(name)+1); - - if (token == NULL) - return (NULL); - strcpy(token, name); - return (token); + return (spnego_token_t)strdup(name); } static gss_buffer_desc diff --git a/src/lib/kadm5/logger.c b/src/lib/kadm5/logger.c index a1edd495d..1dc886abd 100644 --- a/src/lib/kadm5/logger.c +++ b/src/lib/kadm5/logger.c @@ -664,10 +664,7 @@ krb5_klog_init(krb5_context kcontext, char *ename, char *whoami, krb5_boolean do log_control.log_nentries = 1; } if (log_control.log_nentries) { - log_control.log_whoami = (char *) malloc(strlen(whoami)+1); - if (log_control.log_whoami) - strcpy(log_control.log_whoami, whoami); - + log_control.log_whoami = strdup(whoami); log_control.log_hostname = (char *) malloc(MAXHOSTNAMELEN + 1); if (log_control.log_hostname) { gethostname(log_control.log_hostname, MAXHOSTNAMELEN); diff --git a/src/lib/kadm5/srv/server_acl.c b/src/lib/kadm5/srv/server_acl.c index 8eed476c8..f3172e49a 100644 --- a/src/lib/kadm5/srv/server_acl.c +++ b/src/lib/kadm5/srv/server_acl.c @@ -217,9 +217,8 @@ kadm5int_acl_parse_line(lp) } } if (opok) { - acle->ae_name = (char *) malloc(strlen(acle_principal)+1); + acle->ae_name = strdup(acle_principal); if (acle->ae_name) { - strcpy(acle->ae_name, acle_principal); acle->ae_principal = (krb5_principal) NULL; acle->ae_name_bad = 0; DPRINT(DEBUG_ACL, acl_debug_level, diff --git a/src/lib/kadm5/srv/svr_policy.c b/src/lib/kadm5/srv/svr_policy.c index 512876b79..3286404eb 100644 --- a/src/lib/kadm5/srv/svr_policy.c +++ b/src/lib/kadm5/srv/svr_policy.c @@ -289,11 +289,10 @@ kadm5_get_policy(void *server_handle, kadm5_policy_t name, if( cnt != 1 ) return KADM5_UNK_POLICY; - if ((entry->policy = (char *) malloc(strlen(t->name) + 1)) == NULL) { + if ((entry->policy = strdup(t->name)) == NULL) { krb5_db_free_policy(handle->context, t); return ENOMEM; } - strcpy(entry->policy, t->name); entry->pw_min_life = t->pw_min_life; entry->pw_max_life = t->pw_max_life; entry->pw_min_length = t->pw_min_length; diff --git a/src/lib/kadm5/srv/svr_principal.c b/src/lib/kadm5/srv/svr_principal.c index f0b54ee12..4b763b5ff 100644 --- a/src/lib/kadm5/srv/svr_principal.c +++ b/src/lib/kadm5/srv/svr_principal.c @@ -744,11 +744,10 @@ kadm5_get_principal(void *server_handle, krb5_principal principal, if ((mask & KADM5_POLICY) && adb.policy && (adb.aux_attributes & KADM5_POLICY)) { - if ((entry->policy = (char *) malloc(strlen(adb.policy) + 1)) == NULL) { + if ((entry->policy = strdup(adb.policy)) == NULL) { ret = ENOMEM; goto done; } - strcpy(entry->policy, adb.policy); } if (mask & KADM5_AUX_ATTRIBUTES) diff --git a/src/lib/krb5/asn.1/asn1buf.c b/src/lib/krb5/asn.1/asn1buf.c index 49ef84fbb..4162c9ee5 100644 --- a/src/lib/krb5/asn.1/asn1buf.c +++ b/src/lib/krb5/asn.1/asn1buf.c @@ -276,13 +276,11 @@ asn1_error_code asn1buf_unparse(const asn1buf *buf, char **s) { free(*s); if (buf == NULL) { - *s = malloc(sizeof("")); + *s = strdup(""); if (*s == NULL) return ENOMEM; - strcpy(*s,""); } else if (buf->base == NULL) { - *s = malloc(sizeof("")); + *s = strdup(""); if (*s == NULL) return ENOMEM; - strcpy(*s,""); } else { unsigned int length = asn1buf_len(buf); unsigned int i; @@ -305,13 +303,11 @@ asn1_error_code asn1buf_hex_unparse(const asn1buf *buf, char **s) free(*s); if (buf == NULL) { - *s = malloc(sizeof("")); + *s = strdup(""); if (*s == NULL) return ENOMEM; - strcpy(*s,""); } else if (buf->base == NULL) { - *s = malloc(sizeof("")); + *s = strdup(""); if (*s == NULL) return ENOMEM; - strcpy(*s,""); } else { unsigned int length = asn1buf_len(buf); int i; diff --git a/src/lib/krb5/ccache/cc_memory.c b/src/lib/krb5/ccache/cc_memory.c index 4a3b67c80..cdddc58f5 100644 --- a/src/lib/krb5/ccache/cc_memory.c +++ b/src/lib/krb5/ccache/cc_memory.c @@ -415,7 +415,7 @@ new_mcc_data (const char *name, krb5_mcc_data **dataptr) return err; } - d->name = malloc(strlen(name) + 1); + d->name = strdup(name); if (d->name == NULL) { k5_cc_mutex_destroy(&d->lock); krb5_xfree(d); @@ -426,9 +426,6 @@ new_mcc_data (const char *name, krb5_mcc_data **dataptr) d->changetime = 0; update_mcc_change_time(d); - /* Set up the filename */ - strcpy(d->name, name); - n = malloc(sizeof(krb5_mcc_list_node)); if (n == NULL) { free(d->name); diff --git a/src/lib/krb5/ccache/ccapi/stdcc.c b/src/lib/krb5/ccache/ccapi/stdcc.c index a5e171e24..de5de4dd5 100644 --- a/src/lib/krb5/ccache/ccapi/stdcc.c +++ b/src/lib/krb5/ccache/ccapi/stdcc.c @@ -347,12 +347,11 @@ krb5_stdccv3_generate_new (krb5_context context, krb5_ccache *id ) } if (!err) { - name = (char *) malloc (sizeof (*name) * (strlen (ccstring->data) + 1)); + name = strdup (ccstring->data); if (!name) { err = KRB5_CC_NOMEM; } } if (!err) { - strcpy (name, ccstring->data); ccapi_data->cache_name = name; name = NULL; /* take ownership */ @@ -407,7 +406,7 @@ krb5_stdccv3_resolve (krb5_context context, krb5_ccache *id , const char *residu } if (!err) { - name = malloc (strlen(residual) + 1); + name = strdup (residual); if (!name) { err = KRB5_CC_NOMEM; } } @@ -421,7 +420,6 @@ krb5_stdccv3_resolve (krb5_context context, krb5_ccache *id , const char *residu } if (!err) { - strcpy(name, residual); ccapi_data->cache_name = name; name = NULL; /* take ownership */ @@ -936,12 +934,11 @@ krb5_stdccv3_ptcursor_next( } if (!err) { - name = (char *) malloc (sizeof (*name) * (strlen (ccstring->data) + 1)); + name = strdup (ccstring->data); if (!name) { err = KRB5_CC_NOMEM; } } if (!err) { - strcpy (name, ccstring->data); ccapi_data->cache_name = name; name = NULL; /* take ownership */ @@ -1198,15 +1195,13 @@ krb5_error_code KRB5_CALLCONV krb5_stdcc_resolve if (!(ccapi_data = (stdccCacheDataPtr)malloc(sizeof(stdccCacheData)))) goto errout; - if (!(cName = malloc(strlen(residual)+1))) + if (!(cName = strdup(residual))) goto errout; newCache->ops = &krb5_cc_stdcc_ops; newCache->data = ccapi_data; ccapi_data->cache_name = cName; - strcpy(cName, residual); - err = cc_open(gCntrlBlock, cName, CC_CRED_V5, 0L, &ccapi_data->NamedCache); if (err != CC_NOERROR) { diff --git a/src/lib/krb5/keytab/kt_file.c b/src/lib/krb5/keytab/kt_file.c index cd2298ba5..bf394d887 100644 --- a/src/lib/krb5/keytab/kt_file.c +++ b/src/lib/krb5/keytab/kt_file.c @@ -221,14 +221,13 @@ ktfile_common_resolve(krb5_context context, const char *name, return err; } - if ((data->name = (char *)calloc(strlen(name) + 1, sizeof(char))) == NULL) { + if ((data->name = strdup(name)) == NULL) { k5_mutex_destroy(&data->lock); krb5_xfree(data); krb5_xfree(*id); return(ENOMEM); } - (void) strcpy(data->name, name); data->openf = 0; data->version = 0; data->iter_count = 0; diff --git a/src/lib/krb5/keytab/kt_memory.c b/src/lib/krb5/keytab/kt_memory.c index eb1dd77e0..1b4af9bd6 100644 --- a/src/lib/krb5/keytab/kt_memory.c +++ b/src/lib/krb5/keytab/kt_memory.c @@ -252,7 +252,7 @@ krb5_mkt_resolve(krb5_context context, const char *name, krb5_keytab *id) goto done; } - if ((data->name = (char *)calloc(strlen(name) + 1, sizeof(char))) == NULL) { + if ((data->name = strdup(name)) == NULL) { k5_mutex_destroy(&data->lock); krb5_xfree(data); krb5_xfree(list->keytab); @@ -261,8 +261,6 @@ krb5_mkt_resolve(krb5_context context, const char *name, krb5_keytab *id) goto done; } - (void) strcpy(data->name, name); - data->link = NULL; data->refcount = 0; list->keytab->data = (krb5_pointer)data; diff --git a/src/lib/krb5/keytab/kt_srvtab.c b/src/lib/krb5/keytab/kt_srvtab.c index 2bc460345..da09d87a5 100644 --- a/src/lib/krb5/keytab/kt_srvtab.c +++ b/src/lib/krb5/keytab/kt_srvtab.c @@ -127,14 +127,13 @@ krb5_ktsrvtab_resolve(krb5_context context, const char *name, krb5_keytab *id) return(ENOMEM); } - data->name = (char *)malloc(strlen(name) + 1); + data->name = strdup(name); if (data->name == NULL) { krb5_xfree(data); krb5_xfree(*id); return(ENOMEM); } - (void) strcpy(data->name, name); data->openf = 0; (*id)->data = (krb5_pointer)data; diff --git a/src/lib/krb5/krb/get_in_tkt.c b/src/lib/krb5/krb/get_in_tkt.c index 36b731155..a1c29a46a 100644 --- a/src/lib/krb5/krb/get_in_tkt.c +++ b/src/lib/krb5/krb/get_in_tkt.c @@ -788,11 +788,9 @@ goodbye: if (!nameval[0]) { retval = ENOENT; } else { - *ret_value = malloc(strlen(nameval[0]) + 1); + *ret_value = strdup(nameval[0]); if (!*ret_value) retval = ENOMEM; - else - strcpy(*ret_value, nameval[0]); } profile_free_list(nameval); diff --git a/src/lib/krb5/krb/set_realm.c b/src/lib/krb5/krb/set_realm.c index 16112a8da..edb72ae7f 100644 --- a/src/lib/krb5/krb/set_realm.c +++ b/src/lib/krb5/krb/set_realm.c @@ -36,10 +36,9 @@ krb5_set_principal_realm(krb5_context context, krb5_principal principal, const c return -EINVAL; length = strlen(realm); - newrealm = malloc(length+1); /* Include room for the null */ + newrealm = strdup(realm); if (!newrealm) return -ENOMEM; - strcpy(newrealm, realm); (void) krb5_xfree(krb5_princ_realm(context,principal)->data); diff --git a/src/lib/krb5/os/ccdefname.c b/src/lib/krb5/os/ccdefname.c index 8d42a965a..63e00d421 100644 --- a/src/lib/krb5/os/ccdefname.c +++ b/src/lib/krb5/os/ccdefname.c @@ -251,13 +251,9 @@ krb5_cc_set_default_name(krb5_context context, const char *name) if (name != NULL) { if (!err) { /* If the name isn't NULL, make a copy of it */ - new_ccname = malloc (strlen (name) + 1); + new_ccname = strdup (name); if (new_ccname == NULL) { err = ENOMEM; } } - - if (!err) { - strcpy (new_ccname, name); - } } if (!err) { diff --git a/src/lib/krb5/os/def_realm.c b/src/lib/krb5/os/def_realm.c index d63f1af63..13a025d9b 100644 --- a/src/lib/krb5/os/def_realm.c +++ b/src/lib/krb5/os/def_realm.c @@ -72,7 +72,6 @@ krb5_error_code KRB5_CALLCONV krb5_get_default_realm(krb5_context context, char **lrealm) { char *realm = 0; - char *cp; krb5_error_code retval; if (!context || (context->magic != KV5M_CONTEXT)) @@ -90,12 +89,11 @@ krb5_get_default_realm(krb5_context context, char **lrealm) &realm); if (!retval && realm) { - context->default_realm = malloc(strlen(realm) + 1); + context->default_realm = strdup(realm); if (!context->default_realm) { profile_release_string(realm); return ENOMEM; } - strcpy(context->default_realm, realm); profile_release_string(realm); } } @@ -155,9 +153,8 @@ krb5_get_default_realm(krb5_context context, char **lrealm) realm = context->default_realm; - if (!(*lrealm = cp = malloc((unsigned int) strlen(realm) + 1))) + if (!(*lrealm = strdup(realm))) return ENOMEM; - strcpy(cp, realm); return(0); } @@ -176,12 +173,11 @@ krb5_set_default_realm(krb5_context context, const char *lrealm) NULL */ if (!lrealm) return 0; - context->default_realm = malloc(strlen (lrealm) + 1); + context->default_realm = strdup(lrealm); if (!context->default_realm) return ENOMEM; - strcpy(context->default_realm, lrealm); return(0); } diff --git a/src/lib/krb5/os/hst_realm.c b/src/lib/krb5/os/hst_realm.c index 983637864..258288d93 100644 --- a/src/lib/krb5/os/hst_realm.c +++ b/src/lib/krb5/os/hst_realm.c @@ -250,19 +250,17 @@ krb5_get_host_realm(krb5_context context, const char *host, char ***realmsp) #ifdef DEBUG_REFERRALS printf(" temp_realm is %s\n",temp_realm); #endif - realm = malloc(strlen(temp_realm) + 1); + realm = strdup(temp_realm); if (!realm) { profile_release_string(temp_realm); return ENOMEM; } - strcpy(realm, temp_realm); profile_release_string(temp_realm); } if (realm == (char *)NULL) { - if (!(cp = (char *)malloc(strlen(KRB5_REFERRAL_REALM)+1))) + if (!(cp = strdup(KRB5_REFERRAL_REALM))) return ENOMEM; - strcpy(cp, KRB5_REFERRAL_REALM); realm = cp; } @@ -400,9 +398,8 @@ krb5_get_fallback_host_realm(krb5_context context, krb5_data *hdata, char ***rea if (realm == (char *)NULL) { if (default_realm != (char *)NULL) { /* We are defaulting to the realm of the host */ - if (!(cp = (char *)malloc(strlen(default_realm)+1))) + if (!(cp = strdup(default_realm))) return ENOMEM; - strcpy(cp, default_realm); realm = cp; /* Assume the realm name is upper case */ diff --git a/src/lib/krb5/os/init_os_ctx.c b/src/lib/krb5/os/init_os_ctx.c index 4799e9107..7e21b6ef7 100644 --- a/src/lib/krb5/os/init_os_ctx.c +++ b/src/lib/krb5/os/init_os_ctx.c @@ -198,9 +198,8 @@ os_get_default_config_files(profile_filespec_t **pfiles, krb5_boolean secure) char *env = getenv("KRB5_CONFIG"); if (env) { - name = malloc(strlen(env) + 1); + name = strdup(env); if (!name) return ENOMEM; - strcpy(name, env); } } if (!name && !secure) diff --git a/src/lib/krb5/os/promptusr.c b/src/lib/krb5/os/promptusr.c index 68f8b14a0..3c9c50a6c 100644 --- a/src/lib/krb5/os/promptusr.c +++ b/src/lib/krb5/os/promptusr.c @@ -90,11 +90,10 @@ krb5_os_get_tty_uio(krb5_context context, krb5_uio uio) } while (ch != EOF && ch != '\n'); read_string[sizeof(read_string)-1] = 0; - if ((p->response = malloc(strlen(read_string)+1)) == NULL) { + if ((p->response = strdup(read_string)) == NULL) { errno = ENOMEM; goto cleanup; } - strcpy(p->response, read_string); if ((p->flags & KRB5_UIO_ECHORESPONSE) == 0) { (void) putchar('\n'); diff --git a/src/lib/krb5/os/realm_dom.c b/src/lib/krb5/os/realm_dom.c index d7a4be7db..43e6266ca 100644 --- a/src/lib/krb5/os/realm_dom.c +++ b/src/lib/krb5/os/realm_dom.c @@ -55,11 +55,9 @@ krb5_get_realm_domain(krb5_context context, const char *realm, char **domain) "default_domain", realm, &temp_domain); if (!retval && temp_domain) { - *domain = malloc(strlen(temp_domain) + 1); + *domain = strdup(temp_domain); if (!*domain) { retval = ENOMEM; - } else { - strcpy(*domain, temp_domain); } profile_release_string(temp_domain); } diff --git a/src/lib/krb5/rcache/rc_dfl.c b/src/lib/krb5/rcache/rc_dfl.c index 4b1c174b2..f03971909 100644 --- a/src/lib/krb5/rcache/rc_dfl.c +++ b/src/lib/krb5/rcache/rc_dfl.c @@ -285,12 +285,11 @@ krb5_rc_dfl_resolve(krb5_context context, krb5_rcache id, char *name) return KRB5_RC_MALLOC; id->data = (krb5_pointer) t; if (name) { - t->name = malloc(strlen(name)+1); + t->name = strdup(name); if (!t->name) { retval = KRB5_RC_MALLOC; goto cleanup; } - strcpy(t->name, name); } else t->name = 0; t->numhits = t->nummisses = 0; diff --git a/src/lib/krb5/rcache/rc_io.c b/src/lib/krb5/rcache/rc_io.c index 6692ae145..0309ade5f 100644 --- a/src/lib/krb5/rcache/rc_io.c +++ b/src/lib/krb5/rcache/rc_io.c @@ -233,9 +233,8 @@ krb5_rc_io_open_internal(krb5_context context, krb5_rc_iostuff *d, char *fn, GETDIR; if (full_pathname) { - if (!(d->fn = malloc(strlen(full_pathname) + 1))) + if (!(d->fn = strdup(full_pathname))) return KRB5_RC_IO_MALLOC; - (void) strcpy(d->fn, full_pathname); } else { if (!(d->fn = malloc(strlen(fn) + dirlen + 1))) return KRB5_RC_IO_MALLOC; diff --git a/src/plugins/preauth/pkinit/pkinit_profile.c b/src/plugins/preauth/pkinit/pkinit_profile.c index 403068a7d..1f7045aca 100644 --- a/src/plugins/preauth/pkinit/pkinit_profile.c +++ b/src/plugins/preauth/pkinit/pkinit_profile.c @@ -161,11 +161,9 @@ pkinit_kdcdefault_string(krb5_context context, const char *realmname, if (values[0] == NULL) { retval = ENOENT; } else { - *ret_value = malloc(strlen(values[0]) + 1); + *ret_value = strdup(values[0]); if (*ret_value == NULL) retval = ENOMEM; - else - strcpy(*ret_value, values[0]); } profile_free_list(values); @@ -321,11 +319,9 @@ pkinit_libdefault_string(krb5_context context, const krb5_data *realm, if (values[0] == NULL) { retval = ENOENT; } else { - *ret_value = malloc(strlen(values[0]) + 1); + *ret_value = strdup(values[0]); if (*ret_value == NULL) retval = ENOMEM; - else - strcpy(*ret_value, values[0]); } profile_free_list(values); diff --git a/src/slave/kprop.c b/src/slave/kprop.c index 164f8aeb8..f47b2b74d 100644 --- a/src/slave/kprop.c +++ b/src/slave/kprop.c @@ -711,9 +711,8 @@ send_error(context, my_creds, fd, err_text, err_code) else text = error_message(err_code); error.text.length = strlen(text) + 1; - error.text.data = malloc((unsigned int) error.text.length); + error.text.data = strdup(text); if (error.text.data) { - strcpy(error.text.data, text); if (!krb5_mk_error(context, &error, &outbuf)) { (void) krb5_write_message(context, (void *)&fd,&outbuf); krb5_free_data_contents(context, &outbuf); diff --git a/src/slave/kpropd.c b/src/slave/kpropd.c index fd46819a1..959dd4ab5 100644 --- a/src/slave/kpropd.c +++ b/src/slave/kpropd.c @@ -1441,9 +1441,8 @@ send_error(context, fd, err_code, err_text) } } error.text.length = strlen(text) + 1; - error.text.data = malloc(error.text.length); + error.text.data = strdup(text); if (error.text.data) { - strcpy(error.text.data, text); if (!krb5_mk_error(context, &error, &outbuf)) { (void) krb5_write_message(context, (void *)&fd,&outbuf); krb5_free_data_contents(context, &outbuf); diff --git a/src/tests/asn.1/ktest.c b/src/tests/asn.1/ktest.c index 9e8754dd6..d1ba257fc 100644 --- a/src/tests/asn.1/ktest.c +++ b/src/tests/asn.1/ktest.c @@ -652,10 +652,9 @@ krb5_error_code ktest_make_sample_etype_info(p) info[i]->etype = i; sprintf(buf, "Morton's #%d", i); info[i]->length = strlen(buf); - info[i]->salt = malloc((size_t) (info[i]->length+1)); + info[i]->salt = (unsigned char *) strdup(buf); if (info[i]->salt == 0) goto memfail; - strcpy((char *) info[i]->salt, buf); info[i]->s2kparams.data = NULL; info[i]->s2kparams.length = 0; info[i]->magic = KV5M_ETYPE_INFO_ENTRY; @@ -690,10 +689,9 @@ krb5_error_code ktest_make_sample_etype_info2(p) info[i]->etype = i; sprintf(buf, "Morton's #%d", i); info[i]->length = strlen(buf); - info[i]->salt = malloc((size_t) (info[i]->length+1)); + info[i]->salt = (unsigned char *) strdup(buf); if (info[i]->salt == 0) goto memfail; - strcpy((char *) info[i]->salt, buf); sprintf(buf, "s2k: %d", i); info[i]->s2kparams.data = malloc(strlen(buf)+1); if (info[i]->s2kparams.data == NULL) diff --git a/src/tests/asn.1/utility.c b/src/tests/asn.1/utility.c index f2a856af2..00d7f6298 100644 --- a/src/tests/asn.1/utility.c +++ b/src/tests/asn.1/utility.c @@ -15,13 +15,11 @@ asn1_error_code asn1_krb5_data_unparse(code, s) if (*s != NULL) free(*s); if (code==NULL) { - *s = (char*)calloc(strlen("")+1, sizeof(char)); + *s = strdup(""); if (*s == NULL) return ENOMEM; - strcpy(*s,""); } else if (code->data == NULL || ((int) code->length) <= 0) { - *s = (char*)calloc(strlen("")+1, sizeof(char)); + *s = strdup(""); if (*s==NULL) return ENOMEM; - strcpy(*s,""); } else { unsigned int i; diff --git a/src/util/et/error_table.y b/src/util/et/error_table.y index f6b082fd4..6162d61b5 100644 --- a/src/util/et/error_table.y +++ b/src/util/et/error_table.y @@ -100,10 +100,7 @@ char * ds(string) char const *string; { - char *rv; - rv = malloc(strlen(string)+1); - strcpy(rv, string); - return(rv); + return strdup(string); } char * diff --git a/src/util/profile/prof_get.c b/src/util/profile/prof_get.c index c79500215..6426e15b9 100644 --- a/src/util/profile/prof_get.c +++ b/src/util/profile/prof_get.c @@ -85,10 +85,9 @@ static errcode_t add_to_list(struct profile_string_list *list, const char *str) list->max = newmax; list->list = newlist; } - newstr = malloc(strlen(str)+1); + newstr = strdup(str); if (newstr == 0) return ENOMEM; - strcpy(newstr, str); list->list[list->num++] = newstr; list->list[list->num] = 0; @@ -217,10 +216,9 @@ profile_get_string(profile_t profile, const char *name, const char *subname, value = def_val; if (value) { - *ret_string = malloc(strlen(value)+1); + *ret_string = strdup(value); if (*ret_string == 0) return ENOMEM; - strcpy(*ret_string, value); } else *ret_string = 0; return 0; diff --git a/src/util/profile/prof_tree.c b/src/util/profile/prof_tree.c index a99306afd..2b644a935 100644 --- a/src/util/profile/prof_tree.c +++ b/src/util/profile/prof_tree.c @@ -638,11 +638,10 @@ errcode_t profile_set_relation_value(struct profile_node *node, if (!node->value) return PROF_SET_SECTION_VALUE; - cp = malloc(strlen(new_value)+1); + cp = strdup(new_value); if (!cp) return ENOMEM; - strcpy(cp, new_value); free(node->value); node->value = cp; @@ -667,10 +666,9 @@ errcode_t profile_rename_node(struct profile_node *node, const char *new_name) /* * Make sure we can allocate memory for the new name, first! */ - new_string = malloc(strlen(new_name)+1); + new_string = strdup(new_name); if (!new_string) return ENOMEM; - strcpy(new_string, new_name); /* * Find the place to where the new node should go. We look diff --git a/src/util/ss/help.c b/src/util/ss/help.c index b144e282e..10b1c1075 100644 --- a/src/util/ss/help.c +++ b/src/util/ss/help.c @@ -128,8 +128,7 @@ void ss_add_info_dir(sci_idx, info_dir, code_ptr) } info->info_dirs = dirs; dirs[n_dirs + 1] = (char *)NULL; - dirs[n_dirs] = malloc((unsigned)strlen(info_dir)+1); - strcpy(dirs[n_dirs], info_dir); + dirs[n_dirs] = strdup(info_dir); *code_ptr = 0; } diff --git a/src/wconfig.c b/src/wconfig.c index 087a54b5c..a2ec5b005 100644 --- a/src/wconfig.c +++ b/src/wconfig.c @@ -104,13 +104,12 @@ int main(int argc, char *argv[]) if (islower(*cp)) *cp = toupper(*cp); } - cp = malloc(strlen(tmp)+1); + cp = strdup(tmp); if (!cp) { fprintf(stderr, "wconfig: malloc failed!\n"); exit(1); } - strcpy(cp, tmp); add_ignore_list(cp); argc--; argv++; continue;