From 3ad7c4b93b0f3a79a1526a3da7b897f6e13875d3 Mon Sep 17 00:00:00 2001 From: Ken Raeburn Date: Thu, 7 Aug 2008 01:23:33 +0000 Subject: [PATCH] malloc+memset(,0,) -> calloc git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@20630 dc483132-0cff-0310-8789-dd5450dbe970 --- src/lib/krb5/krb/auth_con.c | 9 +++------ src/lib/krb5/krb/gc_via_tkt.c | 4 +--- src/lib/krb5/krb/init_ctx.c | 3 +-- src/lib/krb5/krb/mk_cred.c | 11 ++++------- src/lib/krb5/krb/pkinit_apple_asn1.c | 5 ++--- src/lib/krb5/krb/preauth2.c | 5 ++--- src/lib/krb5/krb/rd_cred.c | 3 +-- src/lib/krb5/krb/ser_actx.c | 5 ++--- src/lib/krb5/krb/ser_adata.c | 5 ++--- src/lib/krb5/krb/ser_addr.c | 5 ++--- src/lib/krb5/krb/ser_auth.c | 10 +++------- src/lib/krb5/krb/ser_cksum.c | 6 ++---- src/lib/krb5/krb/ser_ctx.c | 20 +++++++------------- src/lib/krb5/krb/ser_eblk.c | 6 ++---- src/lib/krb5/krb/ser_key.c | 6 ++---- src/lib/krb5/os/localaddr.c | 10 +++------- src/lib/krb5/os/locate_kdc.c | 3 +-- src/lib/krb5/os/sendto_kdc.c | 8 ++------ src/lib/krb5/rcache/rc_dfl.c | 3 +-- 19 files changed, 43 insertions(+), 84 deletions(-) diff --git a/src/lib/krb5/krb/auth_con.c b/src/lib/krb5/krb/auth_con.c index ca33862fd..ba62d687e 100644 --- a/src/lib/krb5/krb/auth_con.c +++ b/src/lib/krb5/krb/auth_con.c @@ -24,11 +24,9 @@ krb5_error_code KRB5_CALLCONV krb5_auth_con_init(krb5_context context, krb5_auth_context *auth_context) { *auth_context = - (krb5_auth_context)malloc(sizeof(struct _krb5_auth_context)); + (krb5_auth_context)calloc(1, sizeof(struct _krb5_auth_context)); if (!*auth_context) - return ENOMEM; - - memset(*auth_context, 0, sizeof(struct _krb5_auth_context)); + return ENOMEM; /* Default flags, do time not seq */ (*auth_context)->auth_context_flags = @@ -271,8 +269,7 @@ krb5_auth_con_initivector(krb5_context context, krb5_auth_context auth_context) if ((ret = krb5_c_block_size(context, auth_context->keyblock->enctype, &blocksize))) return(ret); - if ((auth_context->i_vector = (krb5_pointer)malloc(blocksize))) { - memset(auth_context->i_vector, 0, blocksize); + if ((auth_context->i_vector = (krb5_pointer)calloc(1,blocksize))) { return 0; } return ENOMEM; diff --git a/src/lib/krb5/krb/gc_via_tkt.c b/src/lib/krb5/krb/gc_via_tkt.c index 3a9c21818..22ac7f9db 100644 --- a/src/lib/krb5/krb/gc_via_tkt.c +++ b/src/lib/krb5/krb/gc_via_tkt.c @@ -43,12 +43,10 @@ krb5_kdcrep2creds(krb5_context context, krb5_kdc_rep *pkdcrep, krb5_address *con krb5_error_code retval; krb5_data *pdata; - if ((*ppcreds = (krb5_creds *)malloc(sizeof(krb5_creds))) == NULL) { + if ((*ppcreds = (krb5_creds *)calloc(1,sizeof(krb5_creds))) == NULL) { return ENOMEM; } - memset(*ppcreds, 0, sizeof(krb5_creds)); - if ((retval = krb5_copy_principal(context, pkdcrep->client, &(*ppcreds)->client))) goto cleanup; diff --git a/src/lib/krb5/krb/init_ctx.c b/src/lib/krb5/krb/init_ctx.c index 3ebbb908d..467aec951 100644 --- a/src/lib/krb5/krb/init_ctx.c +++ b/src/lib/krb5/krb/init_ctx.c @@ -154,10 +154,9 @@ init_common (krb5_context *context, krb5_boolean secure, krb5_boolean kdc) *context = 0; - ctx = malloc(sizeof(struct _krb5_context)); + ctx = calloc(1, sizeof(struct _krb5_context)); if (!ctx) return ENOMEM; - memset(ctx, 0, sizeof(struct _krb5_context)); ctx->magic = KV5M_CONTEXT; ctx->profile_secure = secure; diff --git a/src/lib/krb5/krb/mk_cred.c b/src/lib/krb5/krb/mk_cred.c index 3479aa29e..e9ed3850b 100644 --- a/src/lib/krb5/krb/mk_cred.c +++ b/src/lib/krb5/krb/mk_cred.c @@ -87,11 +87,10 @@ krb5_mk_ncred_basic(krb5_context context, /* Get memory for creds and initialize it */ size = sizeof(krb5_cred_info *) * (nppcreds + 1); - credenc.ticket_info = (krb5_cred_info **) malloc(size); + credenc.ticket_info = (krb5_cred_info **) calloc(1, size); if (credenc.ticket_info == NULL) return ENOMEM; - memset(credenc.ticket_info, 0, size); - + /* * For each credential in the list, initialize a cred info * structure and copy the ticket into the ticket list. @@ -177,16 +176,14 @@ krb5_mk_ncred(krb5_context context, krb5_auth_context auth_context, */ for (ncred = 0; ppcreds[ncred]; ncred++); - if ((pcred = (krb5_cred *)malloc(sizeof(krb5_cred))) == NULL) + if ((pcred = (krb5_cred *)calloc(1, sizeof(krb5_cred))) == NULL) return ENOMEM; - memset(pcred, 0, sizeof(krb5_cred)); if ((pcred->tickets - = (krb5_ticket **)malloc(sizeof(krb5_ticket *) * (ncred + 1))) == NULL) { + = (krb5_ticket **)calloc(ncred+1, sizeof(krb5_ticket *))) == NULL) { free(pcred); return ENOMEM; } - memset(pcred->tickets, 0, sizeof(krb5_ticket *) * (ncred +1)); /* Get keyblock */ if ((keyblock = auth_context->send_subkey) == NULL) diff --git a/src/lib/krb5/krb/pkinit_apple_asn1.c b/src/lib/krb5/krb/pkinit_apple_asn1.c index 52ae1b040..9082a314b 100644 --- a/src/lib/krb5/krb/pkinit_apple_asn1.c +++ b/src/lib/krb5/krb/pkinit_apple_asn1.c @@ -323,10 +323,9 @@ krb5_error_code krb5int_pkinit_auth_pack_decode( alg_ids++) { num_types++; } - *cms_types = kalg_ids = (krb5int_algorithm_id *)malloc( - sizeof(krb5int_algorithm_id) * num_types); + *cms_types = kalg_ids = (krb5int_algorithm_id *)calloc(num_types, + sizeof(krb5int_algorithm_id)); *num_cms_types = num_types; - memset(kalg_ids, 0, sizeof(krb5int_algorithm_id) * num_types); alg_ids = localAuthPack.supportedCMSTypes; for(dex=0; dexalgorithm.Data) { diff --git a/src/lib/krb5/krb/preauth2.c b/src/lib/krb5/krb/preauth2.c index 32beb47a7..fd7d5483a 100644 --- a/src/lib/krb5/krb/preauth2.c +++ b/src/lib/krb5/krb/preauth2.c @@ -1,5 +1,5 @@ /* - * Copyright 1995, 2003 by the Massachusetts Institute of Technology. All + * Copyright 1995, 2003, 2008 by the Massachusetts Institute of Technology. All * Rights Reserved. * * Export of this software from the United States of America may @@ -127,13 +127,12 @@ krb5_init_preauth_context(krb5_context kcontext) krb5int_free_plugin_dir_data(tables); return; } - context->modules = malloc(sizeof(context->modules[0]) * n_modules); + context->modules = calloc(n_modules, sizeof(context->modules[0])); if (context->modules == NULL) { krb5int_free_plugin_dir_data(tables); free(context); return; } - memset(context->modules, 0, sizeof(context->modules[0]) * n_modules); context->n_modules = n_modules; /* fill in the structure */ diff --git a/src/lib/krb5/krb/rd_cred.c b/src/lib/krb5/krb/rd_cred.c index 19370d37b..3c76506b2 100644 --- a/src/lib/krb5/krb/rd_cred.c +++ b/src/lib/krb5/krb/rd_cred.c @@ -99,7 +99,7 @@ krb5_rd_cred_basic(krb5_context context, krb5_data *pcreddata, krb5_creds * pcur; krb5_data * pdata; - if ((pcur = (krb5_creds *)malloc(sizeof(krb5_creds))) == NULL) { + if ((pcur = (krb5_creds *)calloc(1, sizeof(krb5_creds))) == NULL) { retval = ENOMEM; goto cleanup; } @@ -107,7 +107,6 @@ krb5_rd_cred_basic(krb5_context context, krb5_data *pcreddata, (*pppcreds)[i] = pcur; (*pppcreds)[i+1] = 0; pinfo = encpart.ticket_info[i++]; - memset(pcur, 0, sizeof(krb5_creds)); if ((retval = krb5_copy_principal(context, pinfo->client, &pcur->client))) diff --git a/src/lib/krb5/krb/ser_actx.c b/src/lib/krb5/krb/ser_actx.c index 32519e19f..5e4be29c2 100644 --- a/src/lib/krb5/krb/ser_actx.c +++ b/src/lib/krb5/krb/ser_actx.c @@ -1,7 +1,7 @@ /* * lib/krb5/krb/ser_actx.c * - * Copyright 1995 by the Massachusetts Institute of Technology. + * Copyright 1995, 2008 by the Massachusetts Institute of Technology. * All Rights Reserved. * * Export of this software from the United States of America may @@ -371,8 +371,7 @@ krb5_auth_context_internalize(krb5_context kcontext, krb5_pointer *argp, krb5_oc /* Get memory for the auth_context */ if ((remain >= (5*sizeof(krb5_int32))) && (auth_context = (krb5_auth_context) - malloc(sizeof(struct _krb5_auth_context)))) { - memset(auth_context, 0, sizeof(struct _krb5_auth_context)); + calloc(1, sizeof(struct _krb5_auth_context)))) { /* Get auth_context_flags */ (void) krb5_ser_unpack_int32(&ibuf, &bp, &remain); diff --git a/src/lib/krb5/krb/ser_adata.c b/src/lib/krb5/krb/ser_adata.c index ebd4c3e10..82d04dce1 100644 --- a/src/lib/krb5/krb/ser_adata.c +++ b/src/lib/krb5/krb/ser_adata.c @@ -1,7 +1,7 @@ /* * lib/krb5/krb/ser_adata.c * - * Copyright 1995 by the Massachusetts Institute of Technology. + * Copyright 1995, 2008 by the Massachusetts Institute of Technology. * All Rights Reserved. * * Export of this software from the United States of America may @@ -151,8 +151,7 @@ krb5_authdata_internalize(krb5_context kcontext, krb5_pointer *argp, krb5_octet /* Get a authdata */ if ((remain >= (2*sizeof(krb5_int32))) && - (authdata = (krb5_authdata *) malloc(sizeof(krb5_authdata)))) { - memset(authdata, 0, sizeof(krb5_authdata)); + (authdata = (krb5_authdata *) calloc(1, sizeof(krb5_authdata)))) { /* Get the ad_type */ (void) krb5_ser_unpack_int32(&ibuf, &bp, &remain); diff --git a/src/lib/krb5/krb/ser_addr.c b/src/lib/krb5/krb/ser_addr.c index 079cc0fda..11b7f6abf 100644 --- a/src/lib/krb5/krb/ser_addr.c +++ b/src/lib/krb5/krb/ser_addr.c @@ -1,7 +1,7 @@ /* * lib/krb5/krb/ser_addr.c * - * Copyright 1995 by the Massachusetts Institute of Technology. + * Copyright 1995, 2008 by the Massachusetts Institute of Technology. * All Rights Reserved. * * Export of this software from the United States of America may @@ -152,8 +152,7 @@ krb5_address_internalize(krb5_context kcontext, krb5_pointer *argp, krb5_octet * /* Get a address */ if ((remain >= (2*sizeof(krb5_int32))) && - (address = (krb5_address *) malloc(sizeof(krb5_address)))) { - memset(address, 0, sizeof(krb5_address)); + (address = (krb5_address *) calloc(1, sizeof(krb5_address)))) { address->magic = KV5M_ADDRESS; diff --git a/src/lib/krb5/krb/ser_auth.c b/src/lib/krb5/krb/ser_auth.c index 814028e27..d76ec500a 100644 --- a/src/lib/krb5/krb/ser_auth.c +++ b/src/lib/krb5/krb/ser_auth.c @@ -1,7 +1,7 @@ /* * lib/krb5/krb/ser_auth.c * - * Copyright 1995 by the Massachusetts Institute of Technology. + * Copyright 1995, 2008 by the Massachusetts Institute of Technology. * All Rights Reserved. * * Export of this software from the United States of America may @@ -246,8 +246,7 @@ krb5_authenticator_internalize(krb5_context kcontext, krb5_pointer *argp, krb5_o /* Get memory for the authenticator */ if ((remain >= (3*sizeof(krb5_int32))) && (authenticator = (krb5_authenticator *) - malloc(sizeof(krb5_authenticator)))) { - memset(authenticator, 0, sizeof(krb5_authenticator)); + calloc(1, sizeof(krb5_authenticator)))) { /* Get ctime */ (void) krb5_ser_unpack_int32(&ibuf, &bp, &remain); @@ -304,10 +303,7 @@ krb5_authenticator_internalize(krb5_context kcontext, krb5_pointer *argp, krb5_o /* Get memory for the authorization data pointers */ if ((authenticator->authorization_data = (krb5_authdata **) - malloc(sizeof(krb5_authdata *) * len))) { - memset(authenticator->authorization_data, 0, - sizeof(krb5_authdata *) * len); - + calloc(len, sizeof(krb5_authdata *)))) { for (i=0; !kret && (i= (2*sizeof(krb5_int32))) && - (checksum = (krb5_checksum *) malloc(sizeof(krb5_checksum)))) { - memset(checksum, 0, sizeof(krb5_checksum)); - + (checksum = (krb5_checksum *) calloc(1, sizeof(krb5_checksum)))) { /* Get the checksum_type */ (void) krb5_ser_unpack_int32(&ibuf, &bp, &remain); checksum->checksum_type = (krb5_cksumtype) ibuf; diff --git a/src/lib/krb5/krb/ser_ctx.c b/src/lib/krb5/krb/ser_ctx.c index 5854123ea..322f1825b 100644 --- a/src/lib/krb5/krb/ser_ctx.c +++ b/src/lib/krb5/krb/ser_ctx.c @@ -343,10 +343,9 @@ krb5_context_internalize(krb5_context kcontext, krb5_pointer *argp, krb5_octet * return (EINVAL); /* Get memory for the context */ - context = (krb5_context) malloc(sizeof(struct _krb5_context)); + context = (krb5_context) calloc(1, sizeof(struct _krb5_context)); if (!context) return (ENOMEM); - memset(context, 0, sizeof(struct _krb5_context)); /* Get the size of the default realm */ if ((kret = krb5_ser_unpack_int32(&ibuf, &bp, &remain))) @@ -372,15 +371,13 @@ krb5_context_internalize(krb5_context kcontext, krb5_pointer *argp, krb5_octet * goto cleanup; context->in_tkt_ktype_count = (int) ibuf; - context->in_tkt_ktypes = (krb5_enctype *) malloc(sizeof(krb5_enctype) * - (context->in_tkt_ktype_count+1)); + context->in_tkt_ktypes = (krb5_enctype *) calloc(context->in_tkt_ktype_count+1, + sizeof(krb5_enctype)); if (!context->in_tkt_ktypes) { kret = ENOMEM; goto cleanup; } - memset(context->in_tkt_ktypes, 0, (sizeof(krb5_enctype) * - (context->in_tkt_ktype_count + 1))); - + for (i=0; iin_tkt_ktype_count; i++) { if ((kret = krb5_ser_unpack_int32(&ibuf, &bp, &remain))) goto cleanup; @@ -392,14 +389,12 @@ krb5_context_internalize(krb5_context kcontext, krb5_pointer *argp, krb5_octet * goto cleanup; context->tgs_ktype_count = (int) ibuf; - context->tgs_ktypes = (krb5_enctype *) malloc(sizeof(krb5_enctype) * - (context->tgs_ktype_count+1)); + context->tgs_ktypes = (krb5_enctype *) calloc(context->tgs_ktype_count+1, + sizeof(krb5_enctype)); if (!context->tgs_ktypes) { kret = ENOMEM; goto cleanup; } - memset(context->tgs_ktypes, 0, (sizeof(krb5_enctype) * - (context->tgs_ktype_count + 1))); for (i=0; itgs_ktype_count; i++) { if ((kret = krb5_ser_unpack_int32(&ibuf, &bp, &remain))) goto cleanup; @@ -576,9 +571,8 @@ krb5_oscontext_internalize(krb5_context kcontext, krb5_pointer *argp, krb5_octet /* Get memory for the context */ if ((os_ctx = (krb5_os_context) - malloc(sizeof(struct _krb5_os_context))) && + calloc(1, sizeof(struct _krb5_os_context))) && (remain >= 4*sizeof(krb5_int32))) { - memset(os_ctx, 0, sizeof(struct _krb5_os_context)); os_ctx->magic = KV5M_OS_CONTEXT; /* Read out our context */ diff --git a/src/lib/krb5/krb/ser_eblk.c b/src/lib/krb5/krb/ser_eblk.c index aed806ce1..8bce41cf1 100644 --- a/src/lib/krb5/krb/ser_eblk.c +++ b/src/lib/krb5/krb/ser_eblk.c @@ -1,7 +1,7 @@ /* * lib/krb5/krb/ser_eblk.c * - * Copyright 1995 by the Massachusetts Institute of Technology. + * Copyright 1995, 2008 by the Massachusetts Institute of Technology. * All Rights Reserved. * * Export of this software from the United States of America may @@ -192,9 +192,7 @@ krb5_encrypt_block_internalize(kcontext, argp, buffer, lenremain) /* Get an encrypt_block */ if ((remain >= (3*sizeof(krb5_int32))) && (encrypt_block = (krb5_encrypt_block *) - malloc(sizeof(krb5_encrypt_block)))) { - memset(encrypt_block, 0, sizeof(krb5_encrypt_block)); - + calloc(1, sizeof(krb5_encrypt_block)))) { /* Get the enctype */ (void) krb5_ser_unpack_int32(&ibuf, &bp, &remain); ktype = (krb5_enctype) ibuf; diff --git a/src/lib/krb5/krb/ser_key.c b/src/lib/krb5/krb/ser_key.c index 5249006fc..25522de7b 100644 --- a/src/lib/krb5/krb/ser_key.c +++ b/src/lib/krb5/krb/ser_key.c @@ -1,7 +1,7 @@ /* * lib/krb5/krb/ser_key.c * - * Copyright 1995 by the Massachusetts Institute of Technology. + * Copyright 1995, 2008 by the Massachusetts Institute of Technology. * All Rights Reserved. * * Export of this software from the United States of America may @@ -153,9 +153,7 @@ krb5_keyblock_internalize(krb5_context kcontext, krb5_pointer *argp, krb5_octet /* Get a keyblock */ if ((remain >= (3*sizeof(krb5_int32))) && - (keyblock = (krb5_keyblock *) malloc(sizeof(krb5_keyblock)))) { - memset(keyblock, 0, sizeof(krb5_keyblock)); - + (keyblock = (krb5_keyblock *) calloc(1, sizeof(krb5_keyblock)))) { /* Get the enctype */ (void) krb5_ser_unpack_int32(&ibuf, &bp, &remain); keyblock->enctype = (krb5_enctype) ibuf; diff --git a/src/lib/krb5/os/localaddr.c b/src/lib/krb5/os/localaddr.c index 0ca3a3208..ce9674401 100644 --- a/src/lib/krb5/os/localaddr.c +++ b/src/lib/krb5/os/localaddr.c @@ -408,10 +408,9 @@ get_linux_ipv6_addrs () continue; } #endif - nw = malloc (sizeof (struct linux_ipv6_addr_list)); + nw = calloc (1, sizeof (struct linux_ipv6_addr_list)); if (nw == 0) continue; - memset (nw, 0, sizeof (*nw)); nw->addr.sin6_addr = a6; nw->addr.sin6_family = AF_INET6; /* Ignore other fields, we don't actually use them here. */ @@ -1528,16 +1527,13 @@ krb5_os_localaddr (krb5_context context, krb5_address ***addr) { for (count = 0; hostrec->h_addr_list[count]; count++); - paddr = (krb5_address **)malloc(sizeof(krb5_address *) * (count+1)); + paddr = (krb5_address **)calloc(count+1, sizeof(krb5_address *)); if (!paddr) { err = ENOMEM; goto cleanup; } - memset(paddr, 0, sizeof(krb5_address *) * (count+1)); - - for (i = 0; i < count; i++) - { + for (i = 0; i < count; i++) { paddr[i] = (krb5_address *)malloc(sizeof(krb5_address)); if (paddr[i] == NULL) { err = ENOMEM; diff --git a/src/lib/krb5/os/locate_kdc.c b/src/lib/krb5/os/locate_kdc.c index aedb14a88..4725bf4ab 100644 --- a/src/lib/krb5/os/locate_kdc.c +++ b/src/lib/krb5/os/locate_kdc.c @@ -586,12 +586,11 @@ module_callback (void *cbdata, int socktype, struct sockaddr *sa) #endif ) return 0; - x = malloc (sizeof (*x)); + x = calloc (1, sizeof (*x)); if (x == 0) { d->out_of_mem = 1; return 1; } - memset(x, 0, sizeof (*x)); x->ai.ai_addr = (struct sockaddr *) &x->u; x->ai.ai_socktype = socktype; x->ai.ai_family = sa->sa_family; diff --git a/src/lib/krb5/os/sendto_kdc.c b/src/lib/krb5/os/sendto_kdc.c index 48937fd99..658b42136 100644 --- a/src/lib/krb5/os/sendto_kdc.c +++ b/src/lib/krb5/os/sendto_kdc.c @@ -1207,20 +1207,16 @@ krb5int_sendto (krb5_context context, const krb5_data *message, reply->length = 0; n_conns = addrs->naddrs; - conns = malloc(n_conns * sizeof(struct conn_state)); + conns = calloc(n_conns, sizeof(struct conn_state)); if (conns == NULL) { return ENOMEM; } - memset(conns, 0, n_conns * sizeof(struct conn_state)); - if (callback_info) { - callback_data = malloc(n_conns * sizeof(krb5_data)); + callback_data = calloc(n_conns, sizeof(krb5_data)); if (callback_data == NULL) { return ENOMEM; } - - memset(callback_data, 0, n_conns * sizeof(krb5_data)); } for (i = 0; i < n_conns; i++) { diff --git a/src/lib/krb5/rcache/rc_dfl.c b/src/lib/krb5/rcache/rc_dfl.c index 63b923823..4b1c174b2 100644 --- a/src/lib/krb5/rcache/rc_dfl.c +++ b/src/lib/krb5/rcache/rc_dfl.c @@ -281,10 +281,9 @@ krb5_rc_dfl_resolve(krb5_context context, krb5_rcache id, char *name) krb5_error_code retval; /* allocate id? no */ - if (!(t = (struct dfl_data *) malloc(sizeof(struct dfl_data)))) + if (!(t = (struct dfl_data *) calloc(1, sizeof(struct dfl_data)))) return KRB5_RC_MALLOC; id->data = (krb5_pointer) t; - memset(t, 0, sizeof(struct dfl_data)); if (name) { t->name = malloc(strlen(name)+1); if (!t->name) { -- 2.26.2