You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2010/11/15 20:44:15 UTC

[lucy-commits] svn commit: r1035422 - in /incubator/lucy/trunk/core/Lucy: Object/CharBuf.c Test/Object/TestCharBuf.c

Author: marvin
Date: Mon Nov 15 19:44:14 2010
New Revision: 1035422

URL: http://svn.apache.org/viewvc?rev=1035422&view=rev
Log:
Prevent strtod() from running past the end of a CharBuf and calculating the
wrong value for CB_To_F64().

Modified:
    incubator/lucy/trunk/core/Lucy/Object/CharBuf.c
    incubator/lucy/trunk/core/Lucy/Test/Object/TestCharBuf.c

Modified: incubator/lucy/trunk/core/Lucy/Object/CharBuf.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/core/Lucy/Object/CharBuf.c?rev=1035422&r1=1035421&r2=1035422&view=diff
==============================================================================
--- incubator/lucy/trunk/core/Lucy/Object/CharBuf.c (original)
+++ incubator/lucy/trunk/core/Lucy/Object/CharBuf.c Mon Nov 15 19:44:14 2010
@@ -443,10 +443,26 @@ CB_basex_to_i64(CharBuf *self, uint32_t 
     return retval;
 }
 
+static double
+S_safe_to_f64(CharBuf *self)
+{
+    size_t amount = self->size < 511 ? self->size : 511;
+    char buf[512];
+    memcpy(buf, self->ptr, amount);
+    buf[amount] = 0; // NULL-terminate.
+    return strtod(buf, NULL);
+}
+
 double
 CB_to_f64(CharBuf *self)
 {
-    return strtod(self->ptr, NULL);
+    char   *end;
+    double  value    = strtod(self->ptr, &end);
+    size_t  consumed = end - self->ptr;
+    if (consumed > self->size) { // strtod overran
+        value = S_safe_to_f64(self);
+    }
+    return value;
 }
 
 CharBuf*

Modified: incubator/lucy/trunk/core/Lucy/Test/Object/TestCharBuf.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/core/Lucy/Test/Object/TestCharBuf.c?rev=1035422&r1=1035421&r2=1035422&view=diff
==============================================================================
--- incubator/lucy/trunk/core/Lucy/Test/Object/TestCharBuf.c (original)
+++ incubator/lucy/trunk/core/Lucy/Test/Object/TestCharBuf.c Mon Nov 15 19:44:14 2010
@@ -201,6 +201,13 @@ test_To_F64(TestBatch *batch)
     if (difference < 0) { difference = 0 - difference; }
     TEST_TRUE(batch, difference < 0.001, "To_F64 negative");
 
+    CB_setf(charbuf, "1.59");
+    double value_full = CB_To_F64(charbuf);
+    CB_Set_Size(charbuf, 3);
+    double value_short = CB_To_F64(charbuf);
+    TEST_TRUE(batch, value_short < value_full, 
+        "TO_F64 doesn't run past end of string");
+
     DECREF(charbuf);
 }
 
@@ -394,7 +401,7 @@ test_serialization(TestBatch *batch)
 void
 TestCB_run_tests()
 {
-    TestBatch *batch = TestBatch_new(49);
+    TestBatch *batch = TestBatch_new(50);
     TestBatch_Plan(batch);
 
     test_vcatf_s(batch);