diff --git a/geojson.c b/geojson.c index 4c4382f6..baefbe97 100644 --- a/geojson.c +++ b/geojson.c @@ -800,7 +800,7 @@ struct jsonmap { 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; if (jm->off + n >= jm->end) { diff --git a/jsonpull.c b/jsonpull.c index e56c48bc..cfbe3cfe 100644 --- a/jsonpull.c +++ b/jsonpull.c @@ -8,8 +8,12 @@ #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)); + if (j == NULL) { + perror("Out of memory"); + exit(EXIT_FAILURE); + } j->error = NULL; 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->source = source; - j->buffer = malloc(BUFFER); j->buffer_head = 0; j->buffer_tail = 0; + j->buffer = malloc(BUFFER); + if (j->buffer == NULL) { + perror("Out of memory"); + exit(EXIT_FAILURE); + } + 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); } @@ -59,7 +68,7 @@ json_pull *json_begin_file(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; 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) { json_object *o = malloc(sizeof(struct json_object)); + if (o == NULL) { + perror("Out of memory"); + exit(EXIT_FAILURE); + } o->type = type; o->parent = parent; o->array = NULL; @@ -113,6 +126,10 @@ static json_object *add_object(json_pull *j, json_type type) { if (c->expect == JSON_ITEM) { if (SIZE_FOR(c->length + 1) != SIZE_FOR(c->length)) { 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; @@ -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)) { 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 *)); + if (c->keys == NULL || c->values == NULL) { + perror("Out of memory"); + exit(EXIT_FAILURE); + } } c->keys[c->length] = o; @@ -160,7 +181,7 @@ json_object *json_hash_get(json_object *o, const char *s) { return NULL; } - int i; + size_t i; for (i = 0; i < o->length; i++) { if (o->keys[i] != NULL && o->keys[i]->type == JSON_STRING) { 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 { char *buf; - int n; - int nalloc; + size_t n; + size_t nalloc; }; static void string_init(struct string *s) { s->nalloc = 500; s->buf = malloc(s->nalloc); + if (s->buf == NULL) { + perror("Out of memory"); + exit(EXIT_FAILURE); + } s->n = 0; s->buf[0] = '\0'; } @@ -189,6 +214,10 @@ static void string_append(struct string *s, char c) { if (s->n + 2 >= s->nalloc) { s->nalloc += 500; s->buf = realloc(s->buf, s->nalloc); + if (s->buf == NULL) { + perror("Out of memory"); + exit(EXIT_FAILURE); + } } 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) { - int len = strlen(add); + size_t len = strlen(add); if (s->n + len + 1 >= s->nalloc) { s->nalloc += 500 + len; s->buf = realloc(s->buf, s->nalloc); + if (s->buf == NULL) { + perror("Out of memory"); + exit(EXIT_FAILURE); + } } for (; *add != '\0'; add++) { @@ -541,7 +574,7 @@ json_object *json_read_tree(json_pull *p) { } void json_free(json_object *o) { - int i; + size_t i; if (o == NULL) { return; @@ -551,7 +584,7 @@ void json_free(json_object *o) { if (o->type == JSON_ARRAY) { json_object **a = o->array; - int n = o->length; + size_t n = o->length; o->array = NULL; o->length = 0; @@ -564,7 +597,7 @@ void json_free(json_object *o) { } else if (o->type == JSON_HASH) { json_object **k = o->keys; json_object **v = o->values; - int n = o->length; + size_t n = o->length; o->keys = NULL; o->values = NULL; @@ -592,7 +625,7 @@ void json_disconnect(json_object *o) { if (o->parent != NULL) { if (o->parent->type == JSON_ARRAY) { - int i; + size_t i; for (i = 0; i < o->parent->length; i++) { if (o->parent->array[i] == o) { @@ -607,7 +640,7 @@ void json_disconnect(json_object *o) { } if (o->parent->type == JSON_HASH) { - int i; + size_t i; for (i = 0; i < o->parent->length; i++) { 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) { string_append(val, '{'); - int i; + size_t i; for (i = 0; i < o->length; i++) { json_print(val, o->keys[i]); string_append(val, ':'); @@ -695,7 +728,7 @@ static void json_print(struct string *val, json_object *o) { string_append(val, '}'); } else if (o->type == JSON_ARRAY) { string_append(val, '['); - int i; + size_t i; for (i = 0; i < o->length; i++) { json_print(val, o->array[i]); if (i + 1 < o->length) { diff --git a/jsonpull.h b/jsonpull.h index d5cc5aa7..9a715f2e 100644 --- a/jsonpull.h +++ b/jsonpull.h @@ -28,7 +28,7 @@ typedef struct json_object { struct json_object **array; struct json_object **keys; struct json_object **values; - int length; + size_t length; int expect; } json_object; @@ -37,11 +37,11 @@ typedef struct json_pull { char *error; int line; - int (*read)(struct json_pull *, char *buf, int n); + ssize_t (*read)(struct json_pull *, char *buf, size_t n); void *source; char *buffer; - int buffer_tail; - int buffer_head; + ssize_t buffer_tail; + ssize_t buffer_head; json_object *container; json_object *root; @@ -50,7 +50,7 @@ typedef struct json_pull { json_pull *json_begin_file(FILE *f); 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); typedef void (*json_separator_callback)(json_type type, json_pull *j, void *state);