From f243930b6843295b81a4e4025497e40f19ff53ac Mon Sep 17 00:00:00 2001 From: Petri Lehtinen Date: Tue, 27 Oct 2009 17:46:57 +0200 Subject: [PATCH 1/2] json_load_file: Initialize the error struct properly Failing to do this has the effect that the error message is not returned when the input file cannot be opened (e.g. if it doesn't exist). Thanks to Martin Vopatek for reporting. --- src/load.c | 2 ++ test/.gitignore | 1 + test/testprogs/Makefile.am | 3 ++- test/testprogs/test_load.c | 24 ++++++++++++++++++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 test/testprogs/test_load.c diff --git a/src/load.c b/src/load.c index f004525..53241ff 100644 --- a/src/load.c +++ b/src/load.c @@ -864,6 +864,8 @@ json_t *json_load_file(const char *path, json_error_t *error) json_t *result; FILE *fp; + error_init(error); + fp = fopen(path, "r"); if(!fp) { diff --git a/test/.gitignore b/test/.gitignore index 09dfb4d..ac70552 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -3,6 +3,7 @@ loads_dumps load_file_dump_file testlogs testprogs/test_array +testprogs/test_load testprogs/test_number testprogs/test_object testprogs/test_simple diff --git a/test/testprogs/Makefile.am b/test/testprogs/Makefile.am index 400a998..41807ed 100644 --- a/test/testprogs/Makefile.am +++ b/test/testprogs/Makefile.am @@ -1,6 +1,7 @@ -check_PROGRAMS = test_array test_simple test_number test_object +check_PROGRAMS = test_array test_load test_simple test_number test_object test_array_SOURCES = test_array.c util.h +test_load_SOURCES = test_load.c util.h test_simple_SOURCES = test_simple.c util.h test_number_SOURCES = test_number.c util.h test_object_SOURCES = test_object.c util.h diff --git a/test/testprogs/test_load.c b/test/testprogs/test_load.c new file mode 100644 index 0000000..4d8fa88 --- /dev/null +++ b/test/testprogs/test_load.c @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2009 Petri Lehtinen + * + * Jansson is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#include +#include +#include "util.h" + +int main() +{ + json_t *json; + json_error_t error; + + json = json_load_file("/path/to/nonexistent/file.json", &error); + if(error.line != -1) + fail("json_load_file returned an invalid line number"); + if(strcmp(error.text, "unable to open /path/to/nonexistent/file.json: No such file or directory") != 0) + fail("json_load_file returned an invalid error message"); + + return 0; +} From d3959a8ce79bb30a8b39a275a4682885e97d17cc Mon Sep 17 00:00:00 2001 From: Petri Lehtinen Date: Thu, 29 Oct 2009 15:42:05 +0200 Subject: [PATCH 2/2] load: Parse a badly put - sign correctly Thanks to Manolis Delakis for reporting. --- src/load.c | 6 +++++- test/testdata/invalid | 17 ++++++++++++++++- test/testdata/invalid-strip | 17 ++++++++++++++++- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/load.c b/src/load.c index 53241ff..1ae62b3 100644 --- a/src/load.c +++ b/src/load.c @@ -418,11 +418,15 @@ static int lex_scan_number(lex_t *lex, char c, json_error_t *error) goto out; } } - else /* c != '0' */ { + else if(isdigit(c)) { c = lex_get_save(lex, error); while(isdigit(c)) c = lex_get_save(lex, error); } + else { + lex_unget_unsave(lex, c); + goto out; + } if(c != '.' && c != 'E' && c != 'e') { long value; diff --git a/test/testdata/invalid b/test/testdata/invalid index 1a70422..7fede67 100644 --- a/test/testdata/invalid +++ b/test/testdata/invalid @@ -167,7 +167,22 @@ too big negative integer near '-123123123123123' ==== 1 invalid token near 'troo' -==== invalid-escap ==== +==== minus-sign-without-number ==== +[-foo] +==== +1 +invalid token near '-' +==== invalid-negative-integerr ==== +[-123foo] +==== +1 +']' expected near 'foo' +==== invalid-negative-real ==== +[-123.123foo] +==== +1 +']' expected near 'foo' +==== invalid-escape ==== ["\a <-- invalid escape"] ==== 1 diff --git a/test/testdata/invalid-strip b/test/testdata/invalid-strip index 8b4a574..1efdc25 100644 --- a/test/testdata/invalid-strip +++ b/test/testdata/invalid-strip @@ -167,7 +167,22 @@ too big negative integer near '-123123123123123' ==== 1 invalid token near 'troo' -==== invalid-escap ==== +==== minus-sign-without-number ==== +[-foo] +==== +1 +invalid token near '-' +==== invalid-negative-integerr ==== +[-123foo] +==== +1 +']' expected near 'foo' +==== invalid-negative-real ==== +[-123.123foo] +==== +1 +']' expected near 'foo' +==== invalid-escape ==== ["\a <-- invalid escape"] ==== 1