mirror of
https://github.com/python/cpython.git
synced 2025-08-02 16:13:13 +00:00
Issue #26198: Fixed error messages for some argument parsing errors.
Fixed the documented about buffer overflow error for "es#" and "et#" format units.
This commit is contained in:
parent
d5db14794b
commit
c4b813d05d
3 changed files with 23 additions and 14 deletions
|
@ -206,7 +206,8 @@ Unless otherwise stated, buffers are not NUL-terminated.
|
||||||
:c:func:`PyArg_ParseTuple` will use this location as the buffer and interpret the
|
:c:func:`PyArg_ParseTuple` will use this location as the buffer and interpret the
|
||||||
initial value of *\*buffer_length* as the buffer size. It will then copy the
|
initial value of *\*buffer_length* as the buffer size. It will then copy the
|
||||||
encoded data into the buffer and NUL-terminate it. If the buffer is not large
|
encoded data into the buffer and NUL-terminate it. If the buffer is not large
|
||||||
enough, a :exc:`ValueError` will be set.
|
enough, a :exc:`TypeError` will be set.
|
||||||
|
Note: starting from Python 3.6 a :exc:`ValueError` will be set.
|
||||||
|
|
||||||
In both cases, *\*buffer_length* is set to the length of the encoded data
|
In both cases, *\*buffer_length* is set to the length of the encoded data
|
||||||
without the trailing NUL byte.
|
without the trailing NUL byte.
|
||||||
|
|
|
@ -489,7 +489,7 @@ class SkipitemTest(unittest.TestCase):
|
||||||
format.encode("ascii"), keywords)
|
format.encode("ascii"), keywords)
|
||||||
when_not_skipped = False
|
when_not_skipped = False
|
||||||
except TypeError as e:
|
except TypeError as e:
|
||||||
s = "argument 1 must be impossible<bad format char>, not int"
|
s = "argument 1 (impossible<bad format char>)"
|
||||||
when_not_skipped = (str(e) == s)
|
when_not_skipped = (str(e) == s)
|
||||||
except RuntimeError as e:
|
except RuntimeError as e:
|
||||||
when_not_skipped = False
|
when_not_skipped = False
|
||||||
|
|
|
@ -342,7 +342,7 @@ vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
|
||||||
flags, levels, msgbuf,
|
flags, levels, msgbuf,
|
||||||
sizeof(msgbuf), &freelist);
|
sizeof(msgbuf), &freelist);
|
||||||
if (msg) {
|
if (msg) {
|
||||||
seterror(i+1, msg, levels, fname, msg);
|
seterror(i+1, msg, levels, fname, message);
|
||||||
return cleanreturn(0, &freelist);
|
return cleanreturn(0, &freelist);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -535,9 +535,15 @@ converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
|
||||||
{
|
{
|
||||||
assert(expected != NULL);
|
assert(expected != NULL);
|
||||||
assert(arg != NULL);
|
assert(arg != NULL);
|
||||||
PyOS_snprintf(msgbuf, bufsize,
|
if (expected[0] == '(') {
|
||||||
"must be %.50s, not %.50s", expected,
|
PyOS_snprintf(msgbuf, bufsize,
|
||||||
arg == Py_None ? "None" : arg->ob_type->tp_name);
|
"%.100s", expected);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
PyOS_snprintf(msgbuf, bufsize,
|
||||||
|
"must be %.50s, not %.50s", expected,
|
||||||
|
arg == Py_None ? "None" : arg->ob_type->tp_name);
|
||||||
|
}
|
||||||
return msgbuf;
|
return msgbuf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -741,7 +747,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
|
||||||
if (PyLong_Check(arg))
|
if (PyLong_Check(arg))
|
||||||
ival = PyLong_AsUnsignedLongMask(arg);
|
ival = PyLong_AsUnsignedLongMask(arg);
|
||||||
else
|
else
|
||||||
return converterr("integer<k>", arg, msgbuf, bufsize);
|
return converterr("int", arg, msgbuf, bufsize);
|
||||||
*p = ival;
|
*p = ival;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -766,7 +772,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
|
||||||
if (PyLong_Check(arg))
|
if (PyLong_Check(arg))
|
||||||
ival = PyLong_AsUnsignedLongLongMask(arg);
|
ival = PyLong_AsUnsignedLongLongMask(arg);
|
||||||
else
|
else
|
||||||
return converterr("integer<K>", arg, msgbuf, bufsize);
|
return converterr("int", arg, msgbuf, bufsize);
|
||||||
*p = ival;
|
*p = ival;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -1123,9 +1129,11 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
|
||||||
} else {
|
} else {
|
||||||
if (size + 1 > BUFFER_LEN) {
|
if (size + 1 > BUFFER_LEN) {
|
||||||
Py_DECREF(s);
|
Py_DECREF(s);
|
||||||
return converterr(
|
PyErr_Format(PyExc_TypeError,
|
||||||
"(buffer overflow)",
|
"encoded string too long "
|
||||||
arg, msgbuf, bufsize);
|
"(%zd, maximum length %zd)",
|
||||||
|
(Py_ssize_t)size, (Py_ssize_t)(BUFFER_LEN-1));
|
||||||
|
RETURN_ERR_OCCURRED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
memcpy(*buffer, ptr, size+1);
|
memcpy(*buffer, ptr, size+1);
|
||||||
|
@ -1147,7 +1155,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
|
||||||
if ((Py_ssize_t)strlen(ptr) != size) {
|
if ((Py_ssize_t)strlen(ptr) != size) {
|
||||||
Py_DECREF(s);
|
Py_DECREF(s);
|
||||||
return converterr(
|
return converterr(
|
||||||
"encoded string without NULL bytes",
|
"encoded string without null bytes",
|
||||||
arg, msgbuf, bufsize);
|
arg, msgbuf, bufsize);
|
||||||
}
|
}
|
||||||
*buffer = PyMem_NEW(char, size + 1);
|
*buffer = PyMem_NEW(char, size + 1);
|
||||||
|
@ -1237,7 +1245,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
|
||||||
|
|
||||||
if (*format != '*')
|
if (*format != '*')
|
||||||
return converterr(
|
return converterr(
|
||||||
"invalid use of 'w' format character",
|
"(invalid use of 'w' format character)",
|
||||||
arg, msgbuf, bufsize);
|
arg, msgbuf, bufsize);
|
||||||
format++;
|
format++;
|
||||||
|
|
||||||
|
@ -1261,7 +1269,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return converterr("impossible<bad format char>", arg, msgbuf, bufsize);
|
return converterr("(impossible<bad format char>)", arg, msgbuf, bufsize);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue