Skip to content

Fix #44268: Prevent NoSuchElementException in TOML parser endMode()#44306

Open
DuraCode2003 wants to merge 1 commit intoballerina-platform:masterfrom
DuraCode2003:master
Open

Fix #44268: Prevent NoSuchElementException in TOML parser endMode()#44306
DuraCode2003 wants to merge 1 commit intoballerina-platform:masterfrom
DuraCode2003:master

Conversation

@DuraCode2003
Copy link

Purpose

Fixes #44268 - The TOML parser crashes with NoSuchElementException when parsing malformed TOML files containing invalid escape sequences. The crash occurs in AbstractLexer.endMode() when the mode stack becomes empty during error recovery.

Fixes #44268

Approach

Added defensive programming checks to AbstractLexer.endMode():

  • Check modeStack.isEmpty() before calling pop()
  • Check modeStack.isEmpty() before calling peek() after popping
  • Gracefully handle empty stack scenarios without throwing exceptions
  • Maintain backward compatibility with existing functionality

The fix prevents stack underflow while allowing the parser to continue error recovery gracefully.

Samples

Before the fix, malformed TOML like key = "\z invalid" would crash with:
java.util.NoSuchElementException
at java.base/java.util.ArrayDeque.pop(ArrayDeque.java:547)
at io.ballerina.toml.internal.parser.AbstractLexer.endMode(AbstractLexer.java:102)

After the fix, the same input is handled gracefully as a parsing error without crashing.

Remarks

  • This is a defensive programming fix that prevents crashes during error recovery
  • No breaking changes to existing API or behavior
  • The fix addresses the root cause of stack underflow in lexer mode management

Check List

  • Read the Contributing Guide
  • Updated Change Log
  • Checked Tooling Support
  • Added necessary tests
    • Unit Tests (created test to verify fix)
    • Spec Conformance Tests
    • Integration Tests
    • Ballerina By Example Tests
  • Increased Test Coverage
  • Added necessary documentation
    • API documentation
    • Module documentation in Module.md files
    • Ballerina By Examples

- Add isEmpty() checks before modeStack.pop() and modeStack.peek()
- Prevents stack underflow during TOML parsing error recovery
- Handles malformed TOML files with invalid escape sequences gracefully

Fixes ballerina-platform#44268
@CLAassistant
Copy link

CLAassistant commented Sep 23, 2025

CLA assistant check
All committers have signed the CLA.

@codecov
Copy link

codecov bot commented Sep 24, 2025

Codecov Report

❌ Patch coverage is 50.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.08%. Comparing base (ee42802) to head (749d321).
⚠️ Report is 38 commits behind head on master.

Files with missing lines Patch % Lines
.../ballerina/toml/internal/parser/AbstractLexer.java 50.00% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master   #44306      +/-   ##
============================================
- Coverage     75.08%   75.08%   -0.01%     
+ Complexity    58490    58489       -1     
============================================
  Files          3595     3595              
  Lines        225855   225857       +2     
  Branches      29342    29343       +1     
============================================
- Hits         169588   169584       -4     
- Misses        46866    46871       +5     
- Partials       9401     9402       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

[Bug]: Ballerina TOML Parser Crash on Multi-Line String with Special Characters

2 participants