Add error handling for read_number and fix it
This commit is contained in:
4
Makefile
4
Makefile
@@ -12,11 +12,11 @@ CC := gcc
|
||||
# -fsanitize={address,undefined}
|
||||
CFLAGS.gcc.debug := -Og -ggdb -fanalyzer -DBACKTRACE -rdynamic -fsanitize=address -fno-omit-frame-pointer
|
||||
CFLAGS.gcc.release := -O3 -g -march=native -DNDEBUG
|
||||
CFLAGS.gcc := ${CFLAGS.gcc.${BUILD}} -Iinclude -Wall -Wextra -Wpedantic -Werror -fstack-protector-all -std=gnu11
|
||||
CFLAGS.gcc := ${CFLAGS.gcc.${BUILD}} -Iinclude -Wall -Wextra -Wpedantic -Werror -Wno-strict-aliasing -fstack-protector-all -std=gnu11
|
||||
|
||||
CFLAGS.clang.debug=-O0 -g3 -DBACKTRACE -rdynamic
|
||||
CFLAGS.clang.release=-O3 -g -march=native -DNDEBUG
|
||||
CFLAGS.clang=-Wextra -Wall -Wpedantic -Werror -fstack-protector-all ${CFLAGS.clang.${BUILD}}
|
||||
CFLAGS.clang=-Iinclude -Wall -Wextra -Wpedantic -Werror -Wno-strict-aliasing -fstack-protector-all ${CFLAGS.clang.${BUILD}}
|
||||
|
||||
CFLAGS := ${CFLAGS.${COMPILER}}
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ int main(int argc, char* argv[])
|
||||
|
||||
struct json_value x = parse_json_value(fp);
|
||||
|
||||
print_json(x, 1);
|
||||
// print_json(x, 1);
|
||||
|
||||
json_value_delete(x);
|
||||
|
||||
|
||||
14
src/parse.c
14
src/parse.c
@@ -106,6 +106,7 @@ struct json_value parse_json_value(FILE* fp)
|
||||
|
||||
default:
|
||||
if (isdigit(c)) {
|
||||
ungetc(c, fp);
|
||||
result.type = number;
|
||||
result.number = read_number(fp);
|
||||
} else {
|
||||
@@ -364,9 +365,18 @@ bool read_boolean(FILE* fp)
|
||||
*/
|
||||
double read_number(FILE* fp)
|
||||
{
|
||||
double n;
|
||||
static const unsigned long neg_nan = 0xFFFFFFFFFFFFFFFFULL;
|
||||
double n = *(double*)&neg_nan;
|
||||
|
||||
fscanf(fp, "%lf", &n);
|
||||
int n_read = fscanf(fp, "%lf", &n);
|
||||
|
||||
/* try to read as long instead */
|
||||
if (n_read == 0) {
|
||||
err_ctx(UNEXPECTED_CHAR, fp, "(%s) number expected, found %lf", __func__, n);
|
||||
}
|
||||
|
||||
if (n_read == EOF)
|
||||
err_ctx(EARLY_EOF, fp, "(%s) unexpected EOF", __func__);
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user