Skip to content

Commit e43e1a4

Browse files
author
neal.norwitz
committed
Fix more memory allocation issues found with failmalloc.
git-svn-id: http://svn.python.org/projects/python/trunk@50773 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent eaef929 commit e43e1a4

File tree

6 files changed

+74
-36
lines changed

6 files changed

+74
-36
lines changed

Modules/bz2module.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,8 +1348,10 @@ BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs)
13481348

13491349
#ifdef WITH_THREAD
13501350
self->lock = PyThread_allocate_lock();
1351-
if (!self->lock)
1351+
if (!self->lock) {
1352+
PyErr_SetString(PyExc_MemoryError, "unable to allocate lock");
13521353
goto error;
1354+
}
13531355
#endif
13541356

13551357
if (mode_char == 'r')
@@ -1371,10 +1373,12 @@ BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs)
13711373
return 0;
13721374

13731375
error:
1374-
Py_DECREF(self->file);
1376+
Py_CLEAR(self->file);
13751377
#ifdef WITH_THREAD
1376-
if (self->lock)
1378+
if (self->lock) {
13771379
PyThread_free_lock(self->lock);
1380+
self->lock = NULL;
1381+
}
13781382
#endif
13791383
return -1;
13801384
}
@@ -1682,8 +1686,10 @@ BZ2Comp_init(BZ2CompObject *self, PyObject *args, PyObject *kwargs)
16821686

16831687
#ifdef WITH_THREAD
16841688
self->lock = PyThread_allocate_lock();
1685-
if (!self->lock)
1689+
if (!self->lock) {
1690+
PyErr_SetString(PyExc_MemoryError, "unable to allocate lock");
16861691
goto error;
1692+
}
16871693
#endif
16881694

16891695
memset(&self->bzs, 0, sizeof(bz_stream));
@@ -1698,8 +1704,10 @@ BZ2Comp_init(BZ2CompObject *self, PyObject *args, PyObject *kwargs)
16981704
return 0;
16991705
error:
17001706
#ifdef WITH_THREAD
1701-
if (self->lock)
1707+
if (self->lock) {
17021708
PyThread_free_lock(self->lock);
1709+
self->lock = NULL;
1710+
}
17031711
#endif
17041712
return -1;
17051713
}
@@ -1894,8 +1902,10 @@ BZ2Decomp_init(BZ2DecompObject *self, PyObject *args, PyObject *kwargs)
18941902

18951903
#ifdef WITH_THREAD
18961904
self->lock = PyThread_allocate_lock();
1897-
if (!self->lock)
1905+
if (!self->lock) {
1906+
PyErr_SetString(PyExc_MemoryError, "unable to allocate lock");
18981907
goto error;
1908+
}
18991909
#endif
19001910

19011911
self->unused_data = PyString_FromString("");
@@ -1915,10 +1925,12 @@ BZ2Decomp_init(BZ2DecompObject *self, PyObject *args, PyObject *kwargs)
19151925

19161926
error:
19171927
#ifdef WITH_THREAD
1918-
if (self->lock)
1928+
if (self->lock) {
19191929
PyThread_free_lock(self->lock);
1930+
self->lock = NULL;
1931+
}
19201932
#endif
1921-
Py_XDECREF(self->unused_data);
1933+
Py_CLEAR(self->unused_data);
19221934
return -1;
19231935
}
19241936

Modules/cPickle.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ Pdata_clear(Pdata *self, int clearto)
196196
for (i = self->length, p = self->data + clearto;
197197
--i >= clearto;
198198
p++) {
199-
Py_DECREF(*p);
199+
Py_CLEAR(*p);
200200
}
201201
self->length = clearto;
202202

@@ -208,6 +208,7 @@ Pdata_grow(Pdata *self)
208208
{
209209
int bigger;
210210
size_t nbytes;
211+
PyObject **tmp;
211212

212213
bigger = self->size << 1;
213214
if (bigger <= 0) /* was 0, or new value overflows */
@@ -217,14 +218,14 @@ Pdata_grow(Pdata *self)
217218
nbytes = (size_t)bigger * sizeof(PyObject *);
218219
if (nbytes / sizeof(PyObject *) != (size_t)bigger)
219220
goto nomemory;
220-
self->data = realloc(self->data, nbytes);
221-
if (self->data == NULL)
221+
tmp = realloc(self->data, nbytes);
222+
if (tmp == NULL)
222223
goto nomemory;
224+
self->data = tmp;
223225
self->size = bigger;
224226
return 0;
225227

226228
nomemory:
227-
self->size = 0;
228229
PyErr_NoMemory();
229230
return -1;
230231
}
@@ -4163,6 +4164,7 @@ do_append(Unpicklerobject *self, int x)
41634164
int list_len;
41644165

41654166
slice=Pdata_popList(self->stack, x);
4167+
if (! slice) return -1;
41664168
list_len = PyList_GET_SIZE(list);
41674169
i=PyList_SetSlice(list, list_len, list_len, slice);
41684170
Py_DECREF(slice);
@@ -5167,6 +5169,9 @@ newUnpicklerobject(PyObject *f)
51675169
if (!( self->memo = PyDict_New()))
51685170
goto err;
51695171

5172+
if (!self->stack)
5173+
goto err;
5174+
51705175
Py_INCREF(f);
51715176
self->file = f;
51725177

Python/compile.c

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,11 @@ PyCodeObject *
300300
PyNode_Compile(struct _node *n, const char *filename)
301301
{
302302
PyCodeObject *co = NULL;
303+
mod_ty mod;
303304
PyArena *arena = PyArena_New();
304-
mod_ty mod = PyAST_FromNode(n, NULL, filename, arena);
305+
if (!arena)
306+
return NULL;
307+
mod = PyAST_FromNode(n, NULL, filename, arena);
305308
if (mod)
306309
co = PyAST_Compile(mod, filename, NULL, arena);
307310
PyArena_Free(arena);
@@ -615,8 +618,10 @@ markblocks(unsigned char *code, int len)
615618
unsigned int *blocks = (unsigned int *)PyMem_Malloc(len*sizeof(int));
616619
int i,j, opcode, blockcnt = 0;
617620

618-
if (blocks == NULL)
621+
if (blocks == NULL) {
622+
PyErr_NoMemory();
619623
return NULL;
624+
}
620625
memset(blocks, 0, len*sizeof(int));
621626

622627
/* Mark labels in the first pass */
@@ -1071,14 +1076,14 @@ compiler_unit_free(struct compiler_unit *u)
10711076
PyObject_Free((void *)b);
10721077
b = next;
10731078
}
1074-
Py_XDECREF(u->u_ste);
1075-
Py_XDECREF(u->u_name);
1076-
Py_XDECREF(u->u_consts);
1077-
Py_XDECREF(u->u_names);
1078-
Py_XDECREF(u->u_varnames);
1079-
Py_XDECREF(u->u_freevars);
1080-
Py_XDECREF(u->u_cellvars);
1081-
Py_XDECREF(u->u_private);
1079+
Py_CLEAR(u->u_ste);
1080+
Py_CLEAR(u->u_name);
1081+
Py_CLEAR(u->u_consts);
1082+
Py_CLEAR(u->u_names);
1083+
Py_CLEAR(u->u_varnames);
1084+
Py_CLEAR(u->u_freevars);
1085+
Py_CLEAR(u->u_cellvars);
1086+
Py_CLEAR(u->u_private);
10821087
PyObject_Free(u);
10831088
}
10841089

@@ -1139,7 +1144,8 @@ compiler_enter_scope(struct compiler *c, identifier name, void *key,
11391144
/* Push the old compiler_unit on the stack. */
11401145
if (c->u) {
11411146
PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL);
1142-
if (PyList_Append(c->c_stack, wrapper) < 0) {
1147+
if (!wrapper || PyList_Append(c->c_stack, wrapper) < 0) {
1148+
Py_XDECREF(wrapper);
11431149
compiler_unit_free(u);
11441150
return 0;
11451151
}
@@ -1265,6 +1271,7 @@ compiler_next_instr(struct compiler *c, basicblock *b)
12651271
sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
12661272
}
12671273
else if (b->b_iused == b->b_ialloc) {
1274+
struct instr *tmp;
12681275
size_t oldsize, newsize;
12691276
oldsize = b->b_ialloc * sizeof(struct instr);
12701277
newsize = oldsize << 1;
@@ -1273,10 +1280,13 @@ compiler_next_instr(struct compiler *c, basicblock *b)
12731280
return -1;
12741281
}
12751282
b->b_ialloc <<= 1;
1276-
b->b_instr = (struct instr *)PyObject_Realloc(
1283+
tmp = (struct instr *)PyObject_Realloc(
12771284
(void *)b->b_instr, newsize);
1278-
if (b->b_instr == NULL)
1285+
if (tmp == NULL) {
1286+
PyErr_NoMemory();
12791287
return -1;
1288+
}
1289+
b->b_instr = tmp;
12801290
memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
12811291
}
12821292
return b->b_iused++;

Python/pyarena.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,19 +132,19 @@ PyArena_New()
132132
{
133133
PyArena* arena = (PyArena *)malloc(sizeof(PyArena));
134134
if (!arena)
135-
return NULL;
135+
return (PyArena*)PyErr_NoMemory();
136136

137137
arena->a_head = block_new(DEFAULT_BLOCK_SIZE);
138138
arena->a_cur = arena->a_head;
139139
if (!arena->a_head) {
140140
free((void *)arena);
141-
return NULL;
141+
return (PyArena*)PyErr_NoMemory();
142142
}
143143
arena->a_objects = PyList_New(0);
144144
if (!arena->a_objects) {
145145
block_free(arena->a_head);
146146
free((void *)arena);
147-
return NULL;
147+
return (PyArena*)PyErr_NoMemory();
148148
}
149149
#if defined(Py_DEBUG)
150150
arena->total_allocs = 0;
@@ -191,7 +191,7 @@ PyArena_Malloc(PyArena *arena, size_t size)
191191
{
192192
void *p = block_alloc(arena->a_cur, size);
193193
if (!p)
194-
return NULL;
194+
return PyErr_NoMemory();
195195
#if defined(Py_DEBUG)
196196
arena->total_allocs++;
197197
arena->total_size += size;

Python/pythonrun.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,11 @@ PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags
746746
ps2 = PyString_AsString(w);
747747
}
748748
arena = PyArena_New();
749+
if (arena == NULL) {
750+
Py_XDECREF(v);
751+
Py_XDECREF(w);
752+
return -1;
753+
}
749754
mod = PyParser_ASTFromFile(fp, filename,
750755
Py_single_input, ps1, ps2,
751756
flags, &errcode, arena);
@@ -1203,9 +1208,8 @@ PyRun_StringFlags(const char *str, int start, PyObject *globals,
12031208
PyObject *locals, PyCompilerFlags *flags)
12041209
{
12051210
PyObject *ret = NULL;
1206-
PyArena *arena = PyArena_New();
12071211
mod_ty mod;
1208-
1212+
PyArena *arena = PyArena_New();
12091213
if (arena == NULL)
12101214
return NULL;
12111215

@@ -1221,9 +1225,8 @@ PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
12211225
PyObject *locals, int closeit, PyCompilerFlags *flags)
12221226
{
12231227
PyObject *ret;
1224-
PyArena *arena = PyArena_New();
12251228
mod_ty mod;
1226-
1229+
PyArena *arena = PyArena_New();
12271230
if (arena == NULL)
12281231
return NULL;
12291232

@@ -1291,8 +1294,12 @@ Py_CompileStringFlags(const char *str, const char *filename, int start,
12911294
PyCompilerFlags *flags)
12921295
{
12931296
PyCodeObject *co;
1297+
mod_ty mod;
12941298
PyArena *arena = PyArena_New();
1295-
mod_ty mod = PyParser_ASTFromString(str, filename, start, flags, arena);
1299+
if (arena == NULL)
1300+
return NULL;
1301+
1302+
mod = PyParser_ASTFromString(str, filename, start, flags, arena);
12961303
if (mod == NULL) {
12971304
PyArena_Free(arena);
12981305
return NULL;
@@ -1311,8 +1318,12 @@ struct symtable *
13111318
Py_SymtableString(const char *str, const char *filename, int start)
13121319
{
13131320
struct symtable *st;
1321+
mod_ty mod;
13141322
PyArena *arena = PyArena_New();
1315-
mod_ty mod = PyParser_ASTFromString(str, filename, start, NULL, arena);
1323+
if (arena == NULL)
1324+
return NULL;
1325+
1326+
mod = PyParser_ASTFromString(str, filename, start, NULL, arena);
13161327
if (mod == NULL) {
13171328
PyArena_Free(arena);
13181329
return NULL;

Python/symtable.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ symtable_exit_block(struct symtable *st, void *ast)
727727
{
728728
Py_ssize_t end;
729729

730-
Py_DECREF(st->st_cur);
730+
Py_CLEAR(st->st_cur);
731731
end = PyList_GET_SIZE(st->st_stack) - 1;
732732
if (end >= 0) {
733733
st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,

0 commit comments

Comments
 (0)