From e61cceeeacea42566240bc7635bc8cc2f4168cf1 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 15 Aug 2014 23:30:40 +0200 Subject: Issue #22156: Fix "comparison between signed and unsigned integers" compiler warnings in the Python/ subdirectory. --- Python/getargs.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'Python/getargs.c') diff --git a/Python/getargs.c b/Python/getargs.c index 946faf2d7e..a313269658 100644 --- a/Python/getargs.c +++ b/Python/getargs.c @@ -872,7 +872,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags, STORE_SIZE(count); format++; } else { - if (strlen(*p) != count) + if (strlen(*p) != (size_t)count) return converterr( "bytes without null bytes", arg, msgbuf, bufsize); @@ -994,7 +994,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags, *p = PyUnicode_AsUnicodeAndSize(arg, &len); if (*p == NULL) RETURN_ERR_OCCURRED; - if (Py_UNICODE_strlen(*p) != len) + if (Py_UNICODE_strlen(*p) != (size_t)len) return converterr( "str without null characters or None", arg, msgbuf, bufsize); -- cgit v1.2.1 From b6c677f13497ce8dabc8aad7fb272b09dfeccb56 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sat, 6 Sep 2014 20:07:17 +0300 Subject: Issue #22215: Now ValueError is raised instead of TypeError when str or bytes argument contains not permitted null character or byte. --- Python/getargs.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'Python/getargs.c') diff --git a/Python/getargs.c b/Python/getargs.c index a313269658..c749bb643c 100644 --- a/Python/getargs.c +++ b/Python/getargs.c @@ -872,10 +872,10 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags, STORE_SIZE(count); format++; } else { - if (strlen(*p) != (size_t)count) - return converterr( - "bytes without null bytes", - arg, msgbuf, bufsize); + if (strlen(*p) != (size_t)count) { + PyErr_SetString(PyExc_ValueError, "embedded null byte"); + RETURN_ERR_OCCURRED; + } } break; } @@ -948,16 +948,15 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags, if (sarg == NULL) return converterr(CONV_UNICODE, arg, msgbuf, bufsize); + if (strlen(sarg) != (size_t)len) { + PyErr_SetString(PyExc_ValueError, "embedded null character"); + RETURN_ERR_OCCURRED; + } *p = sarg; } else return converterr(c == 'z' ? "str or None" : "str", arg, msgbuf, bufsize); - if (*p != NULL && sarg != NULL && (Py_ssize_t) strlen(*p) != len) - return converterr( - c == 'z' ? "str without null characters or None" - : "str without null characters", - arg, msgbuf, bufsize); } break; } @@ -994,10 +993,10 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags, *p = PyUnicode_AsUnicodeAndSize(arg, &len); if (*p == NULL) RETURN_ERR_OCCURRED; - if (Py_UNICODE_strlen(*p) != (size_t)len) - return converterr( - "str without null characters or None", - arg, msgbuf, bufsize); + if (Py_UNICODE_strlen(*p) != (size_t)len) { + PyErr_SetString(PyExc_ValueError, "embedded null character"); + RETURN_ERR_OCCURRED; + } } else return converterr(c == 'Z' ? "str or None" : "str", arg, msgbuf, bufsize); -- cgit v1.2.1 From 61d5d0b23f4fbcd7648478914717d681abe592e6 Mon Sep 17 00:00:00 2001 From: R David Murray Date: Sun, 5 Oct 2014 11:47:01 -0400 Subject: #16518: Bring error messages in harmony with docs ("bytes-like object") Some time ago we changed the docs to consistently use the term 'bytes-like object' in all the contexts where bytes, bytearray, memoryview, etc are used. This patch (by Ezio Melotti) completes that work by changing the error messages that previously reported that certain types did "not support the buffer interface" to instead say that a bytes-like object is required. (The glossary entry for bytes-like object references the discussion of the buffer protocol in the docs.) --- Python/getargs.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'Python/getargs.c') diff --git a/Python/getargs.c b/Python/getargs.c index c749bb643c..f7297da5db 100644 --- a/Python/getargs.c +++ b/Python/getargs.c @@ -1244,7 +1244,8 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags, supports it directly. */ if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) { PyErr_Clear(); - return converterr("read-write buffer", arg, msgbuf, bufsize); + return converterr("read-write bytes-like object", + arg, msgbuf, bufsize); } if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C')) { PyBuffer_Release((Py_buffer*)p); @@ -1282,7 +1283,7 @@ convertbuffer(PyObject *arg, void **p, char **errmsg) *errmsg = NULL; *p = NULL; if (pb != NULL && pb->bf_releasebuffer != NULL) { - *errmsg = "read-only pinned buffer"; + *errmsg = "read-only bytes-like object"; return -1; } @@ -1298,7 +1299,7 @@ static int getbuffer(PyObject *arg, Py_buffer *view, char **errmsg) { if (PyObject_GetBuffer(arg, view, PyBUF_SIMPLE) != 0) { - *errmsg = "bytes or buffer"; + *errmsg = "bytes-like object"; return -1; } if (!PyBuffer_IsContiguous(view, 'C')) { -- cgit v1.2.1 From 3e1b525ff84fb94d2c853d1211a650f5580465d5 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 8 Feb 2016 01:06:11 +0200 Subject: Issue #26198: Fixed error messages for some argument parsing errors. Fixed the documented about buffer overflow error for "es#" and "et#" format units. --- Python/getargs.c | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'Python/getargs.c') diff --git a/Python/getargs.c b/Python/getargs.c index c365fc25a3..8aab067865 100644 --- a/Python/getargs.c +++ b/Python/getargs.c @@ -342,7 +342,7 @@ vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags) flags, levels, msgbuf, sizeof(msgbuf), &freelist); if (msg) { - seterror(i+1, msg, levels, fname, msg); + seterror(i+1, msg, levels, fname, message); return cleanreturn(0, &freelist); } } @@ -535,9 +535,15 @@ converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize) { assert(expected != NULL); assert(arg != NULL); - PyOS_snprintf(msgbuf, bufsize, - "must be %.50s, not %.50s", expected, - arg == Py_None ? "None" : arg->ob_type->tp_name); + if (expected[0] == '(') { + PyOS_snprintf(msgbuf, bufsize, + "%.100s", expected); + } + else { + PyOS_snprintf(msgbuf, bufsize, + "must be %.50s, not %.50s", expected, + arg == Py_None ? "None" : arg->ob_type->tp_name); + } return msgbuf; } @@ -741,7 +747,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags, if (PyLong_Check(arg)) ival = PyLong_AsUnsignedLongMask(arg); else - return converterr("integer", arg, msgbuf, bufsize); + return converterr("int", arg, msgbuf, bufsize); *p = ival; break; } @@ -766,7 +772,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags, if (PyLong_Check(arg)) ival = PyLong_AsUnsignedLongLongMask(arg); else - return converterr("integer", arg, msgbuf, bufsize); + return converterr("int", arg, msgbuf, bufsize); *p = ival; break; } @@ -1123,9 +1129,11 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags, } else { if (size + 1 > BUFFER_LEN) { Py_DECREF(s); - return converterr( - "(buffer overflow)", - arg, msgbuf, bufsize); + PyErr_Format(PyExc_TypeError, + "encoded string too long " + "(%zd, maximum length %zd)", + (Py_ssize_t)size, (Py_ssize_t)(BUFFER_LEN-1)); + RETURN_ERR_OCCURRED; } } 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) { Py_DECREF(s); return converterr( - "encoded string without NULL bytes", + "encoded string without null bytes", arg, msgbuf, bufsize); } *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 != '*') return converterr( - "invalid use of 'w' format character", + "(invalid use of 'w' format character)", arg, msgbuf, bufsize); format++; @@ -1261,7 +1269,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags, } default: - return converterr("impossible", arg, msgbuf, bufsize); + return converterr("(impossible)", arg, msgbuf, bufsize); } -- cgit v1.2.1