Permalink
Browse files

Handle whitespace only strings in Float::from_cstr

In certain cases when a String with only whitespace (be it spaces, newlines or
a mix of that) would be converted to a Float this would result in a crash.

The added test is a bit odd but it's based on the output/behaviour as described
in #2955 (comment).

In particular String::byte_size() returned 0 for some reason (this might be a
bug on its own as far as I can tell), which would then trigger a crash in
Float::from_cstr().

Thanks to @brixen for helping me out with this one as I was, and this might be
an understatement, rather clueless on what was going on.

See #2955 for more info.
  • Loading branch information...
1 parent fbb4680 commit a5270181f6636d557d3d2dfcb339d7468b9a01c1 @YorickPeterse YorickPeterse committed Feb 25, 2014
Showing with 10 additions and 1 deletion.
  1. +1 −1 vm/builtin/float.cpp
  2. +9 −0 vm/test/test_float.hpp
@@ -68,7 +68,7 @@ namespace rubinius {
Float* Float::from_cstr(STATE, const char* str, const char* end, Object* strict) {
// Skip leading whitespace and underscores.
- while(isspace(*str) || *str == '_') {
+ while(str < end && (isspace(*str) || *str == '_')) {
if(*str == '_') {
// Leading underscores are not allowed in strict mode.
if(CBOOL(strict)) {
@@ -314,5 +314,14 @@ class TestFloat : public CxxTest::TestSuite, public VMTest {
TS_ASSERT_THROWS_ASSERT(f->to_s_formatted(state, format), const RubyException &e,
TS_ASSERT(Exception::argument_error_p(state, e.exception)));
}
+
+ void test_from_cstr_with_whitespace() {
+ const char *str = "\n \n ";
+
+ Float *received = Float::from_cstr(state, str, str + 0, cFalse);
+ Float *expected = Float::create(state, 0.0);
+
+ TS_ASSERT_EQUALS(expected->equal(state, received), cTrue);
+ }
};

1 comment on commit a527018

@YorickPeterse
Member

To clarify, the length of byte_size() is correct and is expected behaviour. I misunderstood the way the values were laid out in my GDB output.

Please sign in to comment.