Skip to content

fix: add size_t overflow guard in ensure()#1057

Open
JIgar-maheshwari-dev wants to merge 1 commit into
DaveGamble:developfrom
JIgar-maheshwari-dev:fix/ensure-sizet-overflow
Open

fix: add size_t overflow guard in ensure()#1057
JIgar-maheshwari-dev wants to merge 1 commit into
DaveGamble:developfrom
JIgar-maheshwari-dev:fix/ensure-sizet-overflow

Conversation

@JIgar-maheshwari-dev

@JIgar-maheshwari-dev JIgar-maheshwari-dev commented Jul 22, 2026

Copy link
Copy Markdown

Fixes #1007

Overview

Adds a boundary check to ensure() in cJSON.c to prevent potential integer wrap-around on 32-bit architectures.

Changes

  • Added if (p->offset > SIZE_MAX - 1 - needed) return NULL; prior to calculating needed += p->offset + 1.

Motivation

On 32-bit systems with large existing buffer offsets (p->offset), adding p->offset + 1 to needed could exceed SIZE_MAX and wrap around, causing realloc() to allocate an undersized buffer.

Verification

  • Built and ran the full unit test suite; all tests pass cleanly.

Add a boundary check in ensure() before adding p->offset + 1 to needed.
This prevents potential size_t wrap-around on 32-bit systems when handling large buffer offsets.

Signed-off-by: JIgar-maheshwari-dev <maheshwarijigar656@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant