Don't allow trailing non-number characters when parsing a string to a number.

This commit is contained in:
Bob Nystrom
2015-02-25 07:07:54 -08:00
parent 4b88292ec1
commit fc2ed6f6e1
2 changed files with 14 additions and 5 deletions
+12 -4
View File
@@ -1,3 +1,4 @@
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
@@ -923,14 +924,21 @@ DEF_NATIVE(num_fromString)
{
if (!validateString(vm, args, 1, "Argument")) return PRIM_ERROR;
ObjString* candidate = AS_STRING(args[1]);
ObjString* string = AS_STRING(args[1]);
// Corner case: Can't parse an empty string.
if (string->length == 0) RETURN_NULL;
char* end;
double number = strtod(candidate->value, &end);
double number = strtod(string->value, &end);
// Invalid number literal.
// Skip past any trailing whitespace.
while (*end != '\0' && isspace(*end)) end++;
// We must have consumed the entire string. Otherwise, it contains non-number
// characters and we can't parse it.
// TODO: Check errno == ERANGE here.
if (end == candidate->value) RETURN_NULL;
if (end < string->value + string->length) RETURN_NULL;
RETURN_NUM(number);
}