Skip to content

Commit 4bde55f

Browse files
committed
Issue #19741: fix tracemalloc_log_alloc(), handle _Py_HASHTABLE_SET() failure
1 parent f7ba44a commit 4bde55f

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

Modules/_tracemalloc.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -447,19 +447,28 @@ tracemalloc_log_alloc(void *ptr, size_t size)
447447
#endif
448448

449449
traceback = traceback_new();
450-
if (traceback == NULL)
450+
if (traceback == NULL) {
451+
/* Memory allocation failed. The error cannot be reported to the
452+
caller, because realloc() may already have shrink the memory block
453+
and so removed bytes. */
451454
return;
455+
}
452456

453457
trace.size = size;
454458
trace.traceback = traceback;
455459

456460
TABLES_LOCK();
457-
assert(tracemalloc_traced_memory <= PY_SIZE_MAX - size);
458-
tracemalloc_traced_memory += size;
459-
if (tracemalloc_traced_memory > tracemalloc_max_traced_memory)
460-
tracemalloc_max_traced_memory = tracemalloc_traced_memory;
461-
462-
_Py_HASHTABLE_SET(tracemalloc_traces, ptr, trace);
461+
if (_Py_HASHTABLE_SET(tracemalloc_traces, ptr, trace) == 0) {
462+
assert(tracemalloc_traced_memory <= PY_SIZE_MAX - size);
463+
tracemalloc_traced_memory += size;
464+
if (tracemalloc_traced_memory > tracemalloc_max_traced_memory)
465+
tracemalloc_max_traced_memory = tracemalloc_traced_memory;
466+
}
467+
else {
468+
/* Hashtabled failed to add a new entry because of a memory allocation
469+
failure. Same than above, the error cannot be reported to the
470+
caller. */
471+
}
463472
TABLES_UNLOCK();
464473
}
465474

0 commit comments

Comments
 (0)