From 0f7e45ab40cd343d98aa337b7ed885e2a4cf5fd0 Mon Sep 17 00:00:00 2001 From: Ellie Eadington Date: Mon, 30 Nov 2026 14:21:27 +0000 Subject: [PATCH] fix(DPC-64447): close a block comment at the first */ when nesting runs to end of input Block comment nesting (#1175) applies to every dialect, but most do not nest: Snowflake, PostgreSQL and others end a comment at the first */. SQL that commented out an expression already containing a commented-out version therefore failed at end of input from IN_BLOCK_COMMENT. consumeBlockCommentBody() now records where the first */ ended and, if nesting runs past the end of the input, rewinds there and closes the comment. Only input that currently throws takes the new path, so nesting is unaffected and no feature flag is needed. Co-Authored-By: Claude Opus 5 --- .../net/sf/jsqlparser/parser/JSqlParserCC.jjt | 20 ++++++++++++--- .../statement/select/NestedCommentTest.java | 25 +++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt b/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt index c064a8e1e5..54cc872be7 100644 --- a/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt +++ b/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt @@ -6178,19 +6178,27 @@ TOKEN_MGR_DECLS : { * Consumes the body of a block comment after the opening delimiter has been matched, * honouring nesting, up to and including the outermost closing delimiter. Then backs * up 2 characters so the MULTI_LINE_COMMENT rule matches the closing delimiter and - * terminates the MORE chain. + * terminates the MORE chain. If nesting runs past the end of the input and a closing + * delimiter was seen along the way, the comment is closed at the first one instead, + * which is how dialects that do not nest block comments read it. */ void consumeBlockCommentBody() { int nesting = 1; char prev = 0; + int read = 0; + int firstCloseEnd = -1; try { while (nesting > 0) { char c = input_stream.readChar(); + read++; if (prev == '/' && c == '*') { nesting++; prev = 0; } else if (prev == '*' && c == '/') { nesting--; + if (firstCloseEnd < 0) { + firstCloseEnd = read; + } prev = 0; } else { prev = c; @@ -6198,8 +6206,14 @@ TOKEN_MGR_DECLS : { } input_stream.backup(2); } catch (java.io.IOException e) { - // Unterminated comment: leave the stream at EOF and let the token - // manager raise the lexical error from IN_BLOCK_COMMENT. + if (firstCloseEnd < 0) { + // Nothing closed the comment at all: leave the stream at EOF and let + // the token manager raise the lexical error from IN_BLOCK_COMMENT. + return; + } + // Nesting ran off the end of the input, so the inner "/*" was comment text, + // not a delimiter: close at the first "*/" as a non-nesting dialect would. + input_stream.backup(read - firstCloseEnd + 2); } } } diff --git a/src/test/java/net/sf/jsqlparser/statement/select/NestedCommentTest.java b/src/test/java/net/sf/jsqlparser/statement/select/NestedCommentTest.java index a470257d2b..a82f21db87 100644 --- a/src/test/java/net/sf/jsqlparser/statement/select/NestedCommentTest.java +++ b/src/test/java/net/sf/jsqlparser/statement/select/NestedCommentTest.java @@ -9,6 +9,7 @@ */ package net.sf.jsqlparser.statement.select; +import net.sf.jsqlparser.JSQLParserException; import net.sf.jsqlparser.parser.CCJSqlParserUtil; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @@ -102,4 +103,28 @@ void testMultilineNestedComment() { void testOracleHintPreserved() { assertParses("SELECT /*+ FULL(t) */ * FROM t"); } + + @Test + void testUnbalancedNestingClosesAtFirstDelimiter() { + // Dialects such as Snowflake do not nest, so the inner "/*" is comment text and + // the comment ends at the only closing delimiter. + assertParses("SELECT /* a /* b */ 1"); + } + + @Test + void testUnbalancedNestingAcrossLines() { + assertParses( + "SELECT * FROM t WHERE 1 = 1\n" + + "/* commented out:\n" + + " AND x = /* default 42\n" + + " AND y = 0\n" + + "*/\n" + + "AND z = 2"); + } + + @Test + void testUnterminatedBlockCommentStillFails() { + assertThrows(JSQLParserException.class, + () -> CCJSqlParserUtil.parse("SELECT /* never closed")); + } }