Skip to content

Commit 58c5eda

Browse files
authored
[3.14] gh-146308: Fix error handling issues in _remote_debugging module (GH-146309) (#146398)
(cherry picked from commit ae6adc9)
1 parent ac2fff4 commit 58c5eda

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fixed several error handling issues in the :mod:`!_remote_debugging` module,
2+
including safer validation of remote ``int`` objects, clearer asyncio task
3+
chain failures, and cache cleanup fixes that avoid leaking or double-freeing
4+
metadata on allocation failure. Patch by Pablo Galindo.

Modules/_remote_debugging_module.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
#define INTERP_STATE_BUFFER_SIZE MAX(INTERP_STATE_MIN_SIZE, 256)
8080
#define MAX_STACK_CHUNK_SIZE (16 * 1024 * 1024) /* 16 MB max for stack chunks */
8181
#define MAX_SET_TABLE_SIZE (1 << 20) /* 1 million entries max for set iteration */
82+
#define MAX_LONG_DIGITS 64 /* Allows values up to ~2^1920 */
8283

8384

8485

@@ -753,6 +754,15 @@ read_py_long(
753754
return 0;
754755
}
755756

757+
if (size < 0 || size > MAX_LONG_DIGITS) {
758+
PyErr_Format(PyExc_RuntimeError,
759+
"Invalid PyLong digit count: %zd (expected 0-%d)",
760+
size, MAX_LONG_DIGITS);
761+
set_exception_cause(unwinder, PyExc_RuntimeError,
762+
"Invalid PyLong size (corrupted remote memory)");
763+
return -1;
764+
}
765+
756766
// If the long object has inline digits, use them directly
757767
digit *digits;
758768
if (size <= _PY_NSMALLNEGINTS + _PY_NSMALLPOSINTS) {
@@ -1364,6 +1374,9 @@ process_running_task_chain(
13641374
PyObject *coro_chain = PyStructSequence_GET_ITEM(task_info, 2);
13651375
assert(coro_chain != NULL);
13661376
if (PyList_GET_SIZE(coro_chain) != 1) {
1377+
PyErr_Format(PyExc_RuntimeError,
1378+
"Expected single-item coro chain, got %zd items",
1379+
PyList_GET_SIZE(coro_chain));
13671380
set_exception_cause(unwinder, PyExc_RuntimeError, "Coro chain is not a single item");
13681381
return -1;
13691382
}
@@ -1625,6 +1638,7 @@ cache_tlbc_array(RemoteUnwinderObject *unwinder, uintptr_t code_addr, uintptr_t
16251638
void *key = (void *)code_addr;
16261639
if (_Py_hashtable_set(unwinder->tlbc_cache, key, entry) < 0) {
16271640
tlbc_cache_entry_destroy(entry);
1641+
PyErr_NoMemory();
16281642
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to store TLBC entry in cache");
16291643
return 0; // Cache error
16301644
}
@@ -1803,7 +1817,11 @@ parse_code_object(RemoteUnwinderObject *unwinder,
18031817
meta->addr_code_adaptive = real_address + (uintptr_t)unwinder->debug_offsets.code_object.co_code_adaptive;
18041818

18051819
if (unwinder && unwinder->code_object_cache && _Py_hashtable_set(unwinder->code_object_cache, key, meta) < 0) {
1820+
func = NULL;
1821+
file = NULL;
1822+
linetable = NULL;
18061823
cached_code_metadata_destroy(meta);
1824+
PyErr_NoMemory();
18071825
set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to cache code metadata");
18081826
goto error;
18091827
}

0 commit comments

Comments
 (0)