Skip to content

Commit 0c644fc

Browse files
authored
fix up signedness in PyImport_ExtendInittab (#4831)
As a result of 92a3c6f, the compiler complains: Python/import.c:2311:21: warning: comparison of integers of different signs: 'long' and 'unsigned long' [-Wsign-compare] if ((i + n + 1) <= PY_SSIZE_T_MAX / sizeof(struct _inittab)) { ~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This overflow is extremely unlikely to happen, but let's avoid undefined behavior anyway.
1 parent 9454060 commit 0c644fc

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

Python/import.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,7 +2291,7 @@ int
22912291
PyImport_ExtendInittab(struct _inittab *newtab)
22922292
{
22932293
struct _inittab *p;
2294-
Py_ssize_t i, n;
2294+
size_t i, n;
22952295
int res = 0;
22962296

22972297
/* Count the number of entries in both tables */
@@ -2308,13 +2308,11 @@ PyImport_ExtendInittab(struct _inittab *newtab)
23082308
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
23092309

23102310
/* Allocate new memory for the combined table */
2311-
if ((i + n + 1) <= PY_SSIZE_T_MAX / (Py_ssize_t)sizeof(struct _inittab)) {
2311+
p = NULL;
2312+
if (i + n <= SIZE_MAX / sizeof(struct _inittab) - 1) {
23122313
size_t size = sizeof(struct _inittab) * (i + n + 1);
23132314
p = PyMem_RawRealloc(inittab_copy, size);
23142315
}
2315-
else {
2316-
p = NULL;
2317-
}
23182316
if (p == NULL) {
23192317
res = -1;
23202318
goto done;

0 commit comments

Comments
 (0)