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

Skip to content

Commit 4af5c8c

Browse files
committed
SF #1444030: Fix several potential defects found by Coverity.
(reviewed by Neal Norwitz)
1 parent ef1701f commit 4af5c8c

11 files changed

Lines changed: 53 additions & 17 deletions

File tree

Modules/arraymodule.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,10 +1852,13 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
18521852
Py_DECREF(v);
18531853
}
18541854
} else if (initial != NULL && PyString_Check(initial)) {
1855-
PyObject *t_initial = PyTuple_Pack(1,
1856-
initial);
1857-
PyObject *v =
1858-
array_fromstring((arrayobject *)a,
1855+
PyObject *t_initial, *v;
1856+
t_initial = PyTuple_Pack(1, initial);
1857+
if (t_initial == NULL) {
1858+
Py_DECREF(a);
1859+
return NULL;
1860+
}
1861+
v = array_fromstring((arrayobject *)a,
18591862
t_initial);
18601863
Py_DECREF(t_initial);
18611864
if (v == NULL) {

Modules/cStringIO.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ newOobject(int size) {
544544
if (!self->buf) {
545545
PyErr_SetString(PyExc_MemoryError,"out of memory");
546546
self->buf_size = 0;
547+
Py_DECREF(self);
547548
return NULL;
548549
}
549550

Modules/zipimport.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,8 @@ initzipimport(void)
11671167

11681168
mod = Py_InitModule4("zipimport", NULL, zipimport_doc,
11691169
NULL, PYTHON_API_VERSION);
1170+
if (mod == NULL)
1171+
return;
11701172

11711173
ZipImportError = PyErr_NewException("zipimport.ZipImportError",
11721174
PyExc_ImportError, NULL);

Objects/longobject.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2809,6 +2809,8 @@ long_bitwise(PyLongObject *a,
28092809

28102810
if (a->ob_size < 0) {
28112811
a = (PyLongObject *) long_invert(a);
2812+
if (a == NULL)
2813+
return NULL;
28122814
maska = MASK;
28132815
}
28142816
else {
@@ -2817,6 +2819,10 @@ long_bitwise(PyLongObject *a,
28172819
}
28182820
if (b->ob_size < 0) {
28192821
b = (PyLongObject *) long_invert(b);
2822+
if (b == NULL) {
2823+
Py_DECREF(a);
2824+
return NULL;
2825+
}
28202826
maskb = MASK;
28212827
}
28222828
else {
@@ -2868,7 +2874,7 @@ long_bitwise(PyLongObject *a,
28682874
: (maskb ? size_a : MIN(size_a, size_b)))
28692875
: MAX(size_a, size_b);
28702876
z = _PyLong_New(size_z);
2871-
if (a == NULL || b == NULL || z == NULL) {
2877+
if (z == NULL) {
28722878
Py_XDECREF(a);
28732879
Py_XDECREF(b);
28742880
Py_XDECREF(z);

Objects/stringobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3276,7 +3276,7 @@ string_splitlines(PyStringObject *self, PyObject *args)
32763276
return list;
32773277

32783278
onError:
3279-
Py_DECREF(list);
3279+
Py_XDECREF(list);
32803280
return NULL;
32813281
}
32823282

Objects/unicodeobject.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,16 +1876,16 @@ PyObject *PyUnicode_DecodeUnicodeEscape(const char *s,
18761876
message = "malformed \\N character escape";
18771877
if (ucnhash_CAPI == NULL) {
18781878
/* load the unicode data module */
1879-
PyObject *m, *v;
1879+
PyObject *m, *api;
18801880
m = PyImport_ImportModule("unicodedata");
18811881
if (m == NULL)
18821882
goto ucnhashError;
1883-
v = PyObject_GetAttrString(m, "ucnhash_CAPI");
1883+
api = PyObject_GetAttrString(m, "ucnhash_CAPI");
18841884
Py_DECREF(m);
1885-
if (v == NULL)
1885+
if (api == NULL)
18861886
goto ucnhashError;
1887-
ucnhash_CAPI = PyCObject_AsVoidPtr(v);
1888-
Py_DECREF(v);
1887+
ucnhash_CAPI = PyCObject_AsVoidPtr(api);
1888+
Py_DECREF(api);
18891889
if (ucnhash_CAPI == NULL)
18901890
goto ucnhashError;
18911891
}
@@ -1945,6 +1945,7 @@ PyObject *PyUnicode_DecodeUnicodeEscape(const char *s,
19451945
PyExc_UnicodeError,
19461946
"\\N escapes not supported (can't load unicodedata module)"
19471947
);
1948+
Py_XDECREF(v);
19481949
Py_XDECREF(errorHandler);
19491950
Py_XDECREF(exc);
19501951
return NULL;
@@ -3962,7 +3963,7 @@ Py_ssize_t PyUnicode_Tailmatch(PyObject *str,
39623963
return -1;
39633964
substr = PyUnicode_FromObject(substr);
39643965
if (substr == NULL) {
3965-
Py_DECREF(substr);
3966+
Py_DECREF(str);
39663967
return -1;
39673968
}
39683969

@@ -4429,7 +4430,7 @@ PyObject *PyUnicode_Splitlines(PyObject *string,
44294430
return list;
44304431

44314432
onError:
4432-
Py_DECREF(list);
4433+
Py_XDECREF(list);
44334434
Py_DECREF(string);
44344435
return NULL;
44354436
}
@@ -6679,6 +6680,10 @@ formatlong(PyObject *val, int flags, int prec, int type)
66796680
if (!str)
66806681
return NULL;
66816682
result = _PyUnicode_New(len);
6683+
if (!result) {
6684+
Py_DECREF(str);
6685+
return NULL;
6686+
}
66826687
for (i = 0; i < len; i++)
66836688
result->str[i] = buf[i];
66846689
result->str[len] = 0;
@@ -6865,7 +6870,7 @@ PyObject *PyUnicode_Format(PyObject *format,
68656870
rescnt = fmtcnt + 100;
68666871
reslen += rescnt;
68676872
if (_PyUnicode_Resize(&result, reslen) < 0)
6868-
return NULL;
6873+
goto onError;
68696874
res = PyUnicode_AS_UNICODE(result) + reslen - rescnt;
68706875
--rescnt;
68716876
}
@@ -7163,6 +7168,7 @@ PyObject *PyUnicode_Format(PyObject *format,
71637168
rescnt = width + fmtcnt + 100;
71647169
reslen += rescnt;
71657170
if (reslen < 0) {
7171+
Py_XDECREF(temp);
71667172
Py_DECREF(result);
71677173
return PyErr_NoMemory();
71687174
}

Objects/weakrefobject.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,8 +903,15 @@ PyObject_ClearWeakRefs(PyObject *object)
903903
}
904904
}
905905
else {
906-
PyObject *tuple = PyTuple_New(count * 2);
906+
PyObject *tuple;
907907
Py_ssize_t i = 0;
908+
909+
tuple = PyTuple_New(count * 2);
910+
if (tuple == NULL) {
911+
if (restore_error)
912+
PyErr_Fetch(&err_type, &err_value, &err_tb);
913+
return;
914+
}
908915

909916
for (i = 0; i < count; ++i) {
910917
PyWeakReference *next = current->wr_next;

Parser/firstsets.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,6 @@ calcfirstset(grammar *g, dfa *d)
107107
}
108108
printf(" }\n");
109109
}
110+
111+
PyMem_FREE(sym);
110112
}

Python/ast.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,6 +1438,8 @@ ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
14381438
}
14391439
/* extract Index values and put them in a Tuple */
14401440
elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
1441+
if (!elts)
1442+
return NULL;
14411443
for (j = 0; j < asdl_seq_LEN(slices); ++j) {
14421444
slc = (slice_ty)asdl_seq_GET(slices, j);
14431445
assert(slc->kind == Index_kind && slc->v.Index.value);

Python/ceval.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3477,8 +3477,11 @@ PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
34773477
{
34783478
PyObject *result;
34793479

3480-
if (arg == NULL)
3480+
if (arg == NULL) {
34813481
arg = PyTuple_New(0);
3482+
if (arg == NULL)
3483+
return NULL;
3484+
}
34823485
else if (!PyTuple_Check(arg)) {
34833486
PyErr_SetString(PyExc_TypeError,
34843487
"argument list must be a tuple");

0 commit comments

Comments
 (0)