Skip to content

Track buffer sizes for static global pointers - #8828

Open
JeewoongKim wants to merge 2 commits into
cppcheck-opensource:mainfrom
JeewoongKim:fix-14934-global-buffer-size
Open

JeewoongKim wants to merge 2 commits into
cppcheck-opensource:mainfrom
JeewoongKim:fix-14934-global-buffer-size

Conversation

@JeewoongKim

@JeewoongKim JeewoongKim commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

This PR started from ticket 14934.

The initial implementation propagated the buffer size of a global pointer into main().
Based on the review feedback, I replaced that approach with tracking for static
global pointers that are not modified or escaped within the translation unit.
Allocation functions such as malloc are also handled in addition to new.

The original 14934 example uses an external-linkage global, which requires additional
reasoning about modification from other translation units. I therefore limited this change
to internal-linkage globals as a conservative first step rather than claiming to fix directly.

@JeewoongKim
JeewoongKim force-pushed the fix-14934-global-buffer-size branch 2 times, most recently from bc87761 to d7d165a Compare September 6, 2026 10:48
@chrchr-github

Copy link
Copy Markdown
Collaborator

Thanks for working on this. I'm not sure this approach is feasible though.
Global variables might be reassigned at any point, which would invalidate any previous buffer size. That's why buffer sizes are currently tracked only at the function level.
Also, this PR only handles new and not e.g. malloc().

@danmar

danmar commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

Thanks for working on this.

Yes I agree, thanks for looking at Cppcheck.

Global variables might be reassigned at any point, which would invalidate any previous buffer size.

I assume that is what the last test case with the reset(); is about.

I fear that the upside of this PR seems pretty small because it only targets main(). it could find problems in some small and simple synthetic test case more or less. more generic heuristics would be appreciated that could find simple bugs in all code.

@JeewoongKim

Copy link
Copy Markdown
Contributor Author

Thanks for all the feedback!

I initially focused too narrowly on the specific reproducer in the ticket, so the solution ended up being too limited.

I agree that a more general heuristic would be more useful. I'm looking into an approach that can handle simple cases across functions.

Replace the main()-specific propagation with tracking for static global
pointers that are not modified or escaped.

Also handle allocation functions such as malloc in addition to new.
@JeewoongKim JeewoongKim changed the title Fix 14934: detect out-of-bounds access to global new arrays Track buffer sizes for static global pointers Sep 23, 2026
@JeewoongKim

Copy link
Copy Markdown
Contributor Author

I reworked the implementation to use a more general heuristic.

I started with internal-linkage globals rather than external-linkage ones, since their modifications
can be checked within a single translation unit.

This removes main() special case, and the buffer size can now be used across functions.

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.

3 participants