From 05f95f41abb5e801b08b59cd0c846ce2d31ab32b Mon Sep 17 00:00:00 2001 From: kristapsdz Date: Fri, 17 Jul 2020 13:14:44 +0000 Subject: [PATCH] Remove the XREALLOC and XREALLOCARRAY functions. Plug a small memory leak when unable to reallocate kpairs. --- child.c | 6 +++--- extern.h | 9 ++------- kcgi.c | 11 +++++------ parent.c | 7 ++++++- wrappers.c | 5 ++--- 5 files changed, 18 insertions(+), 20 deletions(-) diff --git a/child.c b/child.c index ba0fa56..f76acc8 100644 --- a/child.c +++ b/child.c @@ -1692,7 +1692,7 @@ again: return NULL; } - if ((pp = XREALLOC(b->buf, b->sz + sz)) == NULL) { + if ((pp = kxrealloc(b->buf, b->sz + sz)) == NULL) { *er = KCGI_ENOMEM; return NULL; } @@ -1828,7 +1828,7 @@ kworker_fcgi_stdin(struct fcgi_buf *b, const struct fcgi_hdr *hdr, * FIXME: check for addition overflow. */ - ptr = XREALLOC(*sbp, *ssz + hdr->contentLength + 1); + ptr = kxrealloc(*sbp, *ssz + hdr->contentLength + 1); if (ptr == NULL) return KCGI_ENOMEM; @@ -1964,7 +1964,7 @@ kworker_fcgi_params(struct fcgi_buf *buf, const struct fcgi_hdr *hdr, */ if (i == *envsz) { - ptr = XREALLOCARRAY + ptr = kxreallocarray (*envs, *envsz + 1, sizeof(struct env)); if (ptr == NULL) diff --git a/extern.h b/extern.h index 998deca..8e38279 100644 --- a/extern.h +++ b/extern.h @@ -101,9 +101,8 @@ int kxvasprintf(const char *, int, char **, const char *, va_list); void *kxcalloc(const char *, int, size_t, size_t); void *kxmalloc(const char *, int, size_t); -void *kxrealloc(const char *, int, void *, size_t); -void *kxreallocarray(const char *, - int, void *, size_t, size_t); +void *kxrealloc(void *, size_t); +void *kxreallocarray(void *, size_t, size_t); char *kxstrdup(const char *); #define XASPRINTF(_p, ...) \ kxasprintf(__FILE__, __LINE__, (_p), __VA_ARGS__) @@ -113,10 +112,6 @@ char *kxstrdup(const char *); kxcalloc(__FILE__, __LINE__, (_nm), (_sz)) #define XMALLOC(_sz) \ kxmalloc(__FILE__, __LINE__, (_sz)) -#define XREALLOC(_p, _sz) \ - kxrealloc(__FILE__, __LINE__, (_p), (_sz)) -#define XREALLOCARRAY(_p, _nm, _sz) \ - kxreallocarray(__FILE__, __LINE__, (_p), (_nm), (_sz)) __END_DECLS diff --git a/kcgi.c b/kcgi.c index d494f15..c5c1c3d 100644 --- a/kcgi.c +++ b/kcgi.c @@ -334,7 +334,7 @@ krealloc(void *pp, size_t sz) { char *p; - if ((p = XREALLOC(pp, sz)) != NULL) + if ((p = kxrealloc(pp, sz)) != NULL) return p; exit(EXIT_FAILURE); } @@ -347,7 +347,7 @@ kreallocarray(void *pp, size_t nm, size_t sz) { char *p; - if ((p = XREALLOCARRAY(pp, nm, sz)) != NULL) + if ((p = kxreallocarray(pp, nm, sz)) != NULL) return p; exit(EXIT_FAILURE); } @@ -577,7 +577,7 @@ khttp_url_query_string(char *p, va_list ap) total += strlen(keyp) + strlen(valp) + 2; - if ((pp = XREALLOC(p, total)) == NULL) { + if ((pp = kxrealloc(p, total)) == NULL) { free(p); free(keyp); free(valp); @@ -657,7 +657,7 @@ khttp_url_query_stringx(char *p, va_list ap) total += strlen(keyp) + strlen(valp) + 2; - if ((pp = XREALLOC(p, total)) == NULL) { + if ((pp = kxrealloc(p, total)) == NULL) { free(p); free(keyp); free(valpp); @@ -1388,8 +1388,7 @@ kcgi_buf_write(const char *s, size_t sz, void *arg) if (b->sz + sz + 1 > b->maxsz) { b->maxsz = b->sz + sz + 1 + (b->growsz == 0 ? 1024 : b->growsz); - pp = XREALLOC(b->buf, b->maxsz); - if (pp == NULL) + if ((pp = kxrealloc(b->buf, b->maxsz)) == NULL) return KCGI_ENOMEM; b->buf = pp; } diff --git a/parent.c b/parent.c index 7027a2c..9640358 100644 --- a/parent.c +++ b/parent.c @@ -181,8 +181,13 @@ input(enum input *type, struct kpair *kp, int fd, static struct kpair * kpair_expand(struct kpair **kv, size_t *kvsz) { + void *pp; - *kv = XREALLOCARRAY(*kv, *kvsz + 1, sizeof(struct kpair)); + pp = kxreallocarray(*kv, *kvsz + 1, sizeof(struct kpair)); + if (pp == NULL) + return NULL; + + *kv = pp; memset(&(*kv)[*kvsz], 0, sizeof(struct kpair)); (*kvsz)++; return(&(*kv)[*kvsz - 1]); diff --git a/wrappers.c b/wrappers.c index 6772147..6e9de4b 100644 --- a/wrappers.c +++ b/wrappers.c @@ -93,7 +93,7 @@ kxmalloc(const char *file, int line, size_t sz) } void * -kxrealloc(const char *file, int line, void *pp, size_t sz) +kxrealloc(void *pp, size_t sz) { void *p; @@ -108,8 +108,7 @@ kxrealloc(const char *file, int line, void *pp, size_t sz) } void * -kxreallocarray(const char *file, - int line, void *pp, size_t nm, size_t sz) +kxreallocarray(void *pp, size_t nm, size_t sz) { void *p;