[Python-checkins] Fix compiler warning in structseq_repr() (GH-10841)

Victor Stinner webhook-mailer at python.org
Fri Nov 30 20:46:52 EST 2018

commit: 989052047eea7f35da0d7ca268791b2442ee1553
branch: master
author: Victor Stinner <vstinner at redhat.com>
committer: GitHub <noreply at github.com>
date: 2018-12-01T02:46:40+01:00

Fix compiler warning in structseq_repr() (GH-10841)

Replace strncpy() with memcpy() in structseq_repr() to fix the
following compiler warning:

Objects/structseq.c:187:5: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-overflow=]
     strncpy(pbuf, typ->tp_name, len);
Objects/structseq.c:185:11: note: length computed here
     len = strlen(typ->tp_name) > TYPE_MAXSIZE ? TYPE_MAXSIZE :

The function writes the terminating NUL byte later.

M Objects/structseq.c

diff --git a/Objects/structseq.c b/Objects/structseq.c
index 05ea87b67a82..cf94155f18f8 100644
--- a/Objects/structseq.c
+++ b/Objects/structseq.c
@@ -182,9 +182,9 @@ structseq_repr(PyStructSequence *obj)
     endofbuf= &buf[REPR_BUFFER_SIZE-5];
     /* "typename(", limited to  TYPE_MAXSIZE */
-    len = strlen(typ->tp_name) > TYPE_MAXSIZE ? TYPE_MAXSIZE :
-                            strlen(typ->tp_name);
-    strncpy(pbuf, typ->tp_name, len);
+    len = strlen(typ->tp_name);
+    len = Py_MIN(len, TYPE_MAXSIZE);
+    memcpy(pbuf, typ->tp_name, len);
     pbuf += len;
     *pbuf++ = '(';

More information about the Python-checkins mailing list