Skip to content

Commit 582acec

Browse files
committed
Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
tests.
1 parent 6f2a5ef commit 582acec

9 files changed

Lines changed: 40 additions & 21 deletions

File tree

Python/ceval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2876,7 +2876,7 @@ exec_statement(f, prog, globals, locals)
28762876
}
28772877
else {
28782878
char *s = PyString_AsString(prog);
2879-
if ((int)strlen(s) != PyString_Size(prog)) {
2879+
if (strlen(s) != (size_t)PyString_Size(prog)) {
28802880
PyErr_SetString(PyExc_ValueError,
28812881
"embedded '\\0' in exec string");
28822882
return -1;

Python/codecs.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,16 @@ static
8383
PyObject *normalizestring(const char *string)
8484
{
8585
register int i;
86-
int len = strlen(string);
86+
size_t len = strlen(string);
8787
char *p;
8888
PyObject *v;
8989

90-
v = PyString_FromStringAndSize(NULL, len);
90+
if (len > INT_MAX) {
91+
PyErr_SetString(PyExc_OverflowError, "string is too large");
92+
return NULL;
93+
}
94+
95+
v = PyString_FromStringAndSize(NULL, (int)len);
9196
if (v == NULL)
9297
return NULL;
9398
p = PyString_AS_STRING(v);

Python/compile.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ PyCode_New(argcount, nlocals, stacksize, flags,
265265
if (!PyString_Check(v))
266266
continue;
267267
p = PyString_AsString(v);
268-
if ((int)strspn(p, NAME_CHARS)
269-
!= PyString_Size(v))
268+
if (strspn(p, NAME_CHARS)
269+
!= (size_t)PyString_Size(v))
270270
continue;
271271
PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
272272
}
@@ -340,7 +340,7 @@ com_error(c, exc, msg)
340340
PyObject *exc;
341341
char *msg;
342342
{
343-
int n = strlen(msg);
343+
size_t n = strlen(msg);
344344
PyObject *v;
345345
char buffer[30];
346346
char *s;
@@ -720,12 +720,12 @@ com_mangle(c, name, buffer, maxlen)
720720
struct compiling *c;
721721
char *name;
722722
char *buffer;
723-
int maxlen;
723+
size_t maxlen;
724724
{
725725
/* Name mangling: __private becomes _classname__private.
726726
This is independent from how the name is used. */
727727
char *p;
728-
int nlen, plen;
728+
size_t nlen, plen;
729729
nlen = strlen(name);
730730
if (nlen+2 >= maxlen)
731731
return 0; /* Don't mangle __extremely_long_names */
@@ -761,7 +761,7 @@ com_addopnamestr(c, op, name)
761761
char buffer[256];
762762
if (name != NULL && name[0] == '_' && name[1] == '_' &&
763763
c->c_private != NULL &&
764-
com_mangle(c, name, buffer, (int)sizeof(buffer)))
764+
com_mangle(c, name, buffer, sizeof(buffer)))
765765
name = buffer;
766766
#endif
767767
if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
@@ -883,7 +883,7 @@ parsestr(s)
883883
char *s;
884884
{
885885
PyObject *v;
886-
int len;
886+
size_t len;
887887
char *buf;
888888
char *p;
889889
char *end;
@@ -908,6 +908,10 @@ parsestr(s)
908908
}
909909
s++;
910910
len = strlen(s);
911+
if (len > INT_MAX) {
912+
PyErr_SetString(PyExc_OverflowError, "string to parse is too long");
913+
return NULL;
914+
}
911915
if (s[--len] != quote) {
912916
PyErr_BadInternalCall();
913917
return NULL;
@@ -2201,7 +2205,7 @@ com_global_stmt(c, n)
22012205
char buffer[256];
22022206
if (s != NULL && s[0] == '_' && s[1] == '_' &&
22032207
c->c_private != NULL &&
2204-
com_mangle(c, s, buffer, (int)sizeof(buffer)))
2208+
com_mangle(c, s, buffer, sizeof(buffer)))
22052209
s = buffer;
22062210
#endif
22072211
if (PyDict_GetItemString(c->c_locals, s) != NULL) {

Python/dynload_win.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
104104
"DLL load failed with error code %d",
105105
errorCode);
106106
} else {
107-
int len;
107+
size_t len;
108108
/* For some reason a \r\n
109109
is appended to the text */
110110
if (theLength >= 2 &&

Python/getcwd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ getcwd(buf, size)
6262
return NULL;
6363
}
6464
ret = getwd(localbuf);
65-
if (ret != NULL && strlen(localbuf) >= size) {
65+
if (ret != NULL && strlen(localbuf) >= (size_t)size) {
6666
errno = ERANGE;
6767
return NULL;
6868
}

Python/modsupport.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,15 @@ do_mkvalue(p_format, p_va)
355355
Py_INCREF(v);
356356
}
357357
else {
358-
if (n < 0)
359-
n = strlen(str);
358+
if (n < 0) {
359+
size_t m = strlen(str);
360+
if (m > INT_MAX) {
361+
PyErr_SetString(PyExc_OverflowError,
362+
"string too long for Python string");
363+
return NULL;
364+
}
365+
n = (int)m;
366+
}
360367
v = PyString_FromStringAndSize(str, n);
361368
}
362369
return v;

Python/sysmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ _PySys_Init()
504504
Py_XDECREF(v);
505505
#ifdef MS_COREDLL
506506
PyDict_SetItemString(sysdict, "dllhandle",
507-
v = PyInt_FromLong((int)PyWin_DLLhModule));
507+
v = PyLong_FromVoidPtr(PyWin_DLLhModule));
508508
Py_XDECREF(v);
509509
PyDict_SetItemString(sysdict, "winver",
510510
v = PyString_FromString(PyWin_DLLVersionString));

Python/thread_nt.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ BOOL InitializeNonRecursiveMutex(PNRMUTEX mutex)
101101
return mutex->hevent != NULL ; /* TRUE if the mutex is created */
102102
}
103103

104+
#ifdef InterlockedCompareExchange
105+
#undef InterlockedCompareExchange
106+
#endif
104107
#define InterlockedCompareExchange(dest,exchange,comperand) (ixchg((dest), (exchange), (comperand)))
105108

106109
VOID DeleteNonRecursiveMutex(PNRMUTEX mutex)
@@ -179,7 +182,7 @@ static void PyThread__init_thread(void)
179182
*/
180183
int PyThread_start_new_thread(void (*func)(void *), void *arg)
181184
{
182-
long rv;
185+
INT_PTR rv;
183186
int success = 0;
184187

185188
dprintf(("%ld: PyThread_start_new_thread called\n", PyThread_get_thread_ident()));
@@ -190,7 +193,7 @@ int PyThread_start_new_thread(void (*func)(void *), void *arg)
190193

191194
if (rv != -1) {
192195
success = 1;
193-
dprintf(("%ld: PyThread_start_new_thread succeeded: %ld\n", PyThread_get_thread_ident(), rv));
196+
dprintf(("%ld: PyThread_start_new_thread succeeded: %p\n", PyThread_get_thread_ident(), rv));
194197
}
195198

196199
return success;

Python/traceback.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ tb_displayline(f, filename, lineno, name)
166166
path = PySys_GetObject("path");
167167
if (path != NULL && PyList_Check(path)) {
168168
int npath = PyList_Size(path);
169-
int taillen = strlen(tail);
169+
size_t taillen = strlen(tail);
170170
char namebuf[MAXPATHLEN+1];
171171
for (i = 0; i < npath; i++) {
172172
PyObject *v = PyList_GetItem(path, i);
@@ -175,12 +175,12 @@ tb_displayline(f, filename, lineno, name)
175175
break;
176176
}
177177
if (PyString_Check(v)) {
178-
int len;
178+
size_t len;
179179
len = PyString_Size(v);
180180
if (len + 1 + taillen >= MAXPATHLEN)
181181
continue; /* Too long */
182182
strcpy(namebuf, PyString_AsString(v));
183-
if ((int)strlen(namebuf) != len)
183+
if (strlen(namebuf) != len)
184184
continue; /* v contains '\0' */
185185
if (len > 0 && namebuf[len-1] != SEP)
186186
namebuf[len++] = SEP;

0 commit comments

Comments
 (0)