diff options
author | Petri Lehtinen <petri@digip.org> | 2009-07-28 10:37:52 +0300 |
---|---|---|
committer | Petri Lehtinen <petri@digip.org> | 2009-07-28 10:38:21 +0300 |
commit | b348519e96f3414c6264748c2c63bc2a1c64642a (patch) | |
tree | 3b53db97ad4e2a01d0925d37b871f9e7f5a0feaa /src | |
parent | f60c563bab0388ee0ee74571ef0e2abf641685a8 (diff) | |
download | jansson-b348519e96f3414c6264748c2c63bc2a1c64642a.zip jansson-b348519e96f3414c6264748c2c63bc2a1c64642a.tar.gz jansson-b348519e96f3414c6264748c2c63bc2a1c64642a.tar.bz2 |
Fix some memory leaks and invalid memory references
Diffstat (limited to 'src')
-rw-r--r-- | src/dump.c | 7 | ||||
-rw-r--r-- | src/load.c | 22 |
2 files changed, 22 insertions, 7 deletions
@@ -227,11 +227,16 @@ static int do_dump(const json_t *json, uint32_t flags, int depth, int json_dump(const json_t *json, const char *path, uint32_t flags) { + int result; + FILE *output = fopen(path, "w"); if(!output) return -1; - return json_dumpf(json, output, flags); + result = json_dumpf(json, output, flags); + + fclose(output); + return result; } char *json_dumps(const json_t *json, uint32_t flags) @@ -156,11 +156,16 @@ static char stream_get(stream_t *stream, json_error_t *error) } } - return (char)stream->buffer[stream->buffer_pos++]; + return stream->buffer[stream->buffer_pos++]; out: error_set(error, NULL, "unable to decode byte 0x%x at position %d", (unsigned char)c, stream->stream_pos); + + stream->buffer[0] = EOF; + stream->buffer[1] = '\0'; + stream->buffer_pos = 1; + return EOF; } @@ -168,7 +173,7 @@ static void stream_unget(stream_t *stream, char c) { assert(stream->buffer_pos > 0); stream->buffer_pos--; - assert(stream->buffer[stream->buffer_pos] == (unsigned char)c); + assert(stream->buffer[stream->buffer_pos] == c); } @@ -197,8 +202,7 @@ static int lex_get_save(lex_t *lex, json_error_t *error) static void lex_unget_unsave(lex_t *lex, char c) { char d; - if(c != EOF) - stream_unget(&lex->stream, c); + stream_unget(&lex->stream, c); d = strbuffer_pop(&lex->saved_text); assert(c == d); } @@ -243,6 +247,7 @@ static void lex_scan_string(lex_t *lex, json_error_t *error) char *t; int i; + lex->value.string = NULL; lex->token = TOKEN_INVALID; /* skip the " */ @@ -384,9 +389,10 @@ static void lex_scan_string(lex_t *lex, json_error_t *error) } *t = '\0'; lex->token = TOKEN_STRING; + return; out: - return; + free(lex->value.string); } static void lex_scan_number(lex_t *lex, char c, json_error_t *error) @@ -547,6 +553,7 @@ static void lex_close(lex_t *lex) { if(lex->token == TOKEN_STRING) free(lex->value.string); + strbuffer_close(&lex->saved_text); } @@ -756,11 +763,14 @@ static int string_get(void *data) { char c; string_data_t *stream = (string_data_t *)data; - c = stream->data[stream->pos++]; + c = stream->data[stream->pos]; if(c == '\0') return EOF; else + { + stream->pos++; return c; + } } static int string_eof(void *data) |