Skip to content
Myr

Fix signed-int overflow in `stack_reallocate`

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This fixes CVE-2026-41257.

authored by itchyny · committed by itchyny · parent d1a1256 · Browse files · 1 file+10 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+10 added−4 removed

  • other:+10 added−4 removed

1 file+10 added−4 removed

modified:src/exec_stack.h+10 added-4 removed
1#ifndef EXEC_STACK_H2#define EXEC_STACK_H3#include <stddef.h>4#include <stdint.h>5#include <stdio.h>6#include <stdlib.h>7#include <string.h>8#include <limits.h>9#include "jv_alloc.h"1011/*12 * The stack is a directed forest of variably sized blocks. Each block has a
69 unmodified lines
82 return &((stack_ptr*)stack_block(s, p))[-1];83}8485static void stack_reallocate(struct stack* s, size_t sz) {84 int old_mem_length = -(s->bound) + ALIGNMENT;85 char* old_mem_start = (s->mem_end != NULL) ? (s->mem_end - old_mem_length) : NULL;86 size_t old_mem_length = (size_t)(-(s->bound)) + ALIGNMENT;87 char* old_mem_start = s->mem_end != NULL ? s->mem_end - old_mem_length : NULL;8887 int new_mem_length = align_round_up((old_mem_length + sz + 256) * 2);89 size_t new_mem_length = align_round_up((old_mem_length + sz + 256) * 2);90 if (new_mem_length > INT_MAX) {91 fprintf(stderr, "jq: error: cannot allocate memory\n");92 abort();93 }94 char* new_mem_start = jv_mem_realloc(old_mem_start, new_mem_length);95 memmove(new_mem_start + (new_mem_length - old_mem_length),96 new_mem_start, old_mem_length);97 s->mem_end = new_mem_start + new_mem_length;92 s->bound = -(new_mem_length - ALIGNMENT);98 s->bound = -(int)(new_mem_length - ALIGNMENT);99}100101static stack_ptr stack_push_block(struct stack* s, stack_ptr p, size_t sz) {102 int alloc_sz = align_round_up(sz) + ALIGNMENT;
18 unmodified lines