Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 106f2da

Browse files
committed
Trent Mick:
Various small fixes to the builtin module to ensure no buffer overflows. - chunk #1: Proper casting to ensure no truncation, and hence no surprises, in the comparison. - chunk #2: The id() function guarantees a unique return value for different objects. It does this by returning the pointer to the object. By returning a PyInt, on Win64 (sizeof(long) < sizeof(void*)) the pointer is truncated and the guarantee may be proven false. The appropriate return function is PyLong_FromVoidPtr, this returns a PyLong if that is necessary to return the pointer without truncation. [GvR: note that this means that id() can now return a long on Win32 platforms. This *might* break some code...] - chunk #3: Ensure no overflow in raw_input(). Granted the user would have to pass in >2GB of data but it *is* a possible buffer overflow condition.
1 parent 7388f73 commit 106f2da

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

Python/bltinmodule.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ builtin_eval(self, args)
832832
return NULL;
833833
}
834834
str = PyString_AsString(cmd);
835-
if ((int)strlen(str) != PyString_Size(cmd)) {
835+
if (strlen(str) != (size_t)PyString_Size(cmd)) {
836836
PyErr_SetString(PyExc_ValueError,
837837
"embedded '\\0' in string arg");
838838
return NULL;
@@ -985,7 +985,7 @@ builtin_id(self, args)
985985

986986
if (!PyArg_ParseTuple(args, "O:id", &v))
987987
return NULL;
988-
return PyInt_FromLong((long)v);
988+
return PyLong_FromVoidPtr(v);
989989
}
990990

991991
static char id_doc[] =
@@ -1873,7 +1873,14 @@ builtin_raw_input(self, args)
18731873
result = NULL;
18741874
}
18751875
else { /* strip trailing '\n' */
1876-
result = PyString_FromStringAndSize(s, strlen(s)-1);
1876+
size_t len = strlen(s);
1877+
if (len > INT_MAX) {
1878+
PyErr_SetString(PyExc_OverflowError, "input too long");
1879+
result = NULL;
1880+
}
1881+
else {
1882+
result = PyString_FromStringAndSize(s, (int)(len-1));
1883+
}
18771884
}
18781885
PyMem_FREE(s);
18791886
return result;

0 commit comments

Comments
 (0)