Check return values after allocating memory

This commit is contained in:
Eric Fischer 2016-03-25 11:20:56 -07:00
parent eebba7b75b
commit bc5a7b251f
3 changed files with 54 additions and 21 deletions

View File

@ -800,7 +800,7 @@ struct jsonmap {
long long end; long long end;
}; };
int json_map_read(struct json_pull *jp, char *buffer, int n) { ssize_t json_map_read(struct json_pull *jp, char *buffer, size_t n) {
struct jsonmap *jm = jp->source; struct jsonmap *jm = jp->source;
if (jm->off + n >= jm->end) { if (jm->off + n >= jm->end) {

View File

@ -8,8 +8,12 @@
#define BUFFER 10000 #define BUFFER 10000
json_pull *json_begin(int (*read)(struct json_pull *, char *buffer, int n), void *source) { json_pull *json_begin(ssize_t (*read)(struct json_pull *, char *buffer, size_t n), void *source) {
json_pull *j = malloc(sizeof(json_pull)); json_pull *j = malloc(sizeof(json_pull));
if (j == NULL) {
perror("Out of memory");
exit(EXIT_FAILURE);
}
j->error = NULL; j->error = NULL;
j->line = 1; j->line = 1;
@ -18,10 +22,15 @@ json_pull *json_begin(int (*read)(struct json_pull *, char *buffer, int n), void
j->read = read; j->read = read;
j->source = source; j->source = source;
j->buffer = malloc(BUFFER);
j->buffer_head = 0; j->buffer_head = 0;
j->buffer_tail = 0; j->buffer_tail = 0;
j->buffer = malloc(BUFFER);
if (j->buffer == NULL) {
perror("Out of memory");
exit(EXIT_FAILURE);
}
return j; return j;
} }
@ -51,7 +60,7 @@ static inline int next(json_pull *j) {
} }
} }
static int read_file(json_pull *j, char *buffer, int n) { static ssize_t read_file(json_pull *j, char *buffer, size_t n) {
return fread(buffer, 1, n, j->source); return fread(buffer, 1, n, j->source);
} }
@ -59,7 +68,7 @@ json_pull *json_begin_file(FILE *f) {
return json_begin(read_file, f); return json_begin(read_file, f);
} }
static int read_string(json_pull *j, char *buffer, int n) { static ssize_t read_string(json_pull *j, char *buffer, size_t n) {
char *cp = j->source; char *cp = j->source;
int out = 0; int out = 0;
@ -95,6 +104,10 @@ static inline int read_wrap(json_pull *j) {
static json_object *fabricate_object(json_object *parent, json_type type) { static json_object *fabricate_object(json_object *parent, json_type type) {
json_object *o = malloc(sizeof(struct json_object)); json_object *o = malloc(sizeof(struct json_object));
if (o == NULL) {
perror("Out of memory");
exit(EXIT_FAILURE);
}
o->type = type; o->type = type;
o->parent = parent; o->parent = parent;
o->array = NULL; o->array = NULL;
@ -113,6 +126,10 @@ static json_object *add_object(json_pull *j, json_type type) {
if (c->expect == JSON_ITEM) { if (c->expect == JSON_ITEM) {
if (SIZE_FOR(c->length + 1) != SIZE_FOR(c->length)) { if (SIZE_FOR(c->length + 1) != SIZE_FOR(c->length)) {
c->array = realloc(c->array, SIZE_FOR(c->length + 1) * sizeof(json_object *)); c->array = realloc(c->array, SIZE_FOR(c->length + 1) * sizeof(json_object *));
if (c->array == NULL) {
perror("Out of memory");
exit(EXIT_FAILURE);
}
} }
c->array[c->length++] = o; c->array[c->length++] = o;
@ -136,6 +153,10 @@ static json_object *add_object(json_pull *j, json_type type) {
if (SIZE_FOR(c->length + 1) != SIZE_FOR(c->length)) { if (SIZE_FOR(c->length + 1) != SIZE_FOR(c->length)) {
c->keys = realloc(c->keys, SIZE_FOR(c->length + 1) * sizeof(json_object *)); c->keys = realloc(c->keys, SIZE_FOR(c->length + 1) * sizeof(json_object *));
c->values = realloc(c->values, SIZE_FOR(c->length + 1) * sizeof(json_object *)); c->values = realloc(c->values, SIZE_FOR(c->length + 1) * sizeof(json_object *));
if (c->keys == NULL || c->values == NULL) {
perror("Out of memory");
exit(EXIT_FAILURE);
}
} }
c->keys[c->length] = o; c->keys[c->length] = o;
@ -160,7 +181,7 @@ json_object *json_hash_get(json_object *o, const char *s) {
return NULL; return NULL;
} }
int i; size_t i;
for (i = 0; i < o->length; i++) { for (i = 0; i < o->length; i++) {
if (o->keys[i] != NULL && o->keys[i]->type == JSON_STRING) { if (o->keys[i] != NULL && o->keys[i]->type == JSON_STRING) {
if (strcmp(o->keys[i]->string, s) == 0) { if (strcmp(o->keys[i]->string, s) == 0) {
@ -174,13 +195,17 @@ json_object *json_hash_get(json_object *o, const char *s) {
struct string { struct string {
char *buf; char *buf;
int n; size_t n;
int nalloc; size_t nalloc;
}; };
static void string_init(struct string *s) { static void string_init(struct string *s) {
s->nalloc = 500; s->nalloc = 500;
s->buf = malloc(s->nalloc); s->buf = malloc(s->nalloc);
if (s->buf == NULL) {
perror("Out of memory");
exit(EXIT_FAILURE);
}
s->n = 0; s->n = 0;
s->buf[0] = '\0'; s->buf[0] = '\0';
} }
@ -189,6 +214,10 @@ static void string_append(struct string *s, char c) {
if (s->n + 2 >= s->nalloc) { if (s->n + 2 >= s->nalloc) {
s->nalloc += 500; s->nalloc += 500;
s->buf = realloc(s->buf, s->nalloc); s->buf = realloc(s->buf, s->nalloc);
if (s->buf == NULL) {
perror("Out of memory");
exit(EXIT_FAILURE);
}
} }
s->buf[s->n++] = c; s->buf[s->n++] = c;
@ -196,11 +225,15 @@ static void string_append(struct string *s, char c) {
} }
static void string_append_string(struct string *s, char *add) { static void string_append_string(struct string *s, char *add) {
int len = strlen(add); size_t len = strlen(add);
if (s->n + len + 1 >= s->nalloc) { if (s->n + len + 1 >= s->nalloc) {
s->nalloc += 500 + len; s->nalloc += 500 + len;
s->buf = realloc(s->buf, s->nalloc); s->buf = realloc(s->buf, s->nalloc);
if (s->buf == NULL) {
perror("Out of memory");
exit(EXIT_FAILURE);
}
} }
for (; *add != '\0'; add++) { for (; *add != '\0'; add++) {
@ -541,7 +574,7 @@ json_object *json_read_tree(json_pull *p) {
} }
void json_free(json_object *o) { void json_free(json_object *o) {
int i; size_t i;
if (o == NULL) { if (o == NULL) {
return; return;
@ -551,7 +584,7 @@ void json_free(json_object *o) {
if (o->type == JSON_ARRAY) { if (o->type == JSON_ARRAY) {
json_object **a = o->array; json_object **a = o->array;
int n = o->length; size_t n = o->length;
o->array = NULL; o->array = NULL;
o->length = 0; o->length = 0;
@ -564,7 +597,7 @@ void json_free(json_object *o) {
} else if (o->type == JSON_HASH) { } else if (o->type == JSON_HASH) {
json_object **k = o->keys; json_object **k = o->keys;
json_object **v = o->values; json_object **v = o->values;
int n = o->length; size_t n = o->length;
o->keys = NULL; o->keys = NULL;
o->values = NULL; o->values = NULL;
@ -592,7 +625,7 @@ void json_disconnect(json_object *o) {
if (o->parent != NULL) { if (o->parent != NULL) {
if (o->parent->type == JSON_ARRAY) { if (o->parent->type == JSON_ARRAY) {
int i; size_t i;
for (i = 0; i < o->parent->length; i++) { for (i = 0; i < o->parent->length; i++) {
if (o->parent->array[i] == o) { if (o->parent->array[i] == o) {
@ -607,7 +640,7 @@ void json_disconnect(json_object *o) {
} }
if (o->parent->type == JSON_HASH) { if (o->parent->type == JSON_HASH) {
int i; size_t i;
for (i = 0; i < o->parent->length; i++) { for (i = 0; i < o->parent->length; i++) {
if (o->parent->keys[i] == o) { if (o->parent->keys[i] == o) {
@ -683,7 +716,7 @@ static void json_print(struct string *val, json_object *o) {
} else if (o->type == JSON_HASH) { } else if (o->type == JSON_HASH) {
string_append(val, '{'); string_append(val, '{');
int i; size_t i;
for (i = 0; i < o->length; i++) { for (i = 0; i < o->length; i++) {
json_print(val, o->keys[i]); json_print(val, o->keys[i]);
string_append(val, ':'); string_append(val, ':');
@ -695,7 +728,7 @@ static void json_print(struct string *val, json_object *o) {
string_append(val, '}'); string_append(val, '}');
} else if (o->type == JSON_ARRAY) { } else if (o->type == JSON_ARRAY) {
string_append(val, '['); string_append(val, '[');
int i; size_t i;
for (i = 0; i < o->length; i++) { for (i = 0; i < o->length; i++) {
json_print(val, o->array[i]); json_print(val, o->array[i]);
if (i + 1 < o->length) { if (i + 1 < o->length) {

View File

@ -28,7 +28,7 @@ typedef struct json_object {
struct json_object **array; struct json_object **array;
struct json_object **keys; struct json_object **keys;
struct json_object **values; struct json_object **values;
int length; size_t length;
int expect; int expect;
} json_object; } json_object;
@ -37,11 +37,11 @@ typedef struct json_pull {
char *error; char *error;
int line; int line;
int (*read)(struct json_pull *, char *buf, int n); ssize_t (*read)(struct json_pull *, char *buf, size_t n);
void *source; void *source;
char *buffer; char *buffer;
int buffer_tail; ssize_t buffer_tail;
int buffer_head; ssize_t buffer_head;
json_object *container; json_object *container;
json_object *root; json_object *root;
@ -50,7 +50,7 @@ typedef struct json_pull {
json_pull *json_begin_file(FILE *f); json_pull *json_begin_file(FILE *f);
json_pull *json_begin_string(char *s); json_pull *json_begin_string(char *s);
json_pull *json_begin(int (*read)(struct json_pull *, char *buffer, int n), void *source); json_pull *json_begin(ssize_t (*read)(struct json_pull *, char *buffer, size_t n), void *source);
void json_end(json_pull *p); void json_end(json_pull *p);
typedef void (*json_separator_callback)(json_type type, json_pull *j, void *state); typedef void (*json_separator_callback)(json_type type, json_pull *j, void *state);