Skip to content

Commit 05d68a8

Browse files
authored
bpo-9566: Fix size_t=>int downcast warnings (#5230)
* Use wider types (int => Py_ssize_t) to avoid integer overflows. * Fix gc.get_freeze_count(): use Py_ssize_t type rather than int, since gc_list_size() returns a Py_ssize_t.
1 parent ab74504 commit 05d68a8

File tree

5 files changed

+14
-14
lines changed

5 files changed

+14
-14
lines changed

Modules/clinic/gcmodule.c.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,22 +307,22 @@ PyDoc_STRVAR(gc_get_freeze_count__doc__,
307307
#define GC_GET_FREEZE_COUNT_METHODDEF \
308308
{"get_freeze_count", (PyCFunction)gc_get_freeze_count, METH_NOARGS, gc_get_freeze_count__doc__},
309309

310-
static int
310+
static Py_ssize_t
311311
gc_get_freeze_count_impl(PyObject *module);
312312

313313
static PyObject *
314314
gc_get_freeze_count(PyObject *module, PyObject *Py_UNUSED(ignored))
315315
{
316316
PyObject *return_value = NULL;
317-
int _return_value;
317+
Py_ssize_t _return_value;
318318

319319
_return_value = gc_get_freeze_count_impl(module);
320320
if ((_return_value == -1) && PyErr_Occurred()) {
321321
goto exit;
322322
}
323-
return_value = PyLong_FromLong((long)_return_value);
323+
return_value = PyLong_FromSsize_t(_return_value);
324324

325325
exit:
326326
return return_value;
327327
}
328-
/*[clinic end generated code: output=6f9ee4d8dd1f36c1 input=a9049054013a1b77]*/
328+
/*[clinic end generated code: output=21dc9270b10b7891 input=a9049054013a1b77]*/

Modules/gcmodule.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,14 +1449,14 @@ gc_unfreeze_impl(PyObject *module)
14491449
}
14501450

14511451
/*[clinic input]
1452-
gc.get_freeze_count -> int
1452+
gc.get_freeze_count -> Py_ssize_t
14531453
14541454
Return the number of objects in the permanent generation.
14551455
[clinic start generated code]*/
14561456

1457-
static int
1457+
static Py_ssize_t
14581458
gc_get_freeze_count_impl(PyObject *module)
1459-
/*[clinic end generated code: output=e4e2ebcc77e5cbf3 input=4b759db880a3c6e4]*/
1459+
/*[clinic end generated code: output=61cbd9f43aa032e1 input=45ffbc65cfe2a6ed]*/
14601460
{
14611461
return gc_list_size(&_PyRuntime.gc.permanent_generation.head);
14621462
}

Python/ast_opt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ fold_compare(expr_ty node, PyArena *arena, int optimize)
397397
{
398398
asdl_int_seq *ops;
399399
asdl_seq *args;
400-
int i;
400+
Py_ssize_t i;
401401

402402
ops = node->v.Compare.ops;
403403
args = node->v.Compare.comparators;

Python/bltinmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ _Py_IDENTIFIER(stderr);
5252
#include "clinic/bltinmodule.c.h"
5353

5454
static PyObject*
55-
update_bases(PyObject *bases, PyObject *const *args, int nargs)
55+
update_bases(PyObject *bases, PyObject *const *args, Py_ssize_t nargs)
5656
{
57-
int i, j;
57+
Py_ssize_t i, j;
5858
PyObject *base, *meth, *new_base, *result, *new_bases = NULL;
5959
PyObject *stack[1] = {bases};
6060
assert(PyTuple_Check(bases));

Python/pathconfig.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,13 +367,12 @@ _Py_FindEnvConfigValue(FILE *env_file, const wchar_t *key,
367367
fseek(env_file, 0, SEEK_SET);
368368
while (!feof(env_file)) {
369369
char * p = fgets(buffer, MAXPATHLEN*2, env_file);
370-
wchar_t *tmpbuffer;
371-
int n;
372370

373371
if (p == NULL) {
374372
break;
375373
}
376-
n = strlen(p);
374+
375+
size_t n = strlen(p);
377376
if (p[n - 1] != '\n') {
378377
/* line has overflowed - bail */
379378
break;
@@ -382,7 +381,8 @@ _Py_FindEnvConfigValue(FILE *env_file, const wchar_t *key,
382381
/* Comment - skip */
383382
continue;
384383
}
385-
tmpbuffer = _Py_DecodeUTF8_surrogateescape(buffer, n);
384+
385+
wchar_t *tmpbuffer = _Py_DecodeUTF8_surrogateescape(buffer, n);
386386
if (tmpbuffer) {
387387
wchar_t * state;
388388
wchar_t * tok = wcstok(tmpbuffer, L" \t\r\n", &state);

0 commit comments

Comments
 (0)