From d2c89ba53433f8f0940e8bf086436895e46436a6 Mon Sep 17 00:00:00 2001 From: Aliaksey Kandratsenka Date: Mon, 24 Jul 2023 18:57:52 -0400 Subject: [PATCH] don't return raw span when sampling and stacktrace oomed This is nearly impossible in practice, but still. Somehow we missed this logic that DoSampledAllocation always returns actual object, but in that condition where stacktrace_allocator failed to get us StackTrace object we ended up returning span instead of it's object. --- src/tcmalloc.cc | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/tcmalloc.cc b/src/tcmalloc.cc index 47d426f..331323e 100644 --- a/src/tcmalloc.cc +++ b/src/tcmalloc.cc @@ -1177,14 +1177,12 @@ static void* DoSampledAllocation(size_t size) { // Allocate stack trace StackTrace *stack = Static::stacktrace_allocator()->New(); - if (PREDICT_FALSE(stack == NULL)) { - // Sampling failed because of lack of memory - return span; + if (PREDICT_TRUE(stack != nullptr)) { + *stack = tmp; + span->sample = 1; + span->objects = stack; + tcmalloc::DLL_Prepend(Static::sampled_objects(), span); } - *stack = tmp; - span->sample = 1; - span->objects = stack; - tcmalloc::DLL_Prepend(Static::sampled_objects(), span); return SpanToMallocResult(span); #else