Skip to content

Commit 7782c4b

Browse files
author
neal.norwitz
committed
Fix:
* crashes on memory allocation failure found with failmalloc * memory leaks found with valgrind * compiler warnings in opt mode which would lead to invalid memory reads * problem using wrong name in decimal module reported by pychecker Update the valgrind suppressions file with new leaks that are small/one-time leaks we don't care about (ie, they are too hard to fix). TBR=barry TESTED=./python -E -tt ./Lib/test/regrtest.py -uall (both debug and opt modes) in opt mode: valgrind -q --leak-check=yes --suppressions=Misc/valgrind-python.supp \ ./python -E -tt ./Lib/test/regrtest.py -uall,-bsddb,-compiler \ -x test_logging test_ssl test_multiprocessing valgrind -q --leak-check=yes --suppressions=Misc/valgrind-python.supp \ ./python -E -tt ./Lib/test/regrtest.py test_multiprocessing for i in `seq 1 4000` ; do LD_PRELOAD=~/local/lib/libfailmalloc.so FAILMALLOC_INTERVAL=$i \ ./python -c pass done At least some of these fixes should probably be backported to 2.5. git-svn-id: http://svn.python.org/projects/python/trunk@66006 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent ce7c4e6 commit 7782c4b

File tree

10 files changed

+75
-14
lines changed

10 files changed

+75
-14
lines changed

Lib/decimal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5061,7 +5061,7 @@ def _dlog10(c, e, p):
50615061
log_tenpower = f*M # exact
50625062
else:
50635063
log_d = 0 # error < 2.31
5064-
log_tenpower = div_nearest(f, 10**-p) # error < 0.5
5064+
log_tenpower = _div_nearest(f, 10**-p) # error < 0.5
50655065

50665066
return _div_nearest(log_tenpower+log_d, 100)
50675067

Misc/NEWS

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ What's New in Python 2.6 release candidate 1?
1212
Core and Builtins
1313
-----------------
1414

15+
- Fix crashes on memory allocation failure found with failmalloc.
16+
17+
- Fix memory leaks found with valgrind and update suppressions file.
18+
19+
- Fix compiler warnings in opt mode which would lead to invalid memory reads.
20+
21+
- Fix problem using wrong name in decimal module reported by pychecker.
22+
1523
- Issue #3642: Changed type of numarenas from uint to size_t
1624
in order to silence a compilier warning on 64bit OSes.
1725

Misc/valgrind-python.supp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,39 @@
4646
# Will need to fix that.
4747
#
4848

49+
{
50+
Suppress leaking the GIL. Happens once per process, see comment in ceval.c.
51+
Memcheck:Leak
52+
fun:malloc
53+
fun:PyThread_allocate_lock
54+
fun:PyEval_InitThreads
55+
}
56+
57+
{
58+
Suppress leaking the GIL after a fork.
59+
Memcheck:Leak
60+
fun:malloc
61+
fun:PyThread_allocate_lock
62+
fun:PyEval_ReInitThreads
63+
}
64+
65+
{
66+
Suppress leaking the autoTLSkey. This looks like it shouldn't leak though.
67+
Memcheck:Leak
68+
fun:malloc
69+
fun:PyThread_create_key
70+
fun:_PyGILState_Init
71+
fun:Py_InitializeEx
72+
fun:Py_Main
73+
}
74+
75+
{
76+
Hmmm, is this a real leak or like the GIL?
77+
Memcheck:Leak
78+
fun:malloc
79+
fun:PyThread_ReInitTLS
80+
}
81+
4982
{
5083
Handle PyMalloc confusing valgrind (possibly leaked)
5184
Memcheck:Leak

Modules/_ctypes/stgdict.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ StructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct)
416416
ffi_ofs = 0;
417417
}
418418

419+
assert(stgdict->format == NULL);
419420
if (isStruct && !isPacked) {
420421
stgdict->format = alloc_format_string(NULL, "T{");
421422
} else {
@@ -539,7 +540,9 @@ StructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct)
539540
#undef realdict
540541

541542
if (isStruct && !isPacked) {
543+
char *ptr = stgdict->format;
542544
stgdict->format = alloc_format_string(stgdict->format, "}");
545+
PyMem_Free(ptr);
543546
if (stgdict->format == NULL)
544547
return -1;
545548
}

Modules/_fileio.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
278278
ret = -1;
279279

280280
done:
281+
PyMem_Free(name);
281282
return ret;
282283
}
283284

Modules/signalmodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,8 @@ initsignal(void)
784784
#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
785785
ItimerError = PyErr_NewException("signal.ItimerError",
786786
PyExc_IOError, NULL);
787-
PyDict_SetItemString(d, "ItimerError", ItimerError);
787+
if (ItimerError != NULL)
788+
PyDict_SetItemString(d, "ItimerError", ItimerError);
788789
#endif
789790

790791
if (!PyErr_Occurred())

Objects/stringlib/formatter.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,10 @@ format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
641641
/* We know this can't fail, since we've already
642642
reserved enough space. */
643643
STRINGLIB_CHAR *pstart = p + n_leading_chars;
644-
int r = STRINGLIB_GROUPING(pstart, n_digits, n_digits,
644+
#ifndef NDEBUG
645+
int r =
646+
#endif
647+
STRINGLIB_GROUPING(pstart, n_digits, n_digits,
645648
spec.n_total+n_grouping_chars-n_leading_chars,
646649
NULL, 0);
647650
assert(r);

Objects/structseq.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ PyStructSequence_New(PyTypeObject *type)
3232
PyStructSequence *obj;
3333

3434
obj = PyObject_New(PyStructSequence, type);
35+
if (obj == NULL)
36+
return NULL;
3537
Py_SIZE(obj) = VISIBLE_SIZE_TP(type);
3638

3739
return (PyObject*) obj;
@@ -522,10 +524,16 @@ PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc)
522524
Py_INCREF(type);
523525

524526
dict = type->tp_dict;
525-
PyDict_SetItemString(dict, visible_length_key,
526-
PyInt_FromLong((long) desc->n_in_sequence));
527-
PyDict_SetItemString(dict, real_length_key,
528-
PyInt_FromLong((long) n_members));
529-
PyDict_SetItemString(dict, unnamed_fields_key,
530-
PyInt_FromLong((long) n_unnamed_members));
527+
#define SET_DICT_FROM_INT(key, value) \
528+
do { \
529+
PyObject *v = PyInt_FromLong((long) value); \
530+
if (v != NULL) { \
531+
PyDict_SetItemString(dict, key, v); \
532+
Py_DECREF(v); \
533+
} \
534+
} while (0)
535+
536+
SET_DICT_FROM_INT(visible_length_key, desc->n_in_sequence);
537+
SET_DICT_FROM_INT(real_length_key, n_members);
538+
SET_DICT_FROM_INT(unnamed_fields_key, n_unnamed_members);
531539
}

Python/getargs.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,7 +1312,7 @@ convertbuffer(PyObject *arg, void **p, char **errmsg)
13121312
}
13131313

13141314
static int
1315-
getbuffer(PyObject *arg, Py_buffer *view, char**errmsg)
1315+
getbuffer(PyObject *arg, Py_buffer *view, char **errmsg)
13161316
{
13171317
void *buf;
13181318
Py_ssize_t count;
@@ -1322,8 +1322,10 @@ getbuffer(PyObject *arg, Py_buffer *view, char**errmsg)
13221322
return -1;
13231323
}
13241324
if (pb->bf_getbuffer) {
1325-
if (pb->bf_getbuffer(arg, view, 0) < 0)
1325+
if (pb->bf_getbuffer(arg, view, 0) < 0) {
1326+
*errmsg = "convertible to a buffer";
13261327
return -1;
1328+
}
13271329
if (!PyBuffer_IsContiguous(view, 'C')) {
13281330
*errmsg = "contiguous buffer";
13291331
return -1;
@@ -1332,8 +1334,10 @@ getbuffer(PyObject *arg, Py_buffer *view, char**errmsg)
13321334
}
13331335

13341336
count = convertbuffer(arg, &buf, errmsg);
1335-
if (count < 0)
1337+
if (count < 0) {
1338+
*errmsg = "convertible to a buffer";
13361339
return count;
1340+
}
13371341
PyBuffer_FillInfo(view, NULL, buf, count, 1, 0);
13381342
return 0;
13391343
}

Python/pythonrun.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ Py_InitializeEx(int install_sigs)
132132
PyThreadState *tstate;
133133
PyObject *bimod, *sysmod;
134134
char *p;
135-
char *icodeset; /* On Windows, input codeset may theoretically
136-
differ from output codeset. */
135+
char *icodeset = NULL; /* On Windows, input codeset may theoretically
136+
differ from output codeset. */
137137
char *codeset = NULL;
138138
char *errors = NULL;
139139
int free_codeset = 0;

0 commit comments

Comments
 (0)