diff --git a/libopenarc/arc-canon.c b/libopenarc/arc-canon.c index df2dbbd..0b1171a 100644 --- a/libopenarc/arc-canon.c +++ b/libopenarc/arc-canon.c @@ -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(); @@ -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++) @@ -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; @@ -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; @@ -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; diff --git a/libopenarc/arc-dns.c b/libopenarc/arc-dns.c index 268e904..bc21082 100644 --- a/libopenarc/arc-dns.c +++ b/libopenarc/arc-dns.c @@ -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); diff --git a/libopenarc/arc-internal.h b/libopenarc/arc-internal.h index c750402..c6bc9f2 100644 --- a/libopenarc/arc-internal.h +++ b/libopenarc/arc-internal.h @@ -7,6 +7,7 @@ #define ARC_ARC_INTERNAL_H_ /* libopenarc includes */ +#include "arc-malloc.h" #include "arc.h" #ifndef MAXPATHLEN @@ -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. */ diff --git a/libopenarc/arc.c b/libopenarc/arc.c index 45cf30b..8fb8a6f 100644 --- a/libopenarc/arc.c +++ b/libopenarc/arc.c @@ -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; @@ -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; @@ -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; @@ -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) { @@ -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) @@ -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) { @@ -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) @@ -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; @@ -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", @@ -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) { diff --git a/openarc/config.c b/openarc/config.c index d8eed8f..aaf7468 100644 --- a/openarc/config.c +++ b/openarc/config.c @@ -34,7 +34,8 @@ #include #endif /* USE_STRL_H */ -/* opendkim includes */ +/* openarc includes */ +#include "arc-malloc.h" #include "config.h" /* limits */ @@ -78,7 +79,7 @@ config_getline(FILE *in) assert(in != NULL); - new = malloc(asize); + new = ARC_MALLOC(asize); if (new == NULL) { return NULL; @@ -96,7 +97,7 @@ config_getline(FILE *in) { if (len == 0) { - free(new); + ARC_FREE(new); new = NULL; } break; @@ -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; } @@ -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 { @@ -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] = ','; @@ -381,7 +382,7 @@ config_load_level(char *file, } else { - free(buf); + ARC_FREE(buf); continue; /* blank line */ } @@ -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); @@ -432,7 +433,7 @@ config_load_level(char *file, fclose(in); } - free(buf); + ARC_FREE(buf); return NULL; } @@ -456,7 +457,7 @@ config_load_level(char *file, fclose(in); } - free(buf); + ARC_FREE(buf); return NULL; } @@ -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: @@ -487,7 +488,7 @@ config_load_level(char *file, cur = new; - free(buf); + ARC_FREE(buf); } conf_error = CONF_SUCCESS; @@ -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; @@ -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; } } diff --git a/openarc/openarc-crypto.c b/openarc/openarc-crypto.c index 88f6693..72cc8b7 100644 --- a/openarc/openarc-crypto.c +++ b/openarc/openarc-crypto.c @@ -87,7 +87,7 @@ arcf_crypto_get_id(void) id = pthread_getspecific(id_key); if (id == NULL) { - id = (unsigned long *) malloc(sizeof *id); + id = ARC_MALLOC(sizeof *id); assert(pthread_mutex_lock(&id_lock) == 0); threadid++; *id = threadid; @@ -127,7 +127,7 @@ arcf_crypto_free_id(void *ptr) ERR_remove_state(0); - free(ptr); + ARC_FREE(ptr); /* now we can actually clear it for real */ assert(pthread_setspecific(id_key, NULL) == 0); @@ -152,7 +152,7 @@ arcf_crypto_dyn_create(/* UNUSED */ const char *file, int err; pthread_mutex_t *new; - new = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t)); + new = ARC_MALLOC(sizeof(pthread_mutex_t)); if (new == NULL) { return NULL; @@ -161,7 +161,7 @@ arcf_crypto_dyn_create(/* UNUSED */ const char *file, err = pthread_mutex_init(new, NULL); if (err != 0) { - free(new); + ARC_FREE(new); return NULL; } @@ -189,7 +189,7 @@ arcf_crypto_dyn_destroy(struct CRYPTO_dynlock_value *lock, pthread_mutex_destroy((pthread_mutex_t *) lock); - free(lock); + ARC_FREE(lock); } /* @@ -246,7 +246,7 @@ arcf_crypto_init(void) int status; n = CRYPTO_num_locks(); - mutexes = (pthread_mutex_t *) malloc(n * sizeof(pthread_mutex_t)); + mutexes = ARC_CALLOC(n, sizeof(pthread_mutex_t)); if (mutexes == NULL) { return errno; @@ -327,7 +327,7 @@ arcf_crypto_free(void) pthread_mutex_destroy(&mutexes[c]); } - free(mutexes); + ARC_FREE(mutexes); mutexes = NULL; nmutexes = 0; } diff --git a/openarc/openarc-test.c b/openarc/openarc-test.c index 6268dae..e160db8 100644 --- a/openarc/openarc-test.c +++ b/openarc/openarc-test.c @@ -338,7 +338,7 @@ arcf_test_getsymval(void *ctx, char *sym) snprintf(symout, sizeof symout, "DEBUG-%s", sym); - return strdup(symout); + return ARC_STRDUP(symout); } /* @@ -669,7 +669,7 @@ arcf_testfiles(ARC_LIB *libopenarc, char *flist, int verbose) tverbose = verbose; /* set up a fake SMFICTX */ - tctx = (struct test_context *) malloc(sizeof(struct test_context)); + tctx = ARC_MALLOC(sizeof(struct test_context)); if (tctx == NULL) { fprintf(stderr, "%s: malloc(): %s\n", progname, strerror(errno)); diff --git a/openarc/openarc.c b/openarc/openarc.c index caa092e..dbf27b5 100644 --- a/openarc/openarc.c +++ b/openarc/openarc.c @@ -24,10 +24,6 @@ #include #endif /* __linux__ */ #include -#ifdef USE_LUA -#include -#include -#endif /* USE_LUA */ #ifdef AF_INET6 #include #endif /* AF_INET6 */ @@ -266,22 +262,12 @@ pthread_mutex_t pwdb_lock; /* passwd/group lock */ char myhostname[MAXHOSTNAMELEN + 1]; /* local host's name */ /* Other useful definitions */ -#define CRLF "\r\n" /* CRLF */ -#define SUPERUSER "root" /* superuser name */ +#define CRLF "\r\n" /* CRLF */ +#define SUPERUSER "root" /* superuser name */ /* MACROS */ -#define BITSET(b, s) (((b) & (s)) == (b)) -#define JOBID(x) ((x) == NULL ? JOBIDUNKNOWN : (char *) (x)) -#define TRYFREE(x) \ - do \ - { \ - if ((x) != NULL) \ - { \ - free(x); \ - (x) = NULL; \ - } \ - } \ - while (0) +#define BITSET(b, s) (((b) & (s)) == (b)) +#define JOBID(x) ((x) == NULL ? JOBIDUNKNOWN : (char *) (x)) #define ARCF_EOHMACROS "i {daemon_name} {auth_type}" /* @@ -626,7 +612,7 @@ arcf_restart_check(int n, time_t t) if (t == 0) { - list = calloc(n, sizeof(time_t)); + list = ARC_CALLOC(n, sizeof(time_t)); if (list == NULL) { @@ -1194,13 +1180,12 @@ arcf_config_new(void) { struct arcf_config *new; - new = (struct arcf_config *) malloc(sizeof(struct arcf_config)); + new = ARC_CALLOC(1, sizeof(struct arcf_config)); if (new == NULL) { return NULL; } - memset(new, '\0', sizeof(struct arcf_config)); new->conf_maxhdrsz = DEFMAXHDRSZ; new->conf_overridecv = true; new->conf_safekeys = true; @@ -1251,19 +1236,19 @@ arcf_list_load(struct conflist *list, char *path, char **err) } } - v = malloc(sizeof(struct configvalue)); + v = ARC_MALLOC(sizeof(struct configvalue)); if (v == NULL) { *err = strerror(errno); fclose(f); return false; } - v->value = strdup(buf); + v->value = ARC_STRDUP(buf); if (v->value == NULL) { *err = strerror(errno); fclose(f); - free(v); + ARC_FREE(v); return false; } @@ -1291,13 +1276,13 @@ arcf_addlist(struct conflist *list, char *str, char **err) { struct configvalue *v; - v = (struct configvalue *) malloc(sizeof(struct configvalue)); + v = ARC_MALLOC(sizeof(struct configvalue)); if (v == NULL) { *err = strerror(errno); return false; } - v->value = strdup(str); + v->value = ARC_STRDUP(str); LIST_INSERT_HEAD(list, v, entries); return true; @@ -1322,8 +1307,8 @@ arcf_list_destroy(struct conflist *list) n = LIST_FIRST(list); LIST_REMOVE(n, entries); - free(n->value); - free(n); + ARC_FREE(n->value); + ARC_FREE(n); } } @@ -1352,7 +1337,7 @@ arcf_config_free(struct arcf_config *conf) if (conf->conf_authservid != NULL) { - free(conf->conf_authservid); + ARC_FREE(conf->conf_authservid); } if (!LIST_EMPTY(&conf->conf_peers)) @@ -1372,12 +1357,12 @@ arcf_config_free(struct arcf_config *conf) if (conf->conf_signhdrs != NULL) { - free(conf->conf_signhdrs); + ARC_FREE(conf->conf_signhdrs); } if (conf->conf_oversignhdrs != NULL) { - free(conf->conf_oversignhdrs); + ARC_FREE(conf->conf_oversignhdrs); } if (!LIST_EMPTY(&conf->conf_sealheaderchecks)) @@ -1385,7 +1370,7 @@ arcf_config_free(struct arcf_config *conf) arcf_list_destroy(&conf->conf_sealheaderchecks); } - free(conf); + ARC_FREE(conf); } /* @@ -1431,11 +1416,11 @@ arcf_config_load(struct config *data, } if (str == NULL || strcmp(str, "HOSTNAME") == 0) { - conf->conf_authservid = strdup(myhostname); + conf->conf_authservid = ARC_STRDUP(myhostname); } else { - conf->conf_authservid = strdup(str); + conf->conf_authservid = ARC_STRDUP(str); } if (data != NULL) @@ -1474,7 +1459,7 @@ arcf_config_load(struct config *data, char *mode; char *ctx; - copy = strdup(str); + copy = ARC_STRDUP(str); mode = strtok_r(copy, "/", &ctx); conf->conf_canonhdr = arcf_lookup_strtoint(mode, arcf_canonicalizations); @@ -1489,7 +1474,7 @@ arcf_config_load(struct config *data, conf->conf_canonbody = ARC_CANON_SIMPLE; } - free(copy); + ARC_FREE(copy); if (conf->conf_canonhdr == -1 || conf->conf_canonbody == -1) { @@ -1787,7 +1772,7 @@ arcf_config_load(struct config *data, } } - s33krit = malloc(s.st_size + 1); + s33krit = ARC_MALLOC(s.st_size + 1); if (s33krit == NULL) { if (conf->conf_dolog) @@ -1825,7 +1810,7 @@ arcf_config_load(struct config *data, snprintf(err, errlen, "%s: read(): %s", conf->conf_keyfile, strerror(errno)); close(fd); - free(s33krit); + ARC_FREE(s33krit); return -1; } else if (rlen != s.st_size) @@ -1839,7 +1824,7 @@ arcf_config_load(struct config *data, snprintf(err, errlen, "%s: read() wrong size (%lu)", conf->conf_keyfile, (u_long) rlen); close(fd); - free(s33krit); + ARC_FREE(s33krit); return -1; } @@ -2192,14 +2177,12 @@ arcf_initcontext(struct arcf_config *conf) assert(conf != NULL); - ctx = (msgctx) malloc(sizeof(struct msgctx)); + ctx = ARC_CALLOC(1, sizeof(struct msgctx)); if (ctx == NULL) { return NULL; } - (void) memset(ctx, '\0', sizeof(struct msgctx)); - return ctx; } @@ -2241,11 +2224,11 @@ arcf_cleanup(SMFICTX *ctx) hdr = afc->mctx_hqhead; while (hdr != NULL) { - TRYFREE(hdr->hdr_hdr); - TRYFREE(hdr->hdr_val); + ARC_FREE(hdr->hdr_hdr); + ARC_FREE(hdr->hdr_val); prev = hdr; hdr = hdr->hdr_next; - TRYFREE(prev); + ARC_FREE(prev); } } @@ -2260,7 +2243,7 @@ arcf_cleanup(SMFICTX *ctx) vbr_close(afc->mctx_vbr); } - TRYFREE(afc->mctx_vbrinfo); + ARC_FREE(afc->mctx_vbrinfo); #endif /* _FFR_VBR */ if (afc->mctx_tmpstr != NULL) @@ -2279,37 +2262,14 @@ arcf_cleanup(SMFICTX *ctx) { next = cur->se_next; - free(cur); + ARC_FREE(cur); cur = next; } } #endif /* _FFR_STATSEXT */ -#ifdef USE_LUA - if (afc->mctx_luaglobalh != NULL) - { - struct lua_global *cur; - struct lua_global *next; - - cur = afc->mctx_luaglobalh; - while (cur != NULL) - { - next = cur->lg_next; - - if (cur->lg_type == LUA_TNUMBER || cur->lg_type == LUA_TSTRING) - { - free(cur->lg_value); - } - - free(cur); - - cur = next; - } - } -#endif /* USE_LUA */ - - free(afc); + ARC_FREE(afc); cc->cctx_msg = NULL; } } @@ -2763,7 +2723,7 @@ mlfi_negotiate(SMFICTX *ctx, arcf_config_reload(); /* initialize connection context */ - cc = malloc(sizeof(struct connctx)); + cc = ARC_CALLOC(1, sizeof(struct connctx)); if (cc == NULL) { if (curconf->conf_dolog) @@ -2774,8 +2734,6 @@ mlfi_negotiate(SMFICTX *ctx, return SMFIS_TEMPFAIL; } - memset(cc, '\0', sizeof(struct connctx)); - pthread_mutex_lock(&conf_lock); cc->cctx_config = curconf; @@ -2799,7 +2757,7 @@ mlfi_negotiate(SMFICTX *ctx, conf->conf_refcnt--; pthread_mutex_unlock(&conf_lock); - free(cc); + ARC_FREE(cc); return SMFIS_REJECT; } @@ -2863,7 +2821,7 @@ mlfi_connect(SMFICTX *ctx, char *host, _SOCK_ADDR *ip) cc = arcf_getpriv(ctx); if (cc == NULL) { - cc = malloc(sizeof(struct connctx)); + cc = ARC_CALLOC(1, sizeof(struct connctx)); if (cc == NULL) { pthread_mutex_lock(&conf_lock); @@ -2879,8 +2837,6 @@ mlfi_connect(SMFICTX *ctx, char *host, _SOCK_ADDR *ip) return SMFIS_TEMPFAIL; } - memset(cc, '\0', sizeof(struct connctx)); - pthread_mutex_lock(&conf_lock); cc->cctx_config = curconf; @@ -3113,7 +3069,7 @@ mlfi_header(SMFICTX *ctx, char *headerf, char *headerv) return SMFIS_CONTINUE; } - newhdr = (Header) malloc(sizeof(struct Header)); + newhdr = ARC_CALLOC(1, sizeof(struct Header)); if (newhdr == NULL) { if (conf->conf_dolog) @@ -3125,9 +3081,7 @@ mlfi_header(SMFICTX *ctx, char *headerf, char *headerv) return SMFIS_TEMPFAIL; } - (void) memset(newhdr, '\0', sizeof(struct Header)); - - newhdr->hdr_hdr = strdup(headerf); + newhdr->hdr_hdr = ARC_STRDUP(headerf); if (afc->mctx_tmpstr == NULL) { @@ -3139,8 +3093,8 @@ mlfi_header(SMFICTX *ctx, char *headerf, char *headerv) syslog(LOG_ERR, "arc_dstring_new() failed"); } - TRYFREE(newhdr->hdr_hdr); - free(newhdr); + ARC_FREE(newhdr->hdr_hdr); + ARC_FREE(newhdr); arcf_cleanup(ctx); @@ -3193,7 +3147,7 @@ mlfi_header(SMFICTX *ctx, char *headerf, char *headerv) arc_dstring_copy(afc->mctx_tmpstr, headerv); } - newhdr->hdr_val = strdup(arc_dstring_get(afc->mctx_tmpstr)); + newhdr->hdr_val = ARC_STRDUP(arc_dstring_get(afc->mctx_tmpstr)); newhdr->hdr_next = NULL; newhdr->hdr_prev = afc->mctx_hqtail; @@ -3205,9 +3159,9 @@ mlfi_header(SMFICTX *ctx, char *headerf, char *headerv) syslog(LOG_ERR, "malloc(): %s", strerror(errno)); } - TRYFREE(newhdr->hdr_hdr); - TRYFREE(newhdr->hdr_val); - TRYFREE(newhdr); + ARC_FREE(newhdr->hdr_hdr); + ARC_FREE(newhdr->hdr_val); + ARC_FREE(newhdr); arcf_cleanup(ctx); return SMFIS_TEMPFAIL; } @@ -4073,7 +4027,7 @@ mlfi_close(SMFICTX *ctx) pthread_mutex_unlock(&conf_lock); - free(cc); + ARC_FREE(cc); arcf_setpriv(ctx, NULL); } diff --git a/openarc/openarc.h b/openarc/openarc.h index bc0306a..062dfa5 100644 --- a/openarc/openarc.h +++ b/openarc/openarc.h @@ -24,6 +24,7 @@ #endif /* ARCF_MILTER_PROTOTYPES */ /* libopenarc */ +#include "arc-malloc.h" #include "arc.h" /* libjansson */ diff --git a/openarc/util.c b/openarc/util.c index c5574df..7c073b6 100644 --- a/openarc/util.c +++ b/openarc/util.c @@ -319,7 +319,7 @@ arcf_mkarray(char *in) } } - out = (char **) malloc((n + 1) * sizeof(char *)); + out = ARC_CALLOC(n + 1, sizeof(char *)); if (out == NULL) { return (const char **) NULL; diff --git a/util/arc-dstring.c b/util/arc-dstring.c index bf458cf..4ab34a1 100644 --- a/util/arc-dstring.c +++ b/util/arc-dstring.c @@ -21,6 +21,7 @@ #include #include "arc-dstring.h" +#include "arc-malloc.h" /* ** ARC_DSTRING_RESIZE -- resize a dynamic string (dstring) @@ -84,7 +85,7 @@ arc_dstring_resize(struct arc_dstring *dstr, int len) } } - new = malloc(newsz); + new = ARC_MALLOC(newsz); if (new == NULL) { if (dstr->ds_cb) @@ -95,7 +96,7 @@ arc_dstring_resize(struct arc_dstring *dstr, int len) } memcpy(new, dstr->ds_buf, dstr->ds_alloc); - free(dstr->ds_buf); + ARC_FREE(dstr->ds_buf); dstr->ds_alloc = newsz; dstr->ds_buf = new; @@ -133,7 +134,7 @@ arc_dstring_new(int len, len = 1024; } - new = malloc(sizeof *new); + new = ARC_MALLOC(sizeof *new); if (new == NULL) { if (callback) @@ -145,14 +146,14 @@ arc_dstring_new(int len, new->ds_ctx = ctx; new->ds_cb = callback; - new->ds_buf = malloc(len); + new->ds_buf = ARC_MALLOC(len); if (new->ds_buf == NULL) { if (callback) { callback(ctx, "unable to allocate %d bytes", sizeof len); } - free(new); + ARC_FREE(new); return NULL; } @@ -182,8 +183,8 @@ arc_dstring_free(struct arc_dstring *dstr) return; } - free(dstr->ds_buf); - free(dstr); + ARC_FREE(dstr->ds_buf); + ARC_FREE(dstr); } /* @@ -567,7 +568,7 @@ arc_copy_array(char **in) continue; } - out = calloc(sizeof(char *), n + 1); + out = ARC_CALLOC(sizeof(char *), n + 1); if (out == NULL) { return NULL; @@ -575,14 +576,14 @@ arc_copy_array(char **in) for (c = 0; c < n; c++) { - out[c] = strdup(in[c]); + out[c] = ARC_STRDUP(in[c]); if (out[c] == NULL) { for (n = 0; n < c; n++) { - free(out[n]); + ARC_FREE(out[n]); } - free(out); + ARC_FREE(out); return NULL; } } @@ -611,8 +612,8 @@ arc_clobber_array(char **in) for (n = 0; in[n] != NULL; n++) { - free(in[n]); + ARC_FREE(in[n]); } - free(in); + ARC_FREE(in); } diff --git a/util/arc-malloc.h b/util/arc-malloc.h new file mode 100644 index 0000000..414d680 --- /dev/null +++ b/util/arc-malloc.h @@ -0,0 +1,16 @@ +/* + * Copyright 2024 OpenARC contributors. + * + * See LICENSE for distribution terms. + */ + +#ifndef ARC_MALLOC_H +#define ARC_MALLOC_H + +#define ARC_FREE free +#define ARC_MALLOC malloc +#define ARC_CALLOC calloc +#define ARC_REALLOC realloc +#define ARC_STRDUP strdup + +#endif /* ARC_MALLOC_H */