[pypy-svn] pypy default: Fix strtod with empty or malformed strings

amauryfa commits-noreply at bitbucket.org
Tue Jan 25 08:50:06 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: 
Changeset: r41286:76da8cb71171
Date: 2011-01-25 08:39 +0100
http://bitbucket.org/pypy/pypy/changeset/76da8cb71171/

Log:	Fix strtod with empty or malformed strings

diff --git a/pypy/rlib/test/test_rdtoa.py b/pypy/rlib/test/test_rdtoa.py
--- a/pypy/rlib/test/test_rdtoa.py
+++ b/pypy/rlib/test/test_rdtoa.py
@@ -5,8 +5,11 @@
     assert strtod("12345") == 12345.0
     assert strtod("1.1") == 1.1
     assert strtod("3.47") == 3.47
+    assert strtod(".125") == .125
     raises(ValueError, strtod, "123A")
-    assert strtod(".125") == .125
+    raises(ValueError, strtod, "")
+    raises(ValueError, strtod, " ")
+    raises(ValueError, strtod, "\0")
 
 def test_dtoa():
     assert dtoa(3.47) == "3.47"

diff --git a/pypy/rlib/rdtoa.py b/pypy/rlib/rdtoa.py
--- a/pypy/rlib/rdtoa.py
+++ b/pypy/rlib/rdtoa.py
@@ -45,7 +45,7 @@
         ll_input = rffi.str2charp(input)
         try:
             result = dg_strtod(ll_input, end_ptr)
-            if end_ptr[0] and ord(end_ptr[0][0]):
+            if end_ptr[0] and (end_ptr[0] == ll_input or ord(end_ptr[0][0])):
                 offset = (rffi.cast(rffi.LONG, end_ptr[0]) -
                           rffi.cast(rffi.LONG, ll_input))
                 raise ValueError("invalid input at position %d" % (offset,))


More information about the Pypy-commit mailing list