Don't leak the non-GeoJSON objects in the JSON parse tree

This commit is contained in:
Eric Fischer 2016-04-26 17:11:22 -07:00
parent 60318e664e
commit 39c180a673
2 changed files with 11 additions and 4 deletions

View File

@ -86,6 +86,7 @@ json_pull *json_begin_string(char *s) {
}
void json_end(json_pull *p) {
json_free(p->root);
free(p->buffer);
free(p);
}
@ -102,7 +103,7 @@ static inline int read_wrap(json_pull *j) {
#define SIZE_FOR(i, size) ((size_t)((((i) + 31) & ~31) * size))
static json_object *fabricate_object(json_object *parent, json_type type) {
static json_object *fabricate_object(json_pull *jp, json_object *parent, json_type type) {
json_object *o = malloc(sizeof(struct json_object));
if (o == NULL) {
perror("Out of memory");
@ -114,12 +115,13 @@ static json_object *fabricate_object(json_object *parent, json_type type) {
o->keys = NULL;
o->values = NULL;
o->length = 0;
o->parser = jp;
return o;
}
static json_object *add_object(json_pull *j, json_type type) {
json_object *c = j->container;
json_object *o = fabricate_object(c, type);
json_object *o = fabricate_object(j, c, type);
if (c != NULL) {
if (c->type == JSON_ARRAY) {
@ -662,11 +664,11 @@ void json_disconnect(json_object *o) {
for (i = 0; i < o->parent->length; i++) {
if (o->parent->keys[i] == o) {
o->parent->keys[i] = fabricate_object(o->parent, JSON_NULL);
o->parent->keys[i] = fabricate_object(o->parser, o->parent, JSON_NULL);
break;
}
if (o->parent->values[i] == o) {
o->parent->values[i] = fabricate_object(o->parent, JSON_NULL);
o->parent->values[i] = fabricate_object(o->parser, o->parent, JSON_NULL);
break;
}
}
@ -686,6 +688,10 @@ void json_disconnect(json_object *o) {
}
}
if (o->parser != NULL && o->parser->root == o) {
o->parser->root = NULL;
}
o->parent = NULL;
}

View File

@ -21,6 +21,7 @@ typedef enum json_type {
typedef struct json_object {
json_type type;
struct json_object *parent;
struct json_pull *parser;
char *string;
double number;