[Python-checkins] r54173 - in python/trunk: Lib/test/test_builtin.py Misc/NEWS Objects/longobject.c
georg.brandl
python-checkins at python.org
Tue Mar 6 19:41:22 CET 2007
Author: georg.brandl
Date: Tue Mar 6 19:41:12 2007
New Revision: 54173
Modified:
python/trunk/Lib/test/test_builtin.py
python/trunk/Misc/NEWS
python/trunk/Objects/longobject.c
Log:
Patch #1638879: don't accept strings with embedded NUL bytes in long().
Modified: python/trunk/Lib/test/test_builtin.py
==============================================================================
--- python/trunk/Lib/test/test_builtin.py (original)
+++ python/trunk/Lib/test/test_builtin.py Tue Mar 6 19:41:12 2007
@@ -1017,6 +1017,11 @@
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)
Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS (original)
+++ python/trunk/Misc/NEWS Tue Mar 6 19:41:12 2007
@@ -12,6 +12,8 @@
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
Modified: python/trunk/Objects/longobject.c
==============================================================================
--- python/trunk/Objects/longobject.c (original)
+++ python/trunk/Objects/longobject.c Tue Mar 6 19:41:12 2007
@@ -3287,8 +3287,25 @@
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),
More information about the Python-checkins
mailing list