Skip to content

Commit

Permalink
Bug #1545497: when given an explicit base, int() did ignore NULs
Browse files Browse the repository at this point in the history
embedded in the string to convert.


git-svn-id: http://svn.python.org/projects/python/trunk@52305 6015fed2-1504-0410-9fe1-9d1591cc4771
  • Loading branch information
georg.brandl committed Oct 12, 2006
1 parent 1954cbc commit 0dc3cee
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,11 @@ def test_int(self):
self.assertRaises(ValueError, int, '123\0')
self.assertRaises(ValueError, int, '53', 40)

# SF bug 1545497: embedded NULs were not detected with
# explicit base
self.assertRaises(ValueError, int, '123\0', 10)
self.assertRaises(ValueError, int, '123\x00 245', 20)

x = int('1' * 600)
self.assert_(isinstance(x, long))

Expand Down
3 changes: 3 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ What's New in Python 2.6 alpha 1?
Core and builtins
-----------------

- Bug #1545497: when given an explicit base, int() did ignore NULs
embedded in the string to convert.

- Bug #1569998: break inside a try statement (outside a loop) is now
recognized and rejected.

Expand Down
21 changes: 19 additions & 2 deletions Objects/intobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -987,8 +987,25 @@ int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return PyInt_FromLong(0L);
if (base == -909)
return PyNumber_Int(x);
if (PyString_Check(x))
return PyInt_FromString(PyString_AS_STRING(x), NULL, base);
if (PyString_Check(x)) {
/* Since PyInt_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 PyInt_FromString does */
PyObject *srepr;
srepr = PyObject_Repr(x);
if (srepr == NULL)
return NULL;
PyErr_Format(PyExc_ValueError,
"invalid literal for int() with base %d: %s",
base, PyString_AS_STRING(srepr));
Py_DECREF(srepr);
return NULL;
}
return PyInt_FromString(string, NULL, base);
}
#ifdef Py_USING_UNICODE
if (PyUnicode_Check(x))
return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
Expand Down

0 comments on commit 0dc3cee

Please sign in to comment.