Skip to content

Commit f32bcf8

Browse files
authored
[2.7] bpo-38730: Fix -Wstringop-truncation warnings. (pythonGH-17075)
1 parent 089e5f5 commit f32bcf8

File tree

8 files changed

+19
-18
lines changed

8 files changed

+19
-18
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix problems identified by GCC's ``-Wstringop-truncation`` warning.

Modules/getpath.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ calculate_path(void)
486486
if (tmpbuffer[0] == SEP)
487487
/* tmpbuffer should never be longer than MAXPATHLEN,
488488
but extra check does not hurt */
489-
strncpy(argv0_path, tmpbuffer, MAXPATHLEN);
489+
strncpy(argv0_path, tmpbuffer, MAXPATHLEN + 1);
490490
else {
491491
/* Interpret relative to progpath */
492492
reduce(argv0_path);

Modules/parsermodule.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,14 +1055,15 @@ validate_numnodes(node *n, int num, const char *const name)
10551055
static int
10561056
validate_terminal(node *terminal, int type, char *string)
10571057
{
1058-
int res = (validate_ntype(terminal, type)
1059-
&& ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
1060-
1061-
if (!res && !PyErr_Occurred()) {
1058+
if (!validate_ntype(terminal, type)) {
1059+
return 0;
1060+
}
1061+
if (string != NULL && strcmp(string, STR(terminal)) != 0) {
10621062
PyErr_Format(parser_error,
10631063
"Illegal terminal: expected \"%s\"", string);
1064+
return 0;
10641065
}
1065-
return (res);
1066+
return 1;
10661067
}
10671068

10681069

Modules/readline.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,7 @@ call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
11801180
q = p;
11811181
p = PyMem_Malloc(n+2);
11821182
if (p != NULL) {
1183-
strncpy(p, q, n);
1183+
memcpy(p, q, n);
11841184
p[n] = '\n';
11851185
p[n+1] = '\0';
11861186
}

Modules/signalmodule.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,10 @@ trip_signal(int sig_num)
173173
cleared in PyErr_CheckSignals() before .tripped. */
174174
is_tripped = 1;
175175
Py_AddPendingCall(checksignals_witharg, NULL);
176-
if (wakeup_fd != -1)
177-
write(wakeup_fd, "\0", 1);
176+
if (wakeup_fd != -1) {
177+
int rc = write(wakeup_fd, "\0", 1);
178+
(void)rc;
179+
}
178180
}
179181

180182
static void

Modules/zipimport.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -714,8 +714,8 @@ read_directory(const char *archive)
714714
unsigned int count, i;
715715
unsigned char buffer[46];
716716
size_t length;
717-
char path[MAXPATHLEN + 5];
718-
char name[MAXPATHLEN + 5];
717+
char name[MAXPATHLEN + 1];
718+
char path[2*MAXPATHLEN + 2]; /* archive + SEP + name + '\0' */
719719
const char *errmsg = NULL;
720720

721721
if (strlen(archive) > MAXPATHLEN) {
@@ -838,7 +838,7 @@ read_directory(const char *archive)
838838
}
839839
}
840840

841-
strncpy(path + length + 1, name, MAXPATHLEN - length - 1);
841+
memcpy(path + length + 1, name, name_size + 1);
842842

843843
t = Py_BuildValue("sHIIkHHI", path, compress, data_size,
844844
file_size, file_offset, time, date, crc);

Objects/structseq.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,7 @@ structseq_repr(PyStructSequence *obj)
252252
}
253253

254254
/* "typename(", limited to TYPE_MAXSIZE */
255-
len = strlen(typ->tp_name) > TYPE_MAXSIZE ? TYPE_MAXSIZE :
256-
strlen(typ->tp_name);
257-
strncpy(pbuf, typ->tp_name, len);
258-
pbuf += len;
255+
pbuf = stpncpy(pbuf, typ->tp_name, TYPE_MAXSIZE);
259256
*pbuf++ = '(';
260257

261258
for (i=0; i < VISIBLE_SIZE(obj); i++) {

Python/compile.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ _Py_Mangle(PyObject *privateobj, PyObject *ident)
221221
}
222222
plen = strlen(p);
223223

224-
if (plen + nlen >= PY_SSIZE_T_MAX - 1) {
224+
if (nlen >= PY_SSIZE_T_MAX - 1 - plen) {
225225
PyErr_SetString(PyExc_OverflowError,
226226
"private identifier too large to be mangled");
227227
return NULL;
@@ -233,7 +233,7 @@ _Py_Mangle(PyObject *privateobj, PyObject *ident)
233233
/* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
234234
buffer = PyString_AS_STRING(ident);
235235
buffer[0] = '_';
236-
strncpy(buffer+1, p, plen);
236+
memcpy(buffer+1, p, plen);
237237
strcpy(buffer+1+plen, name);
238238
return ident;
239239
}

0 commit comments

Comments
 (0)