summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGlenn Strauss <gstrauss@gluelogic.com>2019-02-12 22:36:04 -0500
committerGlenn Strauss <gstrauss@gluelogic.com>2019-02-12 22:36:04 -0500
commitd28bac32fef755997b720f6f2cd5962cb1cb57d3 (patch)
tree9c1a456f73a08a8626c3b0c5de4a788ec6adfaa7
parent31b546e367b9eddb0d6998e03589a0daa55417ee (diff)
downloadlighttpd1.4-d28bac32fef755997b720f6f2cd5962cb1cb57d3.tar.gz
lighttpd1.4-d28bac32fef755997b720f6f2cd5962cb1cb57d3.zip
[multiple] reduce code dup in list resizing
reduce code duplication in list resizing realloc() of NULL ptr has behavior similar to malloc() Note that if initial size == 0, then code used to adjust size must be += x to ensure the size is non-zero for reallocation. (Multiplying 0 * x, e.g. power-2 resizing, will result in 0.)
-rw-r--r--src/array.c9
-rw-r--r--src/fdevent_poll.c12
-rw-r--r--src/gw_backend.c27
-rw-r--r--src/joblist.c12
-rw-r--r--src/keyvalue.c13
-rw-r--r--src/mod_accesslog.c18
-rw-r--r--src/mod_cgi.c6
-rw-r--r--src/mod_dirlisting.c11
-rw-r--r--src/mod_magnet_cache.c5
-rw-r--r--src/mod_uploadprogress.c8
-rw-r--r--src/mod_webdav.c5
-rw-r--r--src/network.c8
-rw-r--r--src/plugin.c7
13 files changed, 22 insertions, 119 deletions
diff --git a/src/array.c b/src/array.c
index 9abaeada..4331b67b 100644
--- a/src/array.c
+++ b/src/array.c
@@ -275,14 +275,7 @@ static data_unset **array_find_or_insert(array *a, data_unset *entry) {
/* there couldn't possibly be enough memory to store so many entries */
force_assert(a->used + 1 <= SSIZE_MAX);
- if (a->size == 0) {
- a->size = 16;
- a->data = malloc(sizeof(*a->data) * a->size);
- a->sorted = malloc(sizeof(*a->sorted) * a->size);
- force_assert(a->data);
- force_assert(a->sorted);
- for (j = a->used; j < a->size; j++) a->data[j] = NULL;
- } else if (a->size == a->used) {
+ if (a->size == a->used) {
a->size += 16;
a->data = realloc(a->data, sizeof(*a->data) * a->size);
a->sorted = realloc(a->sorted, sizeof(*a->sorted) * a->size);
diff --git a/src/fdevent_poll.c b/src/fdevent_poll.c
index 76e94139..c53b3935 100644
--- a/src/fdevent_poll.c
+++ b/src/fdevent_poll.c
@@ -45,11 +45,7 @@ static int fdevent_poll_event_del(fdevents *ev, int fde_ndx, int fd) {
/* ev->pollfds[k].events = 0; */
/* ev->pollfds[k].revents = 0; */
- if (ev->unused.size == 0) {
- ev->unused.size = 16;
- ev->unused.ptr = malloc(sizeof(*(ev->unused.ptr)) * ev->unused.size);
- force_assert(NULL != ev->unused.ptr);
- } else if (ev->unused.size == ev->unused.used) {
+ if (ev->unused.size == ev->unused.used) {
ev->unused.size += 16;
ev->unused.ptr = realloc(ev->unused.ptr, sizeof(*(ev->unused.ptr)) * ev->unused.size);
force_assert(NULL != ev->unused.ptr);
@@ -106,11 +102,7 @@ static int fdevent_poll_event_set(fdevents *ev, int fde_ndx, int fd, int events)
return k;
} else {
- if (ev->size == 0) {
- ev->size = 16;
- ev->pollfds = malloc(sizeof(*ev->pollfds) * ev->size);
- force_assert(NULL != ev->pollfds);
- } else if (ev->size == ev->used) {
+ if (ev->size == ev->used) {
ev->size += 16;
ev->pollfds = realloc(ev->pollfds, sizeof(*ev->pollfds) * ev->size);
force_assert(NULL != ev->pollfds);
diff --git a/src/gw_backend.c b/src/gw_backend.c
index 84ccc956..bf405fd5 100644
--- a/src/gw_backend.c
+++ b/src/gw_backend.c
@@ -200,11 +200,7 @@ static int gw_extension_insert(gw_exts *ext, buffer *key, gw_host *fh) {
fe->last_used_ndx = -1;
buffer_copy_buffer(fe->key, key);
- if (ext->size == 0) {
- ext->size = 8;
- ext->exts = malloc(ext->size * sizeof(*(ext->exts)));
- force_assert(ext->exts);
- } else if (ext->used == ext->size) {
+ if (ext->used == ext->size) {
ext->size += 8;
ext->exts = realloc(ext->exts, ext->size * sizeof(*(ext->exts)));
force_assert(ext->exts);
@@ -435,11 +431,7 @@ static int env_add(char_array *env, const char *key, size_t key_len, const char
}
}
- if (env->size == 0) {
- env->size = 16;
- env->ptr = malloc(env->size * sizeof(*env->ptr));
- force_assert(env->ptr);
- } else if (env->size == env->used + 1) {
+ if (env->size <= env->used + 1) {
env->size += 16;
env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
force_assert(env->ptr);
@@ -738,10 +730,7 @@ static int parse_binpath(char_array *env, buffer *b) {
case '\t':
/* a WS, stop here and copy the argument */
- if (env->size == 0) {
- env->size = 16;
- env->ptr = malloc(env->size * sizeof(*env->ptr));
- } else if (env->size == env->used) {
+ if (env->size == env->used) {
env->size += 16;
env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
}
@@ -758,10 +747,7 @@ static int parse_binpath(char_array *env, buffer *b) {
}
}
- if (env->size == 0) {
- env->size = 16;
- env->ptr = malloc(env->size * sizeof(*env->ptr));
- } else if (env->size == env->used) { /*need one extra for terminating NULL*/
+ if (env->size == env->used) { /*need one extra for terminating NULL*/
env->size += 16;
env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
}
@@ -769,10 +755,7 @@ static int parse_binpath(char_array *env, buffer *b) {
/* the rest */
env->ptr[env->used++] = strdup(start);
- if (env->size == 0) {
- env->size = 16;
- env->ptr = malloc(env->size * sizeof(*env->ptr));
- } else if (env->size == env->used) { /*need one extra for terminating NULL*/
+ if (env->size == env->used) { /*need one extra for terminating NULL*/
env->size += 16;
env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
}
diff --git a/src/joblist.c b/src/joblist.c
index 7c210247..5a137a9e 100644
--- a/src/joblist.c
+++ b/src/joblist.c
@@ -7,11 +7,7 @@
#include <string.h>
int joblist_append(server *srv, connection *con) {
- if (srv->joblist->size == 0) {
- srv->joblist->size = 16;
- srv->joblist->ptr = malloc(sizeof(*srv->joblist->ptr) * srv->joblist->size);
- force_assert(NULL != srv->joblist->ptr);
- } else if (srv->joblist->used == srv->joblist->size) {
+ if (srv->joblist->used == srv->joblist->size) {
srv->joblist->size += 16;
srv->joblist->ptr = realloc(srv->joblist->ptr, sizeof(*srv->joblist->ptr) * srv->joblist->size);
force_assert(NULL != srv->joblist->ptr);
@@ -44,11 +40,7 @@ connection *fdwaitqueue_unshift(server *srv, connections *fdwaitqueue) {
}
int fdwaitqueue_append(server *srv, connection *con) {
- if (srv->fdwaitqueue->size == 0) {
- srv->fdwaitqueue->size = 16;
- srv->fdwaitqueue->ptr = malloc(sizeof(*(srv->fdwaitqueue->ptr)) * srv->fdwaitqueue->size);
- force_assert(NULL != srv->fdwaitqueue->ptr);
- } else if (srv->fdwaitqueue->used == srv->fdwaitqueue->size) {
+ if (srv->fdwaitqueue->used == srv->fdwaitqueue->size) {
srv->fdwaitqueue->size += 16;
srv->fdwaitqueue->ptr = realloc(srv->fdwaitqueue->ptr, sizeof(*(srv->fdwaitqueue->ptr)) * srv->fdwaitqueue->size);
force_assert(NULL != srv->fdwaitqueue->ptr);
diff --git a/src/keyvalue.c b/src/keyvalue.c
index c1b6b019..90351d7a 100644
--- a/src/keyvalue.c
+++ b/src/keyvalue.c
@@ -38,18 +38,7 @@ int pcre_keyvalue_buffer_append(server *srv, pcre_keyvalue_buffer *kvb, buffer *
if (!key) return -1;
- if (kvb->size == 0) {
- kvb->size = 4;
- kvb->used = 0;
-
- kvb->kv = malloc(kvb->size * sizeof(*kvb->kv));
- force_assert(NULL != kvb->kv);
-
- for(i = 0; i < kvb->size; i++) {
- kvb->kv[i] = calloc(1, sizeof(**kvb->kv));
- force_assert(NULL != kvb->kv[i]);
- }
- } else if (kvb->used == kvb->size) {
+ if (kvb->used == kvb->size) {
kvb->size += 4;
kvb->kv = realloc(kvb->kv, kvb->size * sizeof(*kvb->kv));
diff --git a/src/mod_accesslog.c b/src/mod_accesslog.c
index 753cdb4b..3cc24a72 100644
--- a/src/mod_accesslog.c
+++ b/src/mod_accesslog.c
@@ -257,11 +257,7 @@ static int accesslog_parse_format(server *srv, format_fields *fields, buffer *fo
case '%':
if (i > 0 && start != i) {
/* copy the string before this % */
- if (fields->size == 0) {
- fields->size = 16;
- fields->used = 0;
- fields->ptr = malloc(fields->size * sizeof(format_field * ));
- } else if (fields->used == fields->size) {
+ if (fields->used == fields->size) {
fields->size += 16;
fields->ptr = realloc(fields->ptr, fields->size * sizeof(format_field * ));
}
@@ -277,11 +273,7 @@ static int accesslog_parse_format(server *srv, format_fields *fields, buffer *fo
/* we need a new field */
- if (fields->size == 0) {
- fields->size = 16;
- fields->used = 0;
- fields->ptr = malloc(fields->size * sizeof(format_field * ));
- } else if (fields->used == fields->size) {
+ if (fields->used == fields->size) {
fields->size += 16;
fields->ptr = realloc(fields->ptr, fields->size * sizeof(format_field * ));
}
@@ -412,11 +404,7 @@ static int accesslog_parse_format(server *srv, format_fields *fields, buffer *fo
if (start < i) {
/* copy the string */
- if (fields->size == 0) {
- fields->size = 16;
- fields->used = 0;
- fields->ptr = malloc(fields->size * sizeof(format_field * ));
- } else if (fields->used == fields->size) {
+ if (fields->used == fields->size) {
fields->size += 16;
fields->ptr = realloc(fields->ptr, fields->size * sizeof(format_field * ));
}
diff --git a/src/mod_cgi.c b/src/mod_cgi.c
index d39277d2..39045a9b 100644
--- a/src/mod_cgi.c
+++ b/src/mod_cgi.c
@@ -249,11 +249,7 @@ SETDEFAULTS_FUNC(mod_fastcgi_set_defaults) {
static void cgi_pid_add(plugin_data *p, pid_t pid, void *ctx) {
buffer_pid_t *r = &(p->cgi_pid);
- if (r->size == 0) {
- r->size = 16;
- r->ptr = malloc(sizeof(*r->ptr) * r->size);
- force_assert(r->ptr);
- } else if (r->used == r->size) {
+ if (r->used == r->size) {
r->size += 16;
r->ptr = realloc(r->ptr, sizeof(*r->ptr) * r->size);
force_assert(r->ptr);
diff --git a/src/mod_dirlisting.c b/src/mod_dirlisting.c
index 2f746740..0144a9bd 100644
--- a/src/mod_dirlisting.c
+++ b/src/mod_dirlisting.c
@@ -95,16 +95,7 @@ static int excludes_buffer_append(excludes_buffer *exb, buffer *string) {
if (!string) return -1;
- if (exb->size == 0) {
- exb->size = 4;
- exb->used = 0;
-
- exb->ptr = malloc(exb->size * sizeof(*exb->ptr));
-
- for(i = 0; i < exb->size ; i++) {
- exb->ptr[i] = calloc(1, sizeof(**exb->ptr));
- }
- } else if (exb->used == exb->size) {
+ if (exb->used == exb->size) {
exb->size += 4;
exb->ptr = realloc(exb->ptr, exb->size * sizeof(*exb->ptr));
diff --git a/src/mod_magnet_cache.c b/src/mod_magnet_cache.c
index 204bb8e1..9f09cca2 100644
--- a/src/mod_magnet_cache.c
+++ b/src/mod_magnet_cache.c
@@ -96,10 +96,7 @@ lua_State *script_cache_get_script(server *srv, connection *con, script_cache *c
if (sc == NULL) {
sc = script_init();
- if (cache->size == 0) {
- cache->size = 16;
- cache->ptr = malloc(cache->size * sizeof(*(cache->ptr)));
- } else if (cache->used == cache->size) {
+ if (cache->used == cache->size) {
cache->size += 16;
cache->ptr = realloc(cache->ptr, cache->size * sizeof(*(cache->ptr)));
}
diff --git a/src/mod_uploadprogress.c b/src/mod_uploadprogress.c
index 3dbd61e1..bf2a20e4 100644
--- a/src/mod_uploadprogress.c
+++ b/src/mod_uploadprogress.c
@@ -78,13 +78,7 @@ static int connection_map_insert(connection_map *cm, connection *con, const char
connection_map_entry *cme;
size_t i;
- if (cm->size == 0) {
- cm->size = 16;
- cm->ptr = malloc(cm->size * sizeof(*(cm->ptr)));
- for (i = 0; i < cm->size; i++) {
- cm->ptr[i] = NULL;
- }
- } else if (cm->used == cm->size) {
+ if (cm->used == cm->size) {
cm->size += 16;
cm->ptr = realloc(cm->ptr, cm->size * sizeof(*(cm->ptr)));
for (i = cm->used; i < cm->size; i++) {
diff --git a/src/mod_webdav.c b/src/mod_webdav.c
index 67153e3c..990560da 100644
--- a/src/mod_webdav.c
+++ b/src/mod_webdav.c
@@ -1391,10 +1391,7 @@ static handler_t mod_webdav_propfind(server *srv, connection *con, plugin_data *
}
/* add property to requested list */
- if (req_props->size == 0) {
- req_props->size = 16;
- req_props->ptr = malloc(sizeof(*(req_props->ptr)) * req_props->size);
- } else if (req_props->used == req_props->size) {
+ if (req_props->used == req_props->size) {
req_props->size += 16;
req_props->ptr = realloc(req_props->ptr, sizeof(*(req_props->ptr)) * req_props->size);
}
diff --git a/src/network.c b/src/network.c
index 016d0d5b..811d7e90 100644
--- a/src/network.c
+++ b/src/network.c
@@ -124,16 +124,12 @@ static int network_host_parse_addr(server *srv, sock_addr *addr, socklen_t *addr
}
static void network_srv_sockets_append(server *srv, server_socket *srv_socket) {
- if (srv->srv_sockets.size == 0) {
- srv->srv_sockets.size = 4;
- srv->srv_sockets.used = 0;
- srv->srv_sockets.ptr = malloc(srv->srv_sockets.size * sizeof(server_socket*));
- force_assert(NULL != srv->srv_sockets.ptr);
- } else if (srv->srv_sockets.used == srv->srv_sockets.size) {
+ if (srv->srv_sockets.used == srv->srv_sockets.size) {
srv->srv_sockets.size += 4;
srv->srv_sockets.ptr = realloc(srv->srv_sockets.ptr, srv->srv_sockets.size * sizeof(server_socket*));
force_assert(NULL != srv->srv_sockets.ptr);
}
+
srv->srv_sockets.ptr[srv->srv_sockets.used++] = srv_socket;
}
diff --git a/src/plugin.c b/src/plugin.c
index a509e4e6..d88bd6bf 100644
--- a/src/plugin.c
+++ b/src/plugin.c
@@ -88,12 +88,7 @@ static void plugin_free(plugin *p) {
static int plugins_register(server *srv, plugin *p) {
plugin **ps;
- if (0 == srv->plugins.size) {
- srv->plugins.size = 4;
- srv->plugins.ptr = malloc(srv->plugins.size * sizeof(*ps));
- force_assert(NULL != srv->plugins.ptr);
- srv->plugins.used = 0;
- } else if (srv->plugins.used == srv->plugins.size) {
+ if (srv->plugins.used == srv->plugins.size) {
srv->plugins.size += 4;
srv->plugins.ptr = realloc(srv->plugins.ptr, srv->plugins.size * sizeof(*ps));
force_assert(NULL != srv->plugins.ptr);