diff options
author | Georg Brandl <georg@python.org> | 2007-03-06 18:41:12 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2007-03-06 18:41:12 (GMT) |
commit | 00cd818dea72177dd79a3d86562d52f1433f9f49 (patch) | |
tree | 19b02cfa57feb0ad0e4f2ecb35f7aeac1acb59cf /Objects | |
parent | 1579265aac467db8368676dff6757a46361a1c01 (diff) | |
download | cpython-00cd818dea72177dd79a3d86562d52f1433f9f49.zip cpython-00cd818dea72177dd79a3d86562d52f1433f9f49.tar.gz cpython-00cd818dea72177dd79a3d86562d52f1433f9f49.tar.bz2 |
Patch #1638879: don't accept strings with embedded NUL bytes in long().
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/longobject.c | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/Objects/longobject.c b/Objects/longobject.c index ef3e242..6b8d6e4 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -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), |