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

Skip to content

Commit 7fdcb41

Browse files
committed
Fix SF bug # 561858 Assertion with very long lists
Write 4 bytes for co_stacksize, etc. to prevent writing out bad .pyc files which can cause a crash when read back in.
1 parent 1f68fc7 commit 7fdcb41

3 files changed

Lines changed: 43 additions & 12 deletions

File tree

Lib/test/test_import.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import random
55
import sys
6+
import py_compile
67

78
# Brief digression to test that import is case-sensitive: if we got this
89
# far, we know for sure that "random" exists.
@@ -74,3 +75,33 @@ def test_with_extension(ext): # ext normally ".py"; perhaps ".pyw"
7475
import imp
7576
x = imp.find_module("os")
7677
os = imp.load_module("os", *x)
78+
79+
def test_module_with_large_stack(module):
80+
# create module w/list of 65000 elements to test bug #561858
81+
filename = module + '.py'
82+
83+
# create a file with a list of 65000 elements
84+
f = open(filename, 'w+')
85+
f.write('d = [\n')
86+
for i in range(65000):
87+
f.write('"",\n')
88+
f.write(']')
89+
f.close()
90+
91+
# compile & remove .py file, we only need .pyc
92+
f = open(filename, 'r')
93+
py_compile.compile(filename)
94+
os.unlink(filename)
95+
96+
# need to be able to load from current dir
97+
sys.path.append('')
98+
99+
# this used to crash
100+
exec 'import ' + module
101+
102+
# cleanup
103+
del sys.path[-1]
104+
os.unlink(module + '.pyc')
105+
106+
test_module_with_large_stack('longlist')
107+

Python/import.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ extern time_t PyOS_GetLastModificationTime(char *, FILE *);
5959
Python 2.1.1: 60202
6060
Python 2.1.2: 60202
6161
Python 2.2: 60717
62-
Python 2.3a0: 62001
62+
Python 2.3a0: 62011
6363
*/
64-
#define MAGIC (62001 | ((long)'\r'<<16) | ((long)'\n'<<24))
64+
#define MAGIC (62011 | ((long)'\r'<<16) | ((long)'\n'<<24))
6565

6666
/* Magic word as global; note that _PyImport_Init() can change the
6767
value of this global to accommodate for alterations of how the

Python/marshal.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,10 @@ w_object(PyObject *v, WFILE *p)
241241
else if (PyCode_Check(v)) {
242242
PyCodeObject *co = (PyCodeObject *)v;
243243
w_byte(TYPE_CODE, p);
244-
w_short(co->co_argcount, p);
245-
w_short(co->co_nlocals, p);
246-
w_short(co->co_stacksize, p);
247-
w_short(co->co_flags, p);
244+
w_long(co->co_argcount, p);
245+
w_long(co->co_nlocals, p);
246+
w_long(co->co_stacksize, p);
247+
w_long(co->co_flags, p);
248248
w_object(co->co_code, p);
249249
w_object(co->co_consts, p);
250250
w_object(co->co_names, p);
@@ -253,7 +253,7 @@ w_object(PyObject *v, WFILE *p)
253253
w_object(co->co_cellvars, p);
254254
w_object(co->co_filename, p);
255255
w_object(co->co_name, p);
256-
w_short(co->co_firstlineno, p);
256+
w_long(co->co_firstlineno, p);
257257
w_object(co->co_lnotab, p);
258258
}
259259
else if (PyObject_CheckReadBuffer(v)) {
@@ -588,10 +588,10 @@ r_object(RFILE *p)
588588
return NULL;
589589
}
590590
else {
591-
int argcount = r_short(p);
592-
int nlocals = r_short(p);
593-
int stacksize = r_short(p);
594-
int flags = r_short(p);
591+
int argcount = r_long(p);
592+
int nlocals = r_long(p);
593+
int stacksize = r_long(p);
594+
int flags = r_long(p);
595595
PyObject *code = NULL;
596596
PyObject *consts = NULL;
597597
PyObject *names = NULL;
@@ -612,7 +612,7 @@ r_object(RFILE *p)
612612
if (cellvars) filename = r_object(p);
613613
if (filename) name = r_object(p);
614614
if (name) {
615-
firstlineno = r_short(p);
615+
firstlineno = r_long(p);
616616
lnotab = r_object(p);
617617
}
618618

0 commit comments

Comments
 (0)