Skip to content

Commit 230cc89

Browse files
[3.15] Fix unchecked NULL returns in several C modules (GH-160213) (#160262)
(cherry picked from commit a930e52)
1 parent 3fe5720 commit 230cc89

10 files changed

Lines changed: 58 additions & 6 deletions

File tree

‎Modules/_elementtree.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4210,6 +4210,9 @@ _elementtree_XMLParser__setevents_impl(XMLParserObject *self,
42104210
if (events_to_report == Py_None) {
42114211
/* default is "end" only */
42124212
target->end_event_obj = PyUnicode_FromString("end");
4213+
if (target->end_event_obj == NULL) {
4214+
return NULL;
4215+
}
42134216
Py_RETURN_NONE;
42144217
}
42154218

‎Modules/_io/winconsoleio.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,10 @@ _io__WindowsConsoleIO_readall_impl(winconsoleio *self)
962962

963963
bytes_size += _buflen(self);
964964
bytes = PyBytes_FromStringAndSize(NULL, bytes_size);
965+
if (bytes == NULL) {
966+
PyMem_Free(buf);
967+
return NULL;
968+
}
965969
rn = _copyfrombuf(self, PyBytes_AS_STRING(bytes), bytes_size);
966970

967971
if (len) {

‎Modules/_testinternalcapi.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2581,6 +2581,9 @@ get_crossinterp_data(PyObject *self, PyObject *args, PyObject *kwargs)
25812581
}
25822582
else {
25832583
mode = PyUnicode_AsUTF8(modeobj);
2584+
if (mode == NULL) {
2585+
return NULL;
2586+
}
25842587
if (strlen(mode) == 0) {
25852588
mode = "xidata";
25862589
}

‎Modules/_xxtestfuzz/fuzzer.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,10 @@ static int fuzz_sre_match(const char* data, size_t size) {
325325

326326
PyObject* pattern = compiled_patterns[idx];
327327
PyObject* match_callable = PyObject_GetAttrString(pattern, "match");
328+
if (match_callable == NULL) {
329+
Py_DECREF(to_match);
330+
return 0;
331+
}
328332

329333
PyObject* matches = PyObject_CallOneArg(match_callable, to_match);
330334

‎Modules/getpath.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,11 @@ getpath_warn(PyObject *Py_UNUSED(self), PyObject *args)
591591
if (!PyArg_ParseTuple(args, "U", &msgobj)) {
592592
return NULL;
593593
}
594-
fprintf(stderr, "%s\n", PyUnicode_AsUTF8(msgobj));
594+
const char *msg = PyUnicode_AsUTF8(msgobj);
595+
if (msg == NULL) {
596+
return NULL;
597+
}
598+
fprintf(stderr, "%s\n", msg);
595599
Py_RETURN_NONE;
596600
}
597601

‎Modules/posixmodule.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5696,8 +5696,9 @@ os__getvolumepathname_impl(PyObject *module, path_t *path)
56965696
goto exit;
56975697
}
56985698
result = PyUnicode_FromWideChar(mountpath, -1);
5699-
if (PyBytes_Check(path->object))
5699+
if (result != NULL && PyBytes_Check(path->object)) {
57005700
Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
5701+
}
57015702

57025703
exit:
57035704
PyMem_Free(mountpath);

‎Modules/readline.c‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,10 +1508,19 @@ flex_complete(const char *text, int start, int end)
15081508

15091509
done:
15101510
if (state) {
1511-
Py_XDECREF(state->begidx);
1512-
Py_XDECREF(state->endidx);
1513-
state->begidx = PyLong_FromLong((long) start);
1514-
state->endidx = PyLong_FromLong((long) end);
1511+
PyObject *begidx = PyLong_FromLong((long) start);
1512+
PyObject *endidx = PyLong_FromLong((long) end);
1513+
if (begidx == NULL || endidx == NULL) {
1514+
Py_XDECREF(begidx);
1515+
Py_XDECREF(endidx);
1516+
PyErr_FormatUnraisable("Exception ignored while preparing "
1517+
"readline completion");
1518+
rl_attempted_completion_over = 1;
1519+
PyGILState_Release(gilstate);
1520+
return NULL;
1521+
}
1522+
Py_XSETREF(state->begidx, begidx);
1523+
Py_XSETREF(state->endidx, endidx);
15151524
}
15161525
result = completion_matches((char *)text, *on_completion);
15171526
PyGILState_Release(gilstate);

‎Modules/socketmodule.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,6 +1766,9 @@ makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto)
17661766
PyObject *vmId = PyUnicode_FromWideChar(guidStr, -1);
17671767
res = RpcStringFreeW(&guidStr);
17681768
assert(res == RPC_S_OK);
1769+
if (vmId == NULL) {
1770+
return 0;
1771+
}
17691772

17701773
res = UuidToStringW(&a->ServiceId, &guidStr);
17711774
if (res != RPC_S_OK) {

‎Python/ceval.c‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,8 +1199,16 @@ dtrace_function_entry(_PyInterpreterFrame *frame)
11991199
int lineno;
12001200

12011201
PyCodeObject *code = _PyFrame_GetCode(frame);
1202+
PyObject *exc = PyErr_GetRaisedException();
12021203
filename = PyUnicode_AsUTF8(code->co_filename);
12031204
funcname = PyUnicode_AsUTF8(code->co_name);
1205+
PyErr_SetRaisedException(exc);
1206+
if (filename == NULL) {
1207+
filename = "<unknown>";
1208+
}
1209+
if (funcname == NULL) {
1210+
funcname = "<unknown>";
1211+
}
12041212
lineno = PyUnstable_InterpreterFrame_GetLine(frame);
12051213

12061214
PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
@@ -1214,8 +1222,16 @@ dtrace_function_return(_PyInterpreterFrame *frame)
12141222
int lineno;
12151223

12161224
PyCodeObject *code = _PyFrame_GetCode(frame);
1225+
PyObject *exc = PyErr_GetRaisedException();
12171226
filename = PyUnicode_AsUTF8(code->co_filename);
12181227
funcname = PyUnicode_AsUTF8(code->co_name);
1228+
PyErr_SetRaisedException(exc);
1229+
if (filename == NULL) {
1230+
filename = "<unknown>";
1231+
}
1232+
if (funcname == NULL) {
1233+
funcname = "<unknown>";
1234+
}
12191235
lineno = PyUnstable_InterpreterFrame_GetLine(frame);
12201236

12211237
PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);

‎Python/crossinterp.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,11 @@ check_missing___main___attr(PyObject *exc)
664664
}
665665
}
666666
const char *err = PyUnicode_AsUTF8(msgobj);
667+
if (err == NULL) {
668+
PyErr_Clear();
669+
Py_DECREF(msgobj);
670+
return 0;
671+
}
667672

668673
// Check if it's a missing __main__ attr.
669674
int cmp = strncmp(err, "module '__main__' has no attribute '", 36);

0 commit comments

Comments
 (0)