Skip to content

Commit

Permalink
Consistently use memory management defines
Browse files Browse the repository at this point in the history
* Move them to util so that the milter can use them as well.
* Use `calloc()` preferentially for allocations that need to be zeroed.
  • Loading branch information
flowerysong committed Oct 27, 2024
1 parent ecd1a97 commit 49af4d3
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 169 deletions.
15 changes: 5 additions & 10 deletions libopenarc/arc-canon.c
Original file line number Diff line number Diff line change
Expand Up @@ -565,14 +565,13 @@ arc_canon_init(ARC_MESSAGE *msg, bool tmp, bool keep)
return ARC_STAT_NORESOURCE;
}

cur->canon_hash = ARC_MALLOC(sizeof(struct arc_hash));
cur->canon_hash = ARC_CALLOC(1, sizeof(struct arc_hash));
if (cur->canon_hash == NULL)
{
arc_error(msg, "unable to allocate %d bytes",
sizeof(struct arc_hash));
return ARC_STAT_NORESOURCE;
}
memset(cur->canon_hash, '\0', sizeof(struct arc_hash));

#if OPENSSL_VERSION_NUMBER < 0x10100000L
cur->canon_hash->hash_ctx = EVP_MD_CTX_create();
Expand Down Expand Up @@ -842,13 +841,11 @@ arc_canon_selecthdrs(ARC_MESSAGE *msg,
hdr->hdr_flags &= ~ARC_HDR_SIGNED;
}

n = msg->arc_hdrcnt * sizeof(struct arc_hdrfield *);
lhdrs = ARC_MALLOC(n);
lhdrs = ARC_CALLOC(msg->arc_hdrcnt, sizeof(struct arc_hdrfield *));
if (lhdrs == NULL)
{
return -1;
}
memset(lhdrs, '\0', n);

shcnt = 1;
for (colon = msg->arc_hdrlist; *colon != '\0'; colon++)
Expand All @@ -858,14 +855,12 @@ arc_canon_selecthdrs(ARC_MESSAGE *msg,
shcnt++;
}
}
n = sizeof(unsigned char *) * shcnt;
hdrs = ARC_MALLOC(n);
hdrs = ARC_CALLOC(shcnt, sizeof(unsigned char *));
if (hdrs == NULL)
{
ARC_FREE(lhdrs);
return -1;
}
memset(hdrs, '\0', n);

n = 0;

Expand Down Expand Up @@ -1184,7 +1179,7 @@ arc_canon_runheaders(ARC_MESSAGE *msg)
bool signing;
unsigned char savechar;
int c;
int n;
size_t n;
int nhdrs = 0;
ARC_STAT status;
ARC_CANON *cur;
Expand All @@ -1200,7 +1195,7 @@ arc_canon_runheaders(ARC_MESSAGE *msg)
}

n = msg->arc_hdrcnt * sizeof(struct arc_hdrfield *);
hdrset = ARC_MALLOC(n);
hdrset = ARC_CALLOC(msg->arc_hdrcnt, sizeof(struct arc_hdrfield *));
if (hdrset == NULL)
{
return ARC_STAT_NORESOURCE;
Expand Down
4 changes: 1 addition & 3 deletions libopenarc/arc-dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,12 @@ arc_res_init(void **srv)
#ifdef HAVE_RES_NINIT
struct __res_state *res;

res = ARC_MALLOC(sizeof(struct __res_state));
res = ARC_CALLOC(1, sizeof(struct __res_state));
if (res == NULL)
{
return -1;
}

memset(res, '\0', sizeof(struct __res_state));

if (res_ninit(res) != 0)
{
ARC_FREE(res);
Expand Down
10 changes: 1 addition & 9 deletions libopenarc/arc-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define ARC_ARC_INTERNAL_H_

/* libopenarc includes */
#include "arc-malloc.h"
#include "arc.h"

#ifndef MAXPATHLEN
Expand Down Expand Up @@ -102,15 +103,6 @@ typedef struct arc_plist ARC_PLIST;
struct arc_canon;
typedef struct arc_canon ARC_CANON;

/*
** memory allocation wrappers
*/

#define ARC_FREE free
#define ARC_MALLOC malloc
#define ARC_REALLOC realloc
#define ARC_STRDUP strdup

/*
** ARC_ERROR_CB -- arc_error wrapper for use as a callback.
*/
Expand Down
28 changes: 10 additions & 18 deletions libopenarc/arc.c
Original file line number Diff line number Diff line change
Expand Up @@ -838,13 +838,12 @@ arc_init(void)
{
ARC_LIB *lib;

lib = ARC_MALLOC(sizeof *lib);
lib = ARC_CALLOC(1, sizeof *lib);
if (lib == NULL)
{
return lib;
}

memset(lib, '\0', sizeof *lib);
lib->arcl_minkeysize = ARC_DEFAULT_MINKEYSIZE;
lib->arcl_flags = ARC_LIBFLAGS_DEFAULT;

Expand All @@ -854,13 +853,12 @@ arc_init(void)
(lib)->arcl_flist[FEATURE_INDEX((x))] |= (1 << FEATURE_OFFSET(x))

lib->arcl_flsize = (FEATURE_INDEX(ARC_FEATURE_MAX)) + 1;
lib->arcl_flist = ARC_MALLOC(sizeof(unsigned int) * lib->arcl_flsize);
lib->arcl_flist = ARC_CALLOC(lib->arcl_flsize, sizeof(unsigned int));
if (lib->arcl_flist == NULL)
{
ARC_FREE(lib);
return NULL;
}
memset(lib->arcl_flist, '\0', sizeof(unsigned int) * lib->arcl_flsize);

lib->arcl_dns_callback = NULL;
lib->arcl_dns_service = NULL;
Expand Down Expand Up @@ -1545,14 +1543,13 @@ arc_process_set(ARC_MESSAGE *msg,
}
strlcpy((char *) hcopy, (char *) str, len + 1);

set = ARC_MALLOC(sizeof(ARC_KVSET));
set = ARC_CALLOC(1, sizeof(ARC_KVSET));
if (set == NULL)
{
ARC_FREE(hcopy);
arc_error(msg, "unable to allocate %d byte(s)", sizeof(ARC_KVSET));
return ARC_STAT_INTERNAL;
}
memset(set, '\0', sizeof(ARC_KVSET));

set->set_udata = data;
set->set_type = type;
Expand Down Expand Up @@ -2312,13 +2309,12 @@ arc_validate_msg(ARC_MESSAGE *msg, unsigned int setnum)

/* verify the signature's "bh" against our computed one */
b64bhlen = BASE64SIZE(bhlen);
b64bh = ARC_MALLOC(b64bhlen + 1);
b64bh = ARC_CALLOC(1, b64bhlen + 1);
if (b64bh == NULL)
{
arc_error(msg, "unable to allocate %d bytes", b64bhlen + 1);
return ARC_STAT_INTERNAL;
}
memset(b64bh, '\0', b64bhlen + 1);
elen = arc_base64_encode(bh, bhlen, b64bh, b64bhlen);
if (elen != strlen(b64bhtag) || strcmp((char *) b64bh, b64bhtag) != 0)
{
Expand Down Expand Up @@ -2440,7 +2436,7 @@ arc_message(ARC_LIB *lib,
return NULL;
}

msg = ARC_MALLOC(sizeof *msg);
msg = ARC_CALLOC(1, sizeof *msg);
if (msg == NULL)
{
if (err != NULL)
Expand All @@ -2450,8 +2446,6 @@ arc_message(ARC_LIB *lib,
return NULL;
}

memset(msg, '\0', sizeof *msg);

msg->arc_library = lib;
if (lib->arcl_fixedtime != 0)
{
Expand Down Expand Up @@ -2798,9 +2792,9 @@ arc_eoh_verify(ARC_MESSAGE *msg)
*/

/* sets already in the chain, validation */
msg->arc_sealcanons = ARC_MALLOC(msg->arc_nsets * sizeof(ARC_CANON *));
msg->arc_hdrcanons = ARC_MALLOC(msg->arc_nsets * sizeof(ARC_CANON *));
msg->arc_bodycanons = ARC_MALLOC(msg->arc_nsets * sizeof(ARC_CANON *));
msg->arc_sealcanons = ARC_CALLOC(msg->arc_nsets, sizeof(ARC_CANON *));
msg->arc_hdrcanons = ARC_CALLOC(msg->arc_nsets, sizeof(ARC_CANON *));
msg->arc_bodycanons = ARC_CALLOC(msg->arc_nsets, sizeof(ARC_CANON *));

if (msg->arc_sealcanons == NULL || msg->arc_hdrcanons == NULL ||
msg->arc_bodycanons == NULL)
Expand Down Expand Up @@ -3031,12 +3025,11 @@ arc_eoh(ARC_MESSAGE *msg)
/* build up the array of ARC sets, for use later */
if (nsets > 0)
{
msg->arc_sets = ARC_MALLOC(sizeof(struct arc_set) * nsets);
msg->arc_sets = ARC_CALLOC(nsets, sizeof(struct arc_set));
if (msg->arc_sets == NULL)
{
return ARC_STAT_NORESOURCE;
}
memset(msg->arc_sets, '\0', sizeof(struct arc_set) * nsets);
}

for (set = arc_set_first(msg, ARC_KVSETTYPE_ANY); set != NULL;
Expand Down Expand Up @@ -3611,7 +3604,7 @@ arc_getseal(ARC_MESSAGE *msg,
/* base64 encode it */
b64siglen = siglen * 3 + 5;
b64siglen += (b64siglen / 60);
b64sig = ARC_MALLOC(b64siglen);
b64sig = ARC_CALLOC(1, b64siglen);
if (b64sig == NULL)
{
arc_error(msg, "can't allocate %d bytes for base64 signature",
Expand All @@ -3620,7 +3613,6 @@ arc_getseal(ARC_MESSAGE *msg,
goto error;
}

memset(b64sig, '\0', b64siglen);
rstatus = arc_base64_encode(sigout, siglen, b64sig, b64siglen);
if (rstatus == -1)
{
Expand Down
35 changes: 18 additions & 17 deletions openarc/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
#include <strl.h>
#endif /* USE_STRL_H */

/* opendkim includes */
/* openarc includes */
#include "arc-malloc.h"
#include "config.h"

/* limits */
Expand Down Expand Up @@ -78,7 +79,7 @@ config_getline(FILE *in)

assert(in != NULL);

new = malloc(asize);
new = ARC_MALLOC(asize);
if (new == NULL)
{
return NULL;
Expand All @@ -96,7 +97,7 @@ config_getline(FILE *in)
{
if (len == 0)
{
free(new);
ARC_FREE(new);
new = NULL;
}
break;
Expand All @@ -108,10 +109,10 @@ config_getline(FILE *in)

asize += BUFRSZ;

newnew = realloc(new, asize);
newnew = ARC_REALLOC(new, asize);
if (newnew == NULL)
{
free(new);
ARC_FREE(new);
return NULL;
}

Expand Down Expand Up @@ -315,7 +316,7 @@ config_load_level(char *file,
}
else if (*deprecated == NULL)
{
*deprecated = strdup(def[n].cd_name);
*deprecated = ARC_STRDUP(def[n].cd_name);
}
else
{
Expand All @@ -325,7 +326,7 @@ config_load_level(char *file,

oldlen = strlen(*deprecated);
newlen = oldlen + 2 + strlen(def[n].cd_name);
new = realloc(*deprecated, newlen);
new = ARC_REALLOC(*deprecated, newlen);
if (new != NULL)
{
new[oldlen] = ',';
Expand Down Expand Up @@ -381,7 +382,7 @@ config_load_level(char *file,
}
else
{
free(buf);
ARC_FREE(buf);
continue; /* blank line */
}

Expand All @@ -404,14 +405,14 @@ config_load_level(char *file,
fclose(in);
}

free(buf);
ARC_FREE(buf);
return NULL;
}

if (def[n].cd_type != CONFIG_TYPE_INCLUDE &&
def[n].cd_type != CONFIG_TYPE_DEPRECATED)
{
new = (struct config *) malloc(sizeof(struct config));
new = ARC_MALLOC(sizeof(struct config));
if (new == NULL)
{
config_free(cur);
Expand All @@ -432,7 +433,7 @@ config_load_level(char *file,
fclose(in);
}

free(buf);
ARC_FREE(buf);
return NULL;
}

Expand All @@ -456,7 +457,7 @@ config_load_level(char *file,
fclose(in);
}

free(buf);
ARC_FREE(buf);
return NULL;
}

Expand All @@ -467,7 +468,7 @@ config_load_level(char *file,
}

case CONFIG_TYPE_STRING:
new->cfg_string = strdup(str);
new->cfg_string = ARC_STRDUP(str);
break;

case CONFIG_TYPE_BOOLEAN:
Expand All @@ -487,7 +488,7 @@ config_load_level(char *file,

cur = new;

free(buf);
ARC_FREE(buf);
}

conf_error = CONF_SUCCESS;
Expand All @@ -499,7 +500,7 @@ config_load_level(char *file,

if (myline == 0 || cur == NULL)
{
cur = (struct config *) malloc(sizeof *cur);
cur = ARC_MALLOC(sizeof *cur);
if (cur != NULL)
{
cur->cfg_bool = false;
Expand Down Expand Up @@ -599,9 +600,9 @@ config_free(struct config *head)
next = cur->cfg_next;
if (cur->cfg_type == CONFIG_TYPE_STRING && cur->cfg_string != NULL)
{
free(cur->cfg_string);
ARC_FREE(cur->cfg_string);
}
free(cur);
ARC_FREE(cur);
cur = next;
}
}
Expand Down
Loading

0 comments on commit 49af4d3

Please sign in to comment.