From 2a7fa98a3aa3faab7682862f65ee46323826038a Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Wed, 5 Feb 2014 16:23:01 -0800 Subject: [PATCH] Add numbers (and a buffer overflow risk to fix) --- json.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/json.c b/json.c index 14443d4..a0dfa2a 100644 --- a/json.c +++ b/json.c @@ -62,6 +62,12 @@ static void json_error(char *s) { exit(EXIT_FAILURE); } +int peek(FILE *f) { + int c = getc(f); + ungetc(c, f); + return c; +} + json_object *parse(FILE *f, json_object *current) { int c = getc(f); if (c == EOF) { @@ -139,4 +145,54 @@ json_object *parse(FILE *f, json_object *current) { return parse(f, current); } + + if (c == '-' || (c >= '0' && c <= '9')) { +#define MAX 500 + char val[MAX]; + int off = 0; + + val[off++] = c; + + if (c >= '1' && c <= '9') { + c = peek(f); + + while (c >= '0' && c <= '9') { + val[off++] = getc(f); + c = peek(f); + } + } + + if (peek(f) == '.') { + val[off++] = getc(f); + + c = peek(f); + while (c >= '0' && c <= '9') { + val[off++] = getc(f); + c = peek(f); + } + } + + c = peek(f); + if (c == 'e' || c == 'E') { + val[off++] = getc(f); + + c = peek(f); + if (c == '+' || c == '-') { + val[off++] = getc(f); + } + + c = peek(f); + while (c >= '0' && c <= '9') { + val[off++] = getc(f); + c = peek(f); + } + } + + val[off++] = '\0'; + + json_object *n = add_object(JSON_NUMBER, current); + n->number = atof(val); + + return n; + } }