From eee596d5f505662b3a3d3daae50f4b1a2b5e63f6 Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Fri, 25 Mar 2016 11:57:33 -0700 Subject: [PATCH] Check return values from memory allocation --- geojson.c | 27 +++++++++++++++++++++++++++ mbtiles.c | 8 ++++++++ tile-join.cc | 50 ++++++++++++++++++++++++++++++++++++++++++++------ tile.cc | 11 ++++++++++- 4 files changed, 89 insertions(+), 7 deletions(-) diff --git a/geojson.c b/geojson.c index baefbe97..458b6feb 100644 --- a/geojson.c +++ b/geojson.c @@ -815,6 +815,10 @@ ssize_t json_map_read(struct json_pull *jp, char *buffer, size_t n) { struct json_pull *json_begin_map(char *map, long long len) { struct jsonmap *jm = malloc(sizeof(struct jsonmap)); + if (jm == NULL) { + perror("Out of memory"); + exit(EXIT_FAILURE); + } jm->map = map; jm->off = 0; @@ -1046,6 +1050,11 @@ void start_parsing(int fd, FILE *fp, long long offset, long long len, volatile i *is_parsing = 1; struct read_parallel_arg *rpa = malloc(sizeof(struct read_parallel_arg)); + if (rpa == NULL) { + perror("Out of memory"); + exit(EXIT_FAILURE); + } + rpa->fd = fd; rpa->fp = fp; rpa->offset = offset; @@ -1088,6 +1097,11 @@ int read_json(int argc, char **argv, char *fname, const char *layername, int max r->geomname = malloc(strlen(tmpdir) + strlen("/geom.XXXXXXXX") + 1); r->indexname = malloc(strlen(tmpdir) + strlen("/index.XXXXXXXX") + 1); + if (r->metaname == NULL || r->poolname == NULL || r->treename == NULL || r->geomname == NULL || r->indexname == NULL) { + perror("Out of memory"); + exit(EXIT_FAILURE); + } + sprintf(r->metaname, "%s%s", tmpdir, "/meta.XXXXXXXX"); sprintf(r->poolname, "%s%s", tmpdir, "/pool.XXXXXXXX"); sprintf(r->treename, "%s%s", tmpdir, "/tree.XXXXXXXX"); @@ -1162,6 +1176,10 @@ int read_json(int argc, char **argv, char *fname, const char *layername, int max } r->file_bbox = malloc(4 * sizeof(long long)); + if (r->file_bbox == NULL) { + perror("Out of memory"); + exit(EXIT_FAILURE); + } r->file_bbox[0] = r->file_bbox[1] = UINT_MAX; r->file_bbox[2] = r->file_bbox[3] = 0; } @@ -1364,6 +1382,10 @@ int read_json(int argc, char **argv, char *fname, const char *layername, int max for (i = 0; i < nlayers; i++) { if (layername != NULL) { layernames[i] = strdup(layername); + if (layernames[i] == NULL) { + perror("Out of memory"); + exit(EXIT_FAILURE); + } } else { char *src = argv[i]; if (argc < 1) { @@ -1371,6 +1393,11 @@ int read_json(int argc, char **argv, char *fname, const char *layername, int max } char *trunc = layernames[i] = malloc(strlen(src) + 1); + if (trunc == NULL) { + perror("Out of memory"); + exit(EXIT_FAILURE); + } + const char *ocp, *use = src; for (ocp = src; *ocp; ocp++) { if (*ocp == '/' && ocp[1] != '\0') { diff --git a/mbtiles.c b/mbtiles.c index 02a1a40a..d46f16ec 100644 --- a/mbtiles.c +++ b/mbtiles.c @@ -118,6 +118,10 @@ static void aprintf(char **buf, const char *format, ...) { va_end(ap); *buf = realloc(*buf, strlen(*buf) + strlen(tmp) + 1); + if (*buf == NULL) { + perror("Out of memory"); + exit(EXIT_FAILURE); + } strcat(*buf, tmp); free(tmp); } @@ -219,6 +223,10 @@ void mbtiles_write_metadata(sqlite3 *outdb, const char *fname, char **layername, sqlite3_free(sql); char *buf = strdup("{"); + if (buf == NULL) { + perror("Out of memory"); + exit(EXIT_FAILURE); + } aprintf(&buf, "\"vector_layers\": [ "); int i; diff --git a/tile-join.cc b/tile-join.cc index 36e183c0..73d6bfc0 100644 --- a/tile-join.cc +++ b/tile-join.cc @@ -141,6 +141,10 @@ void handle(std::string message, int z, unsigned x, unsigned y, struct pool **fi pool_init(&((*file_keys)[ll]), 0); (*layernames)[ll] = strdup(ln); + if ((*layernames)[ll] == NULL) { + perror("Out of memory"); + exit(EXIT_FAILURE); + } *nlayers = ll + 1; } @@ -161,6 +165,10 @@ void handle(std::string message, int z, unsigned x, unsigned y, struct pool **fi if (val.has_string_value()) { value = strdup(val.string_value().c_str()); + if (value == NULL) { + perror("Out of memory"); + exit(EXIT_FAILURE); + } type = VT_STRING; } else if (val.has_int_value()) { if (asprintf(&value, "%lld", (long long) val.int_value()) >= 0) { @@ -196,7 +204,12 @@ void handle(std::string message, int z, unsigned x, unsigned y, struct pool **fi if (!is_pooled(exclude, key, VT_STRING)) { if (!is_pooled(&((*file_keys)[ll]), key, type)) { - pool(&((*file_keys)[ll]), strdup(key), type); + char *copy = strdup(key); + if (copy == NULL) { + perror("Out of memory"); + exit(EXIT_FAILURE); + } + pool(&((*file_keys)[ll]), copy, type); } struct pool_val *k, *v; @@ -204,13 +217,23 @@ void handle(std::string message, int z, unsigned x, unsigned y, struct pool **fi if (is_pooled(&keys, key, VT_STRING)) { k = pool(&keys, key, VT_STRING); } else { - k = pool(&keys, strdup(key), VT_STRING); + char *copy = strdup(key); + if (copy == NULL) { + perror("Out of memory"); + exit(EXIT_FAILURE); + } + k = pool(&keys, copy, VT_STRING); } if (is_pooled(&values, value, type)) { v = pool(&values, value, type); } else { - v = pool(&values, strdup(value), type); + char *copy = strdup(value); + if (copy == NULL) { + perror("Out of memory"); + exit(EXIT_FAILURE); + } + v = pool(&values, copy, type); } feature_tags.push_back(k->n); @@ -242,7 +265,12 @@ void handle(std::string message, int z, unsigned x, unsigned y, struct pool **fi if (!is_pooled(exclude, sjoinkey, VT_STRING)) { if (!is_pooled(&((*file_keys)[ll]), sjoinkey, type)) { - pool(&((*file_keys)[ll]), strdup(sjoinkey), type); + char *copy = strdup(sjoinkey); + if (copy == NULL) { + perror("Out of memory"); + exit(EXIT_FAILURE); + } + pool(&((*file_keys)[ll]), copy, type); } struct pool_val *k, *v; @@ -250,13 +278,23 @@ void handle(std::string message, int z, unsigned x, unsigned y, struct pool **fi if (is_pooled(&keys, sjoinkey, VT_STRING)) { k = pool(&keys, sjoinkey, VT_STRING); } else { - k = pool(&keys, strdup(sjoinkey), VT_STRING); + char *copy = strdup(sjoinkey); + if (copy == NULL) { + perror("Out of memory"); + exit(EXIT_FAILURE); + } + k = pool(&keys, copy, VT_STRING); } if (is_pooled(&values, sjoinval, type)) { v = pool(&values, sjoinval, type); } else { - v = pool(&values, strdup(sjoinval), type); + char *copy = strdup(sjoinval); + if (copy == NULL) { + perror("Out of memory"); + exit(EXIT_FAILURE); + } + v = pool(&values, copy, type); } feature_tags.push_back(k->n); diff --git a/tile.cc b/tile.cc index 0838f425..90c53ee0 100644 --- a/tile.cc +++ b/tile.cc @@ -232,7 +232,12 @@ void decode_meta(char **meta, char *stringpool, struct pool *keys, struct pool * } // Dup to retain after munmap - pool(file_keys, strdup(key->s), value->type); + char *copy = strdup(key->s); + if (copy == NULL) { + perror("Out of memory"); + exit(EXIT_FAILURE); + } + pool(file_keys, copy, value->type); if (pthread_mutex_unlock(&var_lock) != 0) { perror("pthread_mutex_unlock"); @@ -1109,6 +1114,10 @@ void *run_thread(void *vargs) { if (len < 0) { int *err = (int *) malloc(sizeof(int)); + if (err == NULL) { + perror("Out of memory"); + exit(EXIT_FAILURE); + } *err = z - 1; return err; }