From: Serhiy Storchaka Date: Sat, 3 Aug 2013 18:14:05 +0000 (+0300) Subject: Issue #16741: Fix an error reporting in int(). X-Git-Tag: v3.4.0a2~313^2~2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=579ddc2fd4fd618b72d8202d79d75d22e7e0b60a;p=python Issue #16741: Fix an error reporting in int(). --- 579ddc2fd4fd618b72d8202d79d75d22e7e0b60a diff --cc Misc/NEWS index 10c0470f39,10d908cd6d..61857a947e --- a/Misc/NEWS +++ b/Misc/NEWS @@@ -10,16 -12,10 +10,18 @@@ What's New in Python 3.4.0 Alpha 1 Core and Builtins ----------------- + - Issue #16741: Fix an error reporting in int(). + - Issue #17899: Fix rare file descriptor leak in os.listdir(). +- Issue #9035: ismount now recognises volumes mounted below a drive root + on Windows. Original patch by Atsuo Ishimoto. + +- Issue #18214: Improve finalization of Python modules to avoid setting + their globals to None, in most cases. + +- Issue #18112: PEP 442 implementation (safe object finalization). + - Issue #18552: Check return value of PyArena_AddPyObject() in obj2ast_object(). diff --cc Objects/longobject.c index b47be40627,30ffc94d41..e0d641a016 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@@ -2274,12 -2292,37 +2287,37 @@@ digit beyond the first if (strobj == NULL) return NULL; PyErr_Format(PyExc_ValueError, -- "invalid literal for int() with base %d: %R", ++ "invalid literal for int() with base %d: %.200R", base, strobj); Py_DECREF(strobj); return NULL; } + /* Since PyLong_FromString doesn't have a length parameter, + * check here for possible NULs in the string. + * + * Reports an invalid literal as a bytes object. + */ + PyObject * + _PyLong_FromBytes(const char *s, Py_ssize_t len, int base) + { + PyObject *result, *strobj; + char *end = NULL; + + result = PyLong_FromString((char*)s, &end, base); + if (end == NULL || (result != NULL && end == s + len)) + return result; + Py_XDECREF(result); + strobj = PyBytes_FromStringAndSize(s, Py_MIN(len, 200)); + if (strobj != NULL) { + PyErr_Format(PyExc_ValueError, - "invalid literal for int() with base %d: %R", ++ "invalid literal for int() with base %d: %.200R", + base, strobj); + Py_DECREF(strobj); + } + return NULL; + } + PyObject * PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base) { @@@ -2294,9 -2337,8 +2332,8 @@@ PyObject * PyLong_FromUnicodeObject(PyObject *u, int base) { - PyObject *result; - PyObject *asciidig; - char *buffer, *end; - PyObject *result, *asciidig, *strobj; ++ PyObject *result, *asciidig; + char *buffer, *end = NULL; Py_ssize_t buflen; asciidig = _PyUnicode_TransformDecimalAndSpaceToASCII(u); @@@ -2305,17 -2347,26 +2342,22 @@@ buffer = PyUnicode_AsUTF8AndSize(asciidig, &buflen); if (buffer == NULL) { Py_DECREF(asciidig); - return NULL; + if (!PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) + return NULL; } - result = PyLong_FromString(buffer, &end, base); - if (result != NULL && end != buffer + buflen) { - PyErr_SetString(PyExc_ValueError, - "null byte in argument for int()"); - Py_DECREF(result); - result = NULL; + else { + result = PyLong_FromString(buffer, &end, base); + if (end == NULL || (result != NULL && end == buffer + buflen)) { + Py_DECREF(asciidig); + return result; + } + Py_DECREF(asciidig); + Py_XDECREF(result); } - Py_DECREF(asciidig); - return result; - strobj = PySequence_GetSlice(u, 0, 200); - if (strobj != NULL) { - PyErr_Format(PyExc_ValueError, - "invalid literal for int() with base %d: %R", - base, strobj); - Py_DECREF(strobj); - } ++ PyErr_Format(PyExc_ValueError, ++ "invalid literal for int() with base %d: %.200R", ++ base, u); + return NULL; } /* forward */