Skip to content

fix(hclsyntax): implement recursion depth limit inside parseBinaryOps to prevent stack overflow#812

Open
PARTH-AI-DS20 wants to merge 1 commit into
hashicorp:mainfrom
PARTH-AI-DS20:main
Open

fix(hclsyntax): implement recursion depth limit inside parseBinaryOps to prevent stack overflow#812
PARTH-AI-DS20 wants to merge 1 commit into
hashicorp:mainfrom
PARTH-AI-DS20:main

Conversation

@PARTH-AI-DS20

Copy link
Copy Markdown

Summary

This PR introduces a hard recursion depth threshold of 1000 loops inside parseBinaryOps to safely intercept deeply nested expressions (such as thousands of sequential parenthetical configurations) before they exhaust the standard Go goroutine stack runtime allocation.

Root Cause & Resolution

  • The Bug: When parsing configurations with excessive nested layers like (((((...))))), the recursive descent structure of parseBinaryOps continually allocates frames on the stack. This eventually results in a non-recoverable, fatal stack overflow panic (runtime: goroutine stack exceeds 1000000000-byte limit).
  • The Fix: Added a tracking field recursionDepth to the internal package parser struct. Entering parseBinaryOps increments this counter, checks against a strict max ceiling (1000), and returns a structured hcl.Diagnostic payload gracefully if crossed instead of throwing an unhandled panic. It clears down via a deferred frame decrement step.
  • Testing: Implemented a target validation regression block TestParseExpression_nestedParenthesesRecursionGuard inside parser_test.go to assert defensive validation engine boundaries.

Fixes #809

@PARTH-AI-DS20 PARTH-AI-DS20 requested a review from a team as a code owner June 19, 2026 03:15
@hashicorp-cla-app

hashicorp-cla-app Bot commented Jun 19, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

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.

hclsyntax parser crashes with fatal error: stack overflow on deeply nested parentheses

1 participant