Skip to content

parsers/c-based: add nesting depth limit to prevent stack overflow#4440

Open
pengfeixx wants to merge 1 commit into
universal-ctags:masterfrom
pengfeixx:fix/issue-4439-java-stack-overflow
Open

parsers/c-based: add nesting depth limit to prevent stack overflow#4440
pengfeixx wants to merge 1 commit into
universal-ctags:masterfrom
pengfeixx:fix/issue-4439-java-stack-overflow

Conversation

@pengfeixx

Copy link
Copy Markdown

Summary

  • Added a nesting depth limit (MAX_NEST_LEVEL = 1024) to the C-based parser to prevent stack overflow when processing deeply nested braces
  • When the limit is exceeded, the parser skips to the matching brace (skipToMatch("{}")) instead of recursing
  • This prevents SIGSEGV crashes on crafted or extremely nested source files (e.g. Java files with thousands of nested blocks)

Changes

File Change
parsers/c-based.c Added #define MAX_NEST_LEVEL 1024; added depth check in createTags() before recursive nest() call

Root Cause

The createTags()nest()createTags() recursive call chain has no depth bound. When processing a file with deeply nested braces (e.g. 54000 levels), this causes uncontrolled stack growth leading to SIGSEGV.

Test Plan

  • Build ctags and run against the PoC file (poc-java-stack-overflow-54000.java) attached to the issue — should no longer crash
  • Existing test suite should pass unchanged (1024 levels is well above any reasonable real-world nesting depth)
  • Tested with gcc -std=c99 -fsyntax-only — no compilation errors

Fixes #4439

Limit the maximum nesting depth in createTags() to
MAX_NEST_LEVEL (1024) to prevent stack overflow when
processing deeply nested braces (e.g. crafted Java source
files with thousands of nested blocks).

When the limit is exceeded, the parser skips to the matching
brace instead of recursing, preventing unbounded stack growth
that leads to SIGSEGV.

Fixes: universal-ctags#4439
@pengfeixx pengfeixx marked this pull request as ready for review June 4, 2026 05:34
@masatake masatake self-assigned this Jun 4, 2026
@masatake

Copy link
Copy Markdown
Member

Thank you. I'm thinking about fixing the original issue via #4387.

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.

Java parser stack overflow / SIGSEGV on deeply nested braces

2 participants