Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions mrbgems/mruby-kernel-ext/test/kernel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,28 @@ def m1; __callee__ end
assert_raise(ArgumentError) { Float('1_2.3__4') }
end

assert('Kernel#Float with long digit strings', '#7846') do
skip unless Object.const_defined?(:Float)
assert_operator(1.0, :eql?, Float('0' * 80 + '1'))
assert_operator(0.0, :eql?, Float('0' * 100))
assert_operator(-5.0, :eql?, Float('-' + '0' * 100 + '5'))
assert_operator(100000.0, :eql?, Float('1.' + '0' * 80 + 'e5'))
assert_operator(1.0e-81, :eql?, Float('0.' + '0' * 80 + '1'))
assert_operator(1.0e+101, :eql?, Float('1_' + '0' * 100 + '0'))
assert_raise(ArgumentError) { Float('1_2__3' + '0' * 100) }
assert_raise(ArgumentError) { Float('a' * 100) }
assert_raise(ArgumentError) { Float('0' * 80 + '1 junk') }
end

assert('Kernel#Float on a shared substring', '#7846') do
skip unless Object.const_defined?(:Float)
s = '1' * 30 + '5' * 30
t = s[0, 30]
assert_operator(Float('1' * 30), :eql?, Float(t))
u = ('9' * 40 + 'e5')[0, 40]
assert_operator(Float('9' * 40), :eql?, Float(u))
end

assert('Kernel#Float with a hexadecimal number too big for mrb_int') do
skip unless Object.const_defined?(:Float)
begin
Expand Down
86 changes: 53 additions & 33 deletions src/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -3854,14 +3854,18 @@ mrb_str_to_i(mrb_state *mrb, mrb_value self)
static double
mrb_str_len_to_dbl(mrb_state *mrb, const char *s, size_t len, mrb_bool badcheck)
{
char buf[DBL_DIG * 4 + 20];
char sbuf[DBL_DIG * 4 + 20];
char *heap = NULL;
char *buf = sbuf;
const char *p = s, *p2;
const char *pend = p + len;
const char *parse;
const char *parse_end;
char *end;
char *n;
char prev = 0;
double d;
mrb_bool dot = FALSE;
mrb_bool strip_us = FALSE;

if (!p) return 0.0;
while (p<pend && ISSPACE(*p)) p++;
Expand All @@ -3885,57 +3889,73 @@ mrb_str_len_to_dbl(mrb_state *mrb, const char *s, size_t len, mrb_bool badcheck)
/* not reached */
}
pend = p;
p = p2;
goto nocopy;
parse = p2;
parse_end = pend;
goto parse;
}
if (!badcheck && *p == ' ') {
pend = p;
p = p2;
goto nocopy;
parse = p2;
parse_end = pend;
goto parse;
}
if (*p == '_') {
strip_us = TRUE;
break;
}
if (*p == '_') break;
p++;
}
p = p2;
n = buf;
while (p < pend) {
char c = *p++;
if (c == '.') dot = TRUE;
if (c == '_') {
/* remove an underscore between digits */
if (n == buf || !ISDIGIT(prev) || p == pend) {
if (badcheck) goto bad;
break;
}
{
size_t copylen = (size_t)(pend - p2);
size_t bufsize = copylen + 1;

if (bufsize > sizeof(sbuf))
buf = heap = (char*)mrb_malloc(mrb, bufsize);
if (!strip_us) {
memcpy(buf, p2, copylen);
buf[copylen] = '\0';
parse = buf;
parse_end = buf + copylen;
}
else if (badcheck && prev == '_' && !ISDIGIT(c)) goto bad;
else {
const char *bend = buf+sizeof(buf)-1;
if (n==bend) { /* buffer overflow */
if (dot) break; /* cut off remaining fractions */
return INFINITY;
p = p2;
n = buf;
while (p < pend) {
char c = *p++;

if (c == '_') {
/* remove an underscore between digits */
if (n == buf || !ISDIGIT(prev) || p == pend) {
if (badcheck) goto bad;
break;
}
}
else if (badcheck && prev == '_' && !ISDIGIT(c)) goto bad;
else *n++ = c;
prev = c;
}
*n++ = c;
*n = '\0';
parse = buf;
parse_end = n;
}
prev = c;
}
*n = '\0';
p = buf;
pend = n;
nocopy:
if (mrb_read_float(p, &end, &d) == FALSE) {
parse:
if (mrb_read_float(parse, &end, &d) == FALSE) {
if (badcheck) {
bad:
if (heap) mrb_free(mrb, heap);
mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid string for float(%!s)", s);
/* not reached */
}
if (heap) mrb_free(mrb, heap);
return 0.0;
}
if (badcheck) {
if (!end || p == end) goto bad;
while (end<pend && ISSPACE(*end)) end++;
if (end<pend) goto bad;
if (!end || parse == end) goto bad;
while (end<parse_end && ISSPACE(*end)) end++;
if (end<parse_end) goto bad;
}
if (heap) mrb_free(mrb, heap);
return d;
}

Expand Down
27 changes: 27 additions & 0 deletions test/t/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1476,6 +1476,33 @@ def []=(*args)
assert_operator(Float::INFINITY, :eql?, ('9' * 200000).to_f)
end if Object.const_defined?(:Float)

assert('String#to_f with long digit strings', '#7846') do
assert_operator(1.0, :eql?, ('0' * 78 + '1').to_f)
assert_operator(1.0, :eql?, ('0' * 79 + '1').to_f)
assert_operator(1.0, :eql?, ('0' * 80 + '1').to_f)
assert_operator(1.0, :eql?, ('0' * 81 + '1').to_f)
assert_operator(1.0, :eql?, ('0' * 100 + '1').to_f)
assert_operator(0.0, :eql?, ('0' * 100).to_f)
assert_operator(0.0, :eql?, ('0' * 1000).to_f)
assert_operator(-5.0, :eql?, ('-' + '0' * 100 + '5').to_f)
assert_operator(5.0, :eql?, ('+' + '0' * 100 + '5').to_f)
assert_operator(100000.0, :eql?, ('1.' + '0' * 80 + 'e5').to_f)
assert_operator(100000.0, :eql?, ('1.' + '0' * 100 + 'E+5').to_f)
assert_operator(1.0e-05, :eql?, ('1.' + '0' * 100 + 'e-5').to_f)
assert_operator(1.0e-81, :eql?, ('0.' + '0' * 80 + '1').to_f)
assert_operator(1.0e+101, :eql?, ('1_' + '0' * 100 + '0').to_f)
assert_operator(0.0, :eql?, ('a' * 100).to_f)
assert_operator(1.2, :eql?, ("\t" + '0' * 100 + '1.2 junk').to_f)
end if Object.const_defined?(:Float)

assert('String#to_f on a shared substring', '#7846') do
s = '1' * 30 + '5' * 30
t = s[0, 30]
assert_operator(('1' * 30).to_f, :eql?, t.to_f)
u = ('9' * 40 + 'e5')[0, 40]
assert_operator(('9' * 40).to_f, :eql?, u.to_f)
end if Object.const_defined?(:Float)

assert('String#to_i', '15.2.10.5.39') do
assert_operator 0, :eql?, ''.to_i
assert_operator 32143, :eql?, '32143'.to_i
Expand Down