Fix some memory leaks and invalid memory references

This commit is contained in:
Petri Lehtinen 2009-07-28 10:37:52 +03:00
parent f60c563bab
commit b348519e96
3 changed files with 27 additions and 7 deletions

View File

@ -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 json_dump(const json_t *json, const char *path, uint32_t flags)
{ {
int result;
FILE *output = fopen(path, "w"); FILE *output = fopen(path, "w");
if(!output) if(!output)
return -1; 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) char *json_dumps(const json_t *json, uint32_t flags)

View File

@ -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: out:
error_set(error, NULL, "unable to decode byte 0x%x at position %d", error_set(error, NULL, "unable to decode byte 0x%x at position %d",
(unsigned char)c, stream->stream_pos); (unsigned char)c, stream->stream_pos);
stream->buffer[0] = EOF;
stream->buffer[1] = '\0';
stream->buffer_pos = 1;
return EOF; return EOF;
} }
@ -168,7 +173,7 @@ static void stream_unget(stream_t *stream, char c)
{ {
assert(stream->buffer_pos > 0); assert(stream->buffer_pos > 0);
stream->buffer_pos--; stream->buffer_pos--;
assert(stream->buffer[stream->buffer_pos] == (unsigned char)c); assert(stream->buffer[stream->buffer_pos] == c);
} }
@ -197,7 +202,6 @@ static int lex_get_save(lex_t *lex, json_error_t *error)
static void lex_unget_unsave(lex_t *lex, char c) static void lex_unget_unsave(lex_t *lex, char c)
{ {
char d; char d;
if(c != EOF)
stream_unget(&lex->stream, c); stream_unget(&lex->stream, c);
d = strbuffer_pop(&lex->saved_text); d = strbuffer_pop(&lex->saved_text);
assert(c == d); assert(c == d);
@ -243,6 +247,7 @@ static void lex_scan_string(lex_t *lex, json_error_t *error)
char *t; char *t;
int i; int i;
lex->value.string = NULL;
lex->token = TOKEN_INVALID; lex->token = TOKEN_INVALID;
/* skip the " */ /* skip the " */
@ -384,9 +389,10 @@ static void lex_scan_string(lex_t *lex, json_error_t *error)
} }
*t = '\0'; *t = '\0';
lex->token = TOKEN_STRING; lex->token = TOKEN_STRING;
return;
out: out:
return; free(lex->value.string);
} }
static void lex_scan_number(lex_t *lex, char c, json_error_t *error) 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) if(lex->token == TOKEN_STRING)
free(lex->value.string); free(lex->value.string);
strbuffer_close(&lex->saved_text);
} }
@ -756,12 +763,15 @@ static int string_get(void *data)
{ {
char c; char c;
string_data_t *stream = (string_data_t *)data; string_data_t *stream = (string_data_t *)data;
c = stream->data[stream->pos++]; c = stream->data[stream->pos];
if(c == '\0') if(c == '\0')
return EOF; return EOF;
else else
{
stream->pos++;
return c; return c;
} }
}
static int string_eof(void *data) static int string_eof(void *data)
{ {

View File

@ -177,3 +177,8 @@ invalid Unicode '\uDFAA'
==== ====
1 1
'[' or '{' expected near 'å' '[' or '{' expected near 'å'
==== ascii-unicode-identifier ====
====
1
'[' or '{' expected near 'a'