Skip to content

Commit 725507b

Browse files
committed
Change int to Py_ssize_t in several places.
Add (int) casts to silence compiler warnings. Raise Python exceptions for overflows.
1 parent 8eb8a82 commit 725507b

File tree

11 files changed

+57
-32
lines changed

11 files changed

+57
-32
lines changed

Modules/posixmodule.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2317,9 +2317,9 @@ posix__exit(PyObject *self, PyObject *args)
23172317

23182318
#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
23192319
static void
2320-
free_string_array(char **array, int count)
2320+
free_string_array(char **array, Py_ssize_t count)
23212321
{
2322-
int i;
2322+
Py_ssize_t i;
23232323
for (i = 0; i < count; i++)
23242324
PyMem_Free(array[i]);
23252325
PyMem_DEL(array);
@@ -2341,7 +2341,7 @@ posix_execv(PyObject *self, PyObject *args)
23412341
char *path;
23422342
PyObject *argv;
23432343
char **argvlist;
2344-
int i, argc;
2344+
Py_ssize_t i, argc;
23452345
PyObject *(*getitem)(PyObject *, Py_ssize_t);
23462346

23472347
/* execv has two arguments: (path, argv), where
@@ -2410,7 +2410,7 @@ posix_execve(PyObject *self, PyObject *args)
24102410
char **argvlist;
24112411
char **envlist;
24122412
PyObject *key, *val, *keys=NULL, *vals=NULL;
2413-
int i, pos, argc, envc;
2413+
Py_ssize_t i, pos, argc, envc;
24142414
PyObject *(*getitem)(PyObject *, Py_ssize_t);
24152415
int lastarg = 0;
24162416

Objects/enumobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ static PyObject *
216216
reversed_next(reversedobject *ro)
217217
{
218218
PyObject *item;
219-
long index = ro->index;
219+
Py_ssize_t index = ro->index;
220220

221221
if (index >= 0) {
222222
item = PySequence_GetItem(ro->seq, index);

Objects/genobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ gen_del(PyObject *self)
177177
* never happened.
178178
*/
179179
{
180-
int refcnt = self->ob_refcnt;
180+
Py_ssize_t refcnt = self->ob_refcnt;
181181
_Py_NewReference(self);
182182
self->ob_refcnt = refcnt;
183183
}

Objects/object.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,7 @@ PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
11721172
PyObject **
11731173
_PyObject_GetDictPtr(PyObject *obj)
11741174
{
1175-
long dictoffset;
1175+
Py_ssize_t dictoffset;
11761176
PyTypeObject *tp = obj->ob_type;
11771177

11781178
if (!(tp->tp_flags & Py_TPFLAGS_HAVE_CLASS))
@@ -1212,7 +1212,7 @@ PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
12121212
PyObject *descr = NULL;
12131213
PyObject *res = NULL;
12141214
descrgetfunc f;
1215-
long dictoffset;
1215+
Py_ssize_t dictoffset;
12161216
PyObject **dictptr;
12171217

12181218
if (!PyString_Check(name)){

Objects/stringobject.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3679,6 +3679,7 @@ _PyString_FormatLong(PyObject *val, int flags, int prec, int type,
36793679
Py_ssize_t i;
36803680
int sign; /* 1 if '-', else 0 */
36813681
int len; /* number of characters */
3682+
Py_ssize_t llen;
36823683
int numdigits; /* len == numnondigits + numdigits */
36833684
int numnondigits = 0;
36843685

@@ -3707,7 +3708,12 @@ _PyString_FormatLong(PyObject *val, int flags, int prec, int type,
37073708
return NULL;
37083709
}
37093710
buf = PyString_AsString(result);
3710-
len = PyString_Size(result);
3711+
llen = PyString_Size(result);
3712+
if (llen > INT_MAX) {
3713+
PyErr_SetString(PyExc_ValueError, "string too large in _PyString_FormatLong");
3714+
return NULL;
3715+
}
3716+
len = (int)llen;
37113717
if (buf[len-1] == 'L') {
37123718
--len;
37133719
buf[len] = '\0';
@@ -3941,12 +3947,12 @@ PyString_Format(PyObject *format, PyObject *args)
39413947
PyObject *temp = NULL;
39423948
char *pbuf;
39433949
int sign;
3944-
int len;
3950+
Py_ssize_t len;
39453951
char formatbuf[FORMATBUFLEN];
39463952
/* For format{float,int,char}() */
39473953
#ifdef Py_USING_UNICODE
39483954
char *fmt_start = fmt;
3949-
int argidx_start = argidx;
3955+
Py_ssize_t argidx_start = argidx;
39503956
#endif
39513957

39523958
fmt++;
@@ -4139,8 +4145,10 @@ PyString_Format(PyObject *format, PyObject *args)
41394145
if (c == 'i')
41404146
c = 'd';
41414147
if (PyLong_Check(v)) {
4148+
int ilen;
41424149
temp = _PyString_FormatLong(v, flags,
4143-
prec, c, &pbuf, &len);
4150+
prec, c, &pbuf, &ilen);
4151+
len = ilen;
41444152
if (!temp)
41454153
goto error;
41464154
sign = 1;

Objects/typeobject.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4244,7 +4244,8 @@ slot_sq_contains(PyObject *self, PyObject *value)
42444244
}
42454245
}
42464246
else if (! PyErr_Occurred()) {
4247-
result = _PySequence_IterSearch(self, value,
4247+
/* Possible results: -1 and 1 */
4248+
result = (int)_PySequence_IterSearch(self, value,
42484249
PY_ITERSEARCH_CONTAINS);
42494250
}
42504251
return result;
@@ -4880,7 +4881,7 @@ slot_tp_del(PyObject *self)
48804881
* never happened.
48814882
*/
48824883
{
4883-
int refcnt = self->ob_refcnt;
4884+
Py_ssize_t refcnt = self->ob_refcnt;
48844885
_Py_NewReference(self);
48854886
self->ob_refcnt = refcnt;
48864887
}

Python/bltinmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2514,7 +2514,7 @@ static PyObject *
25142514
filterunicode(PyObject *func, PyObject *strobj)
25152515
{
25162516
PyObject *result;
2517-
register int i, j;
2517+
register Py_ssize_t i, j;
25182518
Py_ssize_t len = PyUnicode_GetSize(strobj);
25192519
Py_ssize_t outlen = len;
25202520

Python/exceptions.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ static PyObject *
764764
SyntaxError__init__(PyObject *self, PyObject *args)
765765
{
766766
PyObject *rtnval = NULL;
767-
int lenargs;
767+
Py_ssize_t lenargs;
768768

769769
if (!(self = get_self(args)))
770770
return NULL;
@@ -889,7 +889,7 @@ SyntaxError__str__(PyObject *self, PyObject *args)
889889
PyErr_Clear();
890890

891891
if (have_filename || have_lineno) {
892-
int bufsize = PyString_GET_SIZE(str) + 64;
892+
Py_ssize_t bufsize = PyString_GET_SIZE(str) + 64;
893893
if (have_filename)
894894
bufsize += PyString_GET_SIZE(filename);
895895

Python/import.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ write_compiled_module(PyCodeObject *co, char *cpathname, time_t mtime)
853853
/* Now write the true mtime */
854854
fseek(fp, 4L, 0);
855855
assert(mtime < LONG_MAX);
856-
PyMarshal_WriteLongToFile(mtime, fp, Py_MARSHAL_VERSION);
856+
PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
857857
fflush(fp);
858858
fclose(fp);
859859
if (Py_VerboseFlag)
@@ -1016,7 +1016,7 @@ get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
10161016
PyObject *p)
10171017
{
10181018
PyObject *importer;
1019-
int j, nhooks;
1019+
Py_ssize_t j, nhooks;
10201020

10211021
/* These conditions are the caller's responsibility: */
10221022
assert(PyList_Check(path_hooks));
@@ -1075,7 +1075,7 @@ static struct filedescr *
10751075
find_module(char *fullname, char *subname, PyObject *path, char *buf,
10761076
size_t buflen, FILE **p_fp, PyObject **p_loader)
10771077
{
1078-
int i, npath;
1078+
Py_ssize_t i, npath;
10791079
size_t len, namelen;
10801080
struct filedescr *fdp = NULL;
10811081
char *filemode;
@@ -2028,7 +2028,7 @@ get_parent(PyObject *globals, char *buf, Py_ssize_t *p_buflen, int level)
20282028

20292029
modpath = PyDict_GetItem(globals, pathstr);
20302030
if (modpath != NULL) {
2031-
int len = PyString_GET_SIZE(modname);
2031+
Py_ssize_t len = PyString_GET_SIZE(modname);
20322032
if (len > MAXPATHLEN) {
20332033
PyErr_SetString(PyExc_ValueError,
20342034
"Module name too long");

Python/marshal.c

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ w_object(PyObject *v, WFILE *p)
186186
n = strlen(buf);
187187
w_byte(TYPE_FLOAT, p);
188188
w_byte((int)n, p);
189-
w_string(buf, n, p);
189+
w_string(buf, (int)n, p);
190190
}
191191
}
192192
#ifndef WITHOUT_COMPLEX
@@ -215,16 +215,16 @@ w_object(PyObject *v, WFILE *p)
215215
PyComplex_RealAsDouble(v));
216216
PyFloat_AsReprString(buf, temp);
217217
Py_DECREF(temp);
218-
n = (int)strlen(buf);
219-
w_byte(n, p);
220-
w_string(buf, n, p);
218+
n = strlen(buf);
219+
w_byte((int)n, p);
220+
w_string(buf, (int)n, p);
221221
temp = (PyFloatObject*)PyFloat_FromDouble(
222222
PyComplex_ImagAsDouble(v));
223223
PyFloat_AsReprString(buf, temp);
224224
Py_DECREF(temp);
225-
n = (int)strlen(buf);
226-
w_byte(n, p);
227-
w_string(buf, n, p);
225+
n = strlen(buf);
226+
w_byte((int)n, p);
227+
w_string(buf, (int)n, p);
228228
}
229229
}
230230
#endif
@@ -248,8 +248,14 @@ w_object(PyObject *v, WFILE *p)
248248
w_byte(TYPE_STRING, p);
249249
}
250250
n = PyString_GET_SIZE(v);
251+
if (n > INT_MAX) {
252+
/* huge strings are not supported */
253+
p->depth--;
254+
p->error = 1;
255+
return;
256+
}
251257
w_long((long)n, p);
252-
w_string(PyString_AS_STRING(v), n, p);
258+
w_string(PyString_AS_STRING(v), (int)n, p);
253259
}
254260
#ifdef Py_USING_UNICODE
255261
else if (PyUnicode_Check(v)) {
@@ -262,8 +268,13 @@ w_object(PyObject *v, WFILE *p)
262268
}
263269
w_byte(TYPE_UNICODE, p);
264270
n = PyString_GET_SIZE(utf8);
271+
if (n > INT_MAX) {
272+
p->depth--;
273+
p->error = 1;
274+
return;
275+
}
265276
w_long((long)n, p);
266-
w_string(PyString_AS_STRING(utf8), n, p);
277+
w_string(PyString_AS_STRING(utf8), (int)n, p);
267278
Py_DECREF(utf8);
268279
}
269280
#endif
@@ -350,8 +361,13 @@ w_object(PyObject *v, WFILE *p)
350361
PyBufferProcs *pb = v->ob_type->tp_as_buffer;
351362
w_byte(TYPE_STRING, p);
352363
n = (*pb->bf_getreadbuffer)(v, 0, (void **)&s);
364+
if (n > INT_MAX) {
365+
p->depth--;
366+
p->error = 1;
367+
return;
368+
}
353369
w_long((long)n, p);
354-
w_string(s, n, p);
370+
w_string(s, (int)n, p);
355371
}
356372
else {
357373
w_byte(TYPE_UNKNOWN, p);

Python/sysmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ sys_mdebug(PyObject *self, PyObject *args)
597597
static PyObject *
598598
sys_getrefcount(PyObject *self, PyObject *arg)
599599
{
600-
return PyInt_FromLong(arg->ob_refcnt);
600+
return PyInt_FromSsize_t(arg->ob_refcnt);
601601
}
602602

603603
#ifdef Py_REF_DEBUG

0 commit comments

Comments
 (0)