]> granicus.if.org Git - python/commitdiff
Issue #26198: Fixed error messages for some argument parsing errors.
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 7 Feb 2016 23:05:48 +0000 (01:05 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Sun, 7 Feb 2016 23:05:48 +0000 (01:05 +0200)
Fixed the documented about buffer overflow error for "es#" and "et#" format
units.

Doc/c-api/arg.rst
Python/getargs.c

index 8fbdc500b5fbd0593f6dc60a7f82c7c656a1a35e..61fe93735639e5eafcc71ca9972659143c31a517 100644 (file)
@@ -136,7 +136,8 @@ area.  Also, you won't have to release any memory yourself, except with 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 encoded data into the buffer and NUL-terminate it.  If
-   the buffer is not large enough, a :exc:`ValueError` will be set.
+   the buffer is not large 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
    without the trailing NUL byte.
index 81a27217bb5b4a8079fec1f57e4c91e0f56d46b0..bd15c2e08991ed772e574771bd0a63b094ab59ca 100644 (file)
@@ -346,7 +346,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);
         }
     }
@@ -533,9 +533,17 @@ 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);
+        strncpy(msgbuf, expected, bufsize);
+        msgbuf[bufsize-1] = '\0';
+    }
+    else {
+        PyOS_snprintf(msgbuf, bufsize,
+                      "must be %.50s, not %.50s", expected,
+                      arg == Py_None ? "None" : arg->ob_type->tp_name);
+    }
     return msgbuf;
 }
 
@@ -753,7 +761,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
         else if (PyLong_Check(arg))
             ival = PyLong_AsUnsignedLongMask(arg);
         else
-            return converterr("integer<k>", arg, msgbuf, bufsize);
+            return converterr("an integer", arg, msgbuf, bufsize);
         *p = ival;
         break;
     }
@@ -781,7 +789,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
         else if (PyLong_Check(arg))
             ival = PyLong_AsUnsignedLongLongMask(arg);
         else
-            return converterr("integer<K>", arg, msgbuf, bufsize);
+            return converterr("an integer", arg, msgbuf, bufsize);
         *p = ival;
         break;
     }
@@ -1127,9 +1135,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 "";
                 }
             }
             memcpy(*buffer,
@@ -1154,7 +1164,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
                                                     != 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);
@@ -1261,7 +1271,6 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
         break;
     }
 
-
     case 'w': { /* memory buffer, read-write access */
         void **p = va_arg(*p_va, void **);
         void *res;
@@ -1353,7 +1362,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
     }
 
     default:
-        return converterr("impossible<bad format char>", arg, msgbuf, bufsize);
+        return converterr("(impossible<bad format char>)", arg, msgbuf, bufsize);
 
     }