From d7fba566b1a91340ffd19974ea79f463e429ad35 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Tue, 16 Dec 2008 16:19:36 -0800 Subject: [PATCH] Introduce x2nrealloc() helper function, and use it. Also fixes a bug in read_cert_file() in vconn-ssl.c: "sizeof *certs" should have been "sizeof **certs". In fact the sizes will be identical in all practical cases since both *certs and **certs are pointer types, so this is not an important fix. --- lib/fatal-signal.c | 3 +-- lib/util.c | 7 +++++++ lib/util.h | 1 + lib/vconn-ssl.c | 3 +-- secchan/secchan.c | 6 ++---- secchan/status.c | 8 +++----- 6 files changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/fatal-signal.c b/lib/fatal-signal.c index ac9e2a59..e4bc4577 100644 --- a/lib/fatal-signal.c +++ b/lib/fatal-signal.c @@ -201,8 +201,7 @@ fatal_signal_add_file_to_unlink(const char *file) fatal_signal_block(); if (n_files >= max_files) { - max_files = max_files * 2 + 1; - files = xrealloc(files, sizeof *files * max_files); + files = x2nrealloc(files, &max_files, sizeof *files); } files[n_files++] = xstrdup(file); fatal_signal_unblock(); diff --git a/lib/util.c b/lib/util.c index 72138a06..abf005d3 100644 --- a/lib/util.c +++ b/lib/util.c @@ -118,6 +118,13 @@ xvasprintf(const char *format, va_list args) return s; } +void * +x2nrealloc(void *p, size_t *n, size_t s) +{ + *n = *n == 0 ? 1 : 2 * *n; + return xrealloc(p, *n * s); +} + char * xasprintf(const char *format, ...) { diff --git a/lib/util.h b/lib/util.h index 8dee9b6d..8172b69e 100644 --- a/lib/util.h +++ b/lib/util.h @@ -102,6 +102,7 @@ char *xmemdup0(const char *, size_t); char *xstrdup(const char *); char *xasprintf(const char *format, ...) PRINTF_FORMAT(1, 2); char *xvasprintf(const char *format, va_list) PRINTF_FORMAT(1, 0); +void *x2nrealloc(void *p, size_t *n, size_t s); #ifndef HAVE_STRLCPY void strlcpy(char *dst, const char *src, size_t size); diff --git a/lib/vconn-ssl.c b/lib/vconn-ssl.c index 383714af..6d24e277 100644 --- a/lib/vconn-ssl.c +++ b/lib/vconn-ssl.c @@ -1087,8 +1087,7 @@ read_cert_file(const char *file_name, X509 ***certs, size_t *n_certs) /* Add certificate to array. */ if (*n_certs >= allocated_certs) { - allocated_certs = 1 + 2 * allocated_certs; - *certs = xrealloc(*certs, sizeof *certs * allocated_certs); + *certs = x2nrealloc(*certs, &allocated_certs, sizeof **certs); } (*certs)[(*n_certs)++] = certificate; diff --git a/secchan/secchan.c b/secchan/secchan.c index fa8d6e85..e2712cd1 100644 --- a/secchan/secchan.c +++ b/secchan/secchan.c @@ -313,10 +313,8 @@ add_hook(struct secchan *secchan, const struct hook_class *class, void *aux) struct hook *hook; if (secchan->n_hooks >= secchan->allocated_hooks) { - secchan->allocated_hooks = secchan->allocated_hooks * 2 + 1; - secchan->hooks = xrealloc(secchan->hooks, - (sizeof *secchan->hooks - * secchan->allocated_hooks)); + secchan->hooks = x2nrealloc(secchan->hooks, &secchan->allocated_hooks, + sizeof *secchan->hooks); } hook = &secchan->hooks[secchan->n_hooks++]; hook->class = class; diff --git a/secchan/status.c b/secchan/status.c index 8b6224c4..cf228123 100644 --- a/secchan/status.c +++ b/secchan/status.c @@ -58,7 +58,7 @@ struct switch_status { const struct settings *s; time_t booted; struct switch_status_category *categories; - int n_categories, allocated_categories; + size_t n_categories, allocated_categories; }; struct status_reply { @@ -195,10 +195,8 @@ switch_status_register_category(struct switch_status *ss, { struct switch_status_category *c; if (ss->n_categories >= ss->allocated_categories) { - ss->allocated_categories = 1 + ss->allocated_categories * 2; - ss->categories = xrealloc(ss->categories, - (sizeof *ss->categories - * ss->allocated_categories)); + ss->categories = x2nrealloc(ss->categories, &ss->allocated_categories, + sizeof *ss->categories); } c = &ss->categories[ss->n_categories++]; c->cb = cb; -- 2.30.2