Skip to content

Commit c821f88

Browse files
authored
Merge pull request #124 from taegyunkim/taegyunkim/prof-12244-ub
Avoids UB on `python_stack` by using unsigned types with underflow check
2 parents f0ed225 + 6eed774 commit c821f88

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

echion/threads.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,19 +244,20 @@ void ThreadInfo::unwind_tasks()
244244
{
245245
auto& task = current_task.get();
246246

247-
int stack_size = task.unwind(stack);
247+
size_t stack_size = task.unwind(stack);
248248

249249
if (on_cpu)
250250
{
251251
// Undo the stack unwinding
252252
// TODO[perf]: not super-efficient :(
253-
for (int i = 0; i < stack_size; i++)
253+
for (size_t i = 0; i < stack_size; i++)
254254
stack.pop_back();
255255

256256
// Instead we get part of the thread stack
257257
FrameStack temp_stack;
258-
ssize_t nframes = python_stack.size() - stack_size + 1;
259-
for (ssize_t i = 0; i < nframes; i++)
258+
size_t nframes =
259+
(python_stack.size() > stack_size) ? python_stack.size() - stack_size : 0;
260+
for (size_t i = 0; i < nframes; i++)
260261
{
261262
auto python_frame = python_stack.front();
262263
temp_stack.push_front(python_frame);

0 commit comments

Comments
 (0)