]> granicus.if.org Git - python/commitdiff
SF bug 543840: complex(string) accepts strings with \0
authorTim Peters <tim.peters@gmail.com>
Sun, 14 Apr 2002 22:04:03 +0000 (22:04 +0000)
committerTim Peters <tim.peters@gmail.com>
Sun, 14 Apr 2002 22:04:03 +0000 (22:04 +0000)
complex_subtype_from_string():  this stopped parsing at the first 0
byte, as if that were the end of the input string.

Bugfix candidate.

Lib/test/test_b1.py
Objects/complexobject.c

index 8d47abc73706c69ac911fcb2aba6629f70e2db94..554e350fda289a4e154fd9d4dbe945ff2f994918 100644 (file)
@@ -124,16 +124,29 @@ if complex(0j, 3.14) != 3.14j: raise TestFailed, 'complex(0j, 3.14)'
 if complex(0.0, 3.14) != 3.14j: raise TestFailed, 'complex(0.0, 3.14)'
 if complex("1") != 1+0j: raise TestFailed, 'complex("1")'
 if complex("1j") != 1j: raise TestFailed, 'complex("1j")'
+
 try: complex("1", "1")
 except TypeError: pass
 else: raise TestFailed, 'complex("1", "1")'
+
 try: complex(1, "1")
 except TypeError: pass
 else: raise TestFailed, 'complex(1, "1")'
+
 if complex("  3.14+J  ") != 3.14+1j:  raise TestFailed, 'complex("  3.14+J  )"'
 if have_unicode:
     if complex(unicode("  3.14+J  ")) != 3.14+1j:
         raise TestFailed, 'complex(u"  3.14+J  )"'
+
+# SF bug 543840:  complex(string) accepts strings with \0
+# Fixed in 2.3.
+try:
+    complex('1+1j\0j')
+except ValueError:
+    pass
+else:
+    raise TestFailed("complex('1+1j\0j') should have raised ValueError")
+
 class Z:
     def __complex__(self): return 3.14j
 z = Z()
index 3dbaea364b7adf691348c4131a92449af3c334f1..c074aee7fb9fb37513b179d56c26f38a9d54d1e2 100644 (file)
@@ -787,7 +787,7 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
 
                }  /* end of switch  */
 
-       } while (*s!='\0' && !sw_error);
+       } while (s - start < len && !sw_error);
 
        if (sw_error) {
                PyErr_SetString(PyExc_ValueError,