load: Check for integer and real overlfows and underflows
[jansson.git] / src / load.c
index fc3679d..8d5a392 100644 (file)
@@ -8,11 +8,11 @@
 #define _GNU_SOURCE
 #include <ctype.h>
 #include <errno.h>
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdarg.h>
-#include <unistd.h>
 #include <assert.h>
 
 #include <jansson.h>
@@ -399,10 +399,11 @@ out:
     free(lex->value.string);
 }
 
-static void lex_scan_number(lex_t *lex, char c, json_error_t *error)
+static int lex_scan_number(lex_t *lex, char c, json_error_t *error)
 {
     const char *saved_text;
     char *end;
+    double value;
 
     lex->token = TOKEN_INVALID;
 
@@ -423,14 +424,26 @@ static void lex_scan_number(lex_t *lex, char c, json_error_t *error)
     }
 
     if(c != '.' && c != 'E' && c != 'e') {
+        long value;
+
         lex_unget_unsave(lex, c);
-        lex->token = TOKEN_INTEGER;
 
         saved_text = strbuffer_value(&lex->saved_text);
-        lex->value.integer = strtol(saved_text, &end, 10);
+        value = strtol(saved_text, &end, 10);
         assert(end == saved_text + lex->saved_text.length);
 
-        return;
+        if((value == LONG_MAX && errno == ERANGE) || value > INT_MAX) {
+            error_set(error, lex, "too big integer");
+            goto out;
+        }
+        else if((value == LONG_MIN && errno == ERANGE) || value < INT_MIN) {
+            error_set(error, lex, "too big negative integer");
+            goto out;
+        }
+
+        lex->token = TOKEN_INTEGER;
+        lex->value.integer = (int)value;
+        return 0;
     }
 
     if(c == '.') {
@@ -460,14 +473,29 @@ static void lex_scan_number(lex_t *lex, char c, json_error_t *error)
     }
 
     lex_unget_unsave(lex, c);
-    lex->token = TOKEN_REAL;
 
     saved_text = strbuffer_value(&lex->saved_text);
-    lex->value.real = strtod(saved_text, &end);
+    value = strtod(saved_text, &end);
     assert(end == saved_text + lex->saved_text.length);
 
+    if(value == 0 && errno == ERANGE) {
+        error_set(error, lex, "real number underflow");
+        goto out;
+    }
+
+    /* Cannot test for +/-HUGE_VAL because the HUGE_VAL constant is
+       only defined in C99 mode. So let's trust in sole errno. */
+    else if(errno == ERANGE) {
+        error_set(error, lex, "real number overflow");
+        goto out;
+    }
+
+    lex->token = TOKEN_REAL;
+    lex->value.real = value;
+    return 0;
+
 out:
-    return;
+    return -1;
 }
 
 static int lex_scan(lex_t *lex, json_error_t *error)
@@ -506,8 +534,10 @@ static int lex_scan(lex_t *lex, json_error_t *error)
     else if(c == '"')
         lex_scan_string(lex, error);
 
-    else if(isdigit(c) || c == '-')
-        lex_scan_number(lex, c, error);
+    else if(isdigit(c) || c == '-') {
+        if(lex_scan_number(lex, c, error))
+            goto out;
+    }
 
     else if(isupper(c) || islower(c)) {
         /* eat up the whole identifier for clearer error messages */
@@ -541,6 +571,17 @@ out:
     return lex->token;
 }
 
+static char *lex_steal_string(lex_t *lex)
+{
+    char *result = NULL;
+    if(lex->token == TOKEN_STRING)
+    {
+        result = lex->value.string;
+        lex->value.string = NULL;
+    }
+    return result;
+}
+
 static int lex_init(lex_t *lex, get_func get, eof_func eof, void *data)
 {
     stream_init(&lex->stream, get, eof, data);
@@ -584,7 +625,7 @@ static json_t *parse_object(lex_t *lex, json_error_t *error)
             goto error;
         }
 
-        key = strdup(lex->value.string);
+        key = lex_steal_string(lex);
         if(!key)
             return NULL;