]> granicus.if.org Git - python/commitdiff
Patch #1638879: don't accept strings with embedded NUL bytes in long().
authorGeorg Brandl <georg@python.org>
Tue, 6 Mar 2007 18:41:12 +0000 (18:41 +0000)
committerGeorg Brandl <georg@python.org>
Tue, 6 Mar 2007 18:41:12 +0000 (18:41 +0000)
Lib/test/test_builtin.py
Misc/NEWS
Objects/longobject.c

index 72b696690b5a8bc161632911f6cc525c0d6d857b..da2afbac35af4004c583c9fd1884e71c116458f6 100644 (file)
@@ -1017,6 +1017,11 @@ class BuiltinTest(unittest.TestCase):
         self.assertRaises(ValueError, long, '53', 40)
         self.assertRaises(TypeError, long, 1, 12)
 
+        # SF patch #1638879: embedded NULs were not detected with
+        # explicit base
+        self.assertRaises(ValueError, long, '123\0', 10)
+        self.assertRaises(ValueError, long, '123\x00 245', 20)
+
         self.assertEqual(long('100000000000000000000000000000000', 2),
                          4294967296)
         self.assertEqual(long('102002022201221111211', 3), 4294967296)
index cda539e63e267b2af676aea2b46fd6df9aa6e360..f91e300c9673373cf43604ab8cfbc311bf14d1ad 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's New in Python 2.6 alpha 1?
 Core and builtins
 -----------------
 
+- Patch #1638879: don't accept strings with embedded NUL bytes in long().
+
 - Bug #1674503: close the file opened by execfile() in an error condition.
 
 - Patch #1674228: when assigning a slice (old-style), check for the
index ef3e242cc47ed6d0443aabb02f2a5a86273c4cb4..6b8d6e4b40f8d3fcb6b6c18e9c883ff040899f3c 100644 (file)
@@ -3287,8 +3287,25 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
                return PyLong_FromLong(0L);
        if (base == -909)
                return PyNumber_Long(x);
-       else if (PyString_Check(x))
+       else if (PyString_Check(x)) {
+               /* Since PyLong_FromString doesn't have a length parameter,
+                * check here for possible NULs in the string. */
+               char *string = PyString_AS_STRING(x);
+               if (strlen(string) != PyString_Size(x)) {
+                       /* create a repr() of the input string,
+                        * just like PyLong_FromString does. */
+                       PyObject *srepr;
+                       srepr = PyObject_Repr(x);
+                       if (srepr == NULL)
+                               return NULL;
+                       PyErr_Format(PyExc_ValueError,
+                            "invalid literal for long() with base %d: %s",
+                            base, PyString_AS_STRING(srepr));
+                       Py_DECREF(srepr);
+                       return NULL;
+               }
                return PyLong_FromString(PyString_AS_STRING(x), NULL, base);
+       }
 #ifdef Py_USING_UNICODE
        else if (PyUnicode_Check(x))
                return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),