1
0
mirror of https://github.com/kgabis/parson.git synced 2025-01-28 06:32:55 +08:00

Checking errno after strtod call.

This commit is contained in:
Krzysztof Gabis 2017-01-06 21:35:29 +01:00
parent defb57f2d3
commit cb14736e96
2 changed files with 10 additions and 8 deletions

View File

@ -33,6 +33,7 @@
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include <math.h> #include <math.h>
#include <errno.h>
#define STARTING_CAPACITY 15 #define STARTING_CAPACITY 15
#define ARRAY_MAX_CAPACITY 122880 /* 15*(2^13) */ #define ARRAY_MAX_CAPACITY 122880 /* 15*(2^13) */
@ -737,15 +738,14 @@ static JSON_Value * parse_boolean_value(const char **string) {
static JSON_Value * parse_number_value(const char **string) { static JSON_Value * parse_number_value(const char **string) {
char *end; char *end;
double number = strtod(*string, &end); double number = 0;
JSON_Value *output_value; errno = 0;
if (is_decimal(*string, end - *string)) { number = strtod(*string, &end);
*string = end; if (errno || !is_decimal(*string, end - *string)) {
output_value = json_value_init_number(number); return NULL;
} else {
output_value = NULL;
} }
return output_value; *string = end;
return json_value_init_number(number);
} }
static JSON_Value * parse_null_value(const char **string) { static JSON_Value * parse_null_value(const char **string) {

View File

@ -291,6 +291,8 @@ void test_suite_3(void) {
TEST(json_parse_string("[-007]") == NULL); TEST(json_parse_string("[-007]") == NULL);
TEST(json_parse_string("[-07.0]") == NULL); TEST(json_parse_string("[-07.0]") == NULL);
TEST(json_parse_string("[\"\\uDF67\\uD834\"]") == NULL); /* wrong order surrogate pair */ TEST(json_parse_string("[\"\\uDF67\\uD834\"]") == NULL); /* wrong order surrogate pair */
TEST(json_parse_string("[1.7976931348623157e309]") == NULL);
TEST(json_parse_string("[-1.7976931348623157e309]") == NULL);
} }
void test_suite_4() { void test_suite_4() {