Skip to content

Commit

Permalink
Merge pull request #9783 from JuliaLang/ird/fix9781
Browse files Browse the repository at this point in the history
Fix #9871 by making jl_substrtod/f whitespace-tolerant
  • Loading branch information
IainNZ committed Jan 18, 2015
2 parents faa0466 + 64e2dbd commit ad489ed
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -755,9 +755,18 @@ DLLEXPORT int jl_substrtod(char *str, size_t offset, int len, double *out)
pend = bstr+len;
}
*out = strtod_c(bstr, &p);
if ((p == bstr) || (p != pend) ||
if (p == bstr ||
(errno==ERANGE && (*out==0 || *out==HUGE_VAL || *out==-HUGE_VAL)))
err = 1;
// Deal with case where the substring might be something like "1 ",
// which is OK, and "1 X", which we don't allow.
while (p != pend) {
if (!isspace((unsigned char)*p)) {
err = 1;
break;
}
p++;
}
if (bstr != str+offset)
free(bstr);
return err;
Expand Down Expand Up @@ -805,9 +814,18 @@ DLLEXPORT int jl_substrtof(char *str, int offset, int len, float *out)
*out = strtof_c(bstr, &p);
#endif

if ((p == bstr) || (p != pend) ||
if (p == bstr ||
(errno==ERANGE && (*out==0 || *out==HUGE_VALF || *out==-HUGE_VALF)))
err = 1;
// Deal with case where the substring might be something like "1 ",
// which is OK, and "1 X", which we don't allow.
while (p != pend) {
if (!isspace((unsigned char)*p)) {
err = 1;
break;
}
p++;
}
if (bstr != str+offset)
free(bstr);
return err;
Expand Down
11 changes: 11 additions & 0 deletions test/strings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1305,3 +1305,14 @@ for T in (ASCIIString, UTF8String, UTF16String, UTF32String)
end
end
end

# issue #9781
# float(SubString) wasn't tolerant of trailing whitespace, which was different
# to "normal" strings. This also checks we aren't being too tolerant and allowing
# any arbitrary trailing characters.
@test float64("1\n") == 1.0
@test float64(split("0,1\n",","))[2] == 1.0
@test_throws ArgumentError float64(split("0,1 X\n",","))[2]
@test float32("1\n") == 1.0
@test float32(split("0,1\n",","))[2] == 1.0
@test_throws ArgumentError float32(split("0,1 X\n",","))[2]

0 comments on commit ad489ed

Please sign in to comment.