]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[2.7] bpo-38730: Fix -Wstringop-truncation warnings. (GH-17075)
authorBenjamin Peterson <benjamin@python.org>
Thu, 7 Nov 2019 15:06:28 +0000 (07:06 -0800)
committerGitHub <noreply@github.com>
Thu, 7 Nov 2019 15:06:28 +0000 (07:06 -0800)
Misc/NEWS.d/next/Build/2019-11-06-20-53-54.bpo-38730.UQsW_r.rst [new file with mode: 0644]
Modules/getpath.c
Modules/parsermodule.c
Modules/readline.c
Modules/signalmodule.c
Modules/zipimport.c
Objects/structseq.c
Python/compile.c

diff --git a/Misc/NEWS.d/next/Build/2019-11-06-20-53-54.bpo-38730.UQsW_r.rst b/Misc/NEWS.d/next/Build/2019-11-06-20-53-54.bpo-38730.UQsW_r.rst
new file mode 100644 (file)
index 0000000..08e4e04
--- /dev/null
@@ -0,0 +1 @@
+Fix problems identified by GCC's ``-Wstringop-truncation`` warning.
index c42ce31178593fa3308cec74aa364320e0d566e9..092ccc712f9d08446c3cbe74b543310368f66a93 100644 (file)
@@ -486,7 +486,7 @@ calculate_path(void)
             if (tmpbuffer[0] == SEP)
                 /* tmpbuffer should never be longer than MAXPATHLEN,
                    but extra check does not hurt */
-                strncpy(argv0_path, tmpbuffer, MAXPATHLEN);
+                strncpy(argv0_path, tmpbuffer, MAXPATHLEN + 1);
             else {
                 /* Interpret relative to progpath */
                 reduce(argv0_path);
index fcc618d5d90a6f25d12be6260cc99e22c8155984..759f0ff4f6f85063d7e5957d1849d80ade43ce32 100644 (file)
@@ -1055,14 +1055,15 @@ validate_numnodes(node *n, int num, const char *const name)
 static int
 validate_terminal(node *terminal, int type, char *string)
 {
-    int res = (validate_ntype(terminal, type)
-               && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
-
-    if (!res && !PyErr_Occurred()) {
+    if (!validate_ntype(terminal, type)) {
+        return 0;
+    }
+    if (string != NULL && strcmp(string, STR(terminal)) != 0) {
         PyErr_Format(parser_error,
                      "Illegal terminal: expected \"%s\"", string);
+        return 0;
     }
-    return (res);
+    return 1;
 }
 
 
index 02621358238ea76343a8ec84eb0fd08370f81922..64bb19ae3fa37fdc3b2b0660b1567fcd79dc3ad6 100644 (file)
@@ -1180,7 +1180,7 @@ call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
     q = p;
     p = PyMem_Malloc(n+2);
     if (p != NULL) {
-        strncpy(p, q, n);
+        memcpy(p, q, n);
         p[n] = '\n';
         p[n+1] = '\0';
     }
index 8628f7a8005ff7d305de58014fb914fce714af51..88b471193214d59160d9afc7940f86409c30b61c 100644 (file)
@@ -173,8 +173,10 @@ trip_signal(int sig_num)
        cleared in PyErr_CheckSignals() before .tripped. */
     is_tripped = 1;
     Py_AddPendingCall(checksignals_witharg, NULL);
-    if (wakeup_fd != -1)
-        write(wakeup_fd, "\0", 1);
+    if (wakeup_fd != -1) {
+        int rc = write(wakeup_fd, "\0", 1);
+        (void)rc;
+    }
 }
 
 static void
index 1691773ec10f2dd274eca9e27094725c4ca0d734..8ec2475571ca6190ddcaac97473990d953e45d27 100644 (file)
@@ -714,8 +714,8 @@ read_directory(const char *archive)
     unsigned int count, i;
     unsigned char buffer[46];
     size_t length;
-    char path[MAXPATHLEN + 5];
-    char name[MAXPATHLEN + 5];
+    char name[MAXPATHLEN + 1];
+    char path[2*MAXPATHLEN + 2]; /* archive + SEP + name + '\0' */
     const char *errmsg = NULL;
 
     if (strlen(archive) > MAXPATHLEN) {
@@ -838,7 +838,7 @@ read_directory(const char *archive)
             }
         }
 
-        strncpy(path + length + 1, name, MAXPATHLEN - length - 1);
+        memcpy(path + length + 1, name, name_size + 1);
 
         t = Py_BuildValue("sHIIkHHI", path, compress, data_size,
                           file_size, file_offset, time, date, crc);
index aee95286f78bcf88173ab756d6b81fe3221be7ae..14b51688a65a27d8481d4129f574987d8e3148bb 100644 (file)
@@ -252,10 +252,7 @@ structseq_repr(PyStructSequence *obj)
     }
 
     /* "typename(", limited to  TYPE_MAXSIZE */
-    len = strlen(typ->tp_name) > TYPE_MAXSIZE ? TYPE_MAXSIZE :
-                            strlen(typ->tp_name);
-    strncpy(pbuf, typ->tp_name, len);
-    pbuf += len;
+    pbuf = stpncpy(pbuf, typ->tp_name, TYPE_MAXSIZE);
     *pbuf++ = '(';
 
     for (i=0; i < VISIBLE_SIZE(obj); i++) {
index 4fe69e12bf84840e3ad0d041c77110537d9d0ba0..6386a40731ed68e13af94b001592f8207b6438c4 100644 (file)
@@ -221,7 +221,7 @@ _Py_Mangle(PyObject *privateobj, PyObject *ident)
     }
     plen = strlen(p);
 
-    if (plen + nlen >= PY_SSIZE_T_MAX - 1) {
+    if (nlen >= PY_SSIZE_T_MAX - 1 - plen) {
         PyErr_SetString(PyExc_OverflowError,
                         "private identifier too large to be mangled");
         return NULL;
@@ -233,7 +233,7 @@ _Py_Mangle(PyObject *privateobj, PyObject *ident)
     /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
     buffer = PyString_AS_STRING(ident);
     buffer[0] = '_';
-    strncpy(buffer+1, p, plen);
+    memcpy(buffer+1, p, plen);
     strcpy(buffer+1+plen, name);
     return ident;
 }