Skip to content

Commit 7d39010

Browse files
edeadingtonclaude
andcommitted
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 <noreply@anthropic.com>
1 parent 2ba80d3 commit 7d39010

2 files changed

Lines changed: 44 additions & 3 deletions

File tree

‎src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6178,28 +6178,44 @@ TOKEN_MGR_DECLS : {
61786178
* Consumes the body of a block comment after the opening delimiter has been matched,
61796179
* honouring nesting, up to and including the outermost closing delimiter. Then backs
61806180
* up 2 characters so the MULTI_LINE_COMMENT rule matches the closing delimiter and
6181-
* terminates the MORE chain.
6181+
* terminates the MORE chain. If nesting runs past the end of the input and a closing
6182+
* delimiter was seen along the way, the comment is closed at the first one instead,
6183+
* which is how dialects that do not nest block comments read it.
61826184
*/
61836185
void consumeBlockCommentBody() {
61846186
int nesting = 1;
61856187
char prev = 0;
6188+
int read = 0;
6189+
int firstCloseEnd = -1;
61866190
try {
61876191
while (nesting > 0) {
61886192
char c = input_stream.readChar();
6193+
read++;
61896194
if (prev == '/' && c == '*') {
61906195
nesting++;
61916196
prev = 0;
61926197
} else if (prev == '*' && c == '/') {
61936198
nesting--;
6199+
if (firstCloseEnd < 0) {
6200+
firstCloseEnd = read;
6201+
}
61946202
prev = 0;
61956203
} else {
61966204
prev = c;
61976205
}
61986206
}
61996207
input_stream.backup(2);
62006208
} catch (java.io.IOException e) {
6201-
// Unterminated comment: leave the stream at EOF and let the token
6202-
// manager raise the lexical error from IN_BLOCK_COMMENT.
6209+
if (firstCloseEnd < 0) {
6210+
// Nothing closed the comment at all: leave the stream at EOF and let
6211+
// the token manager raise the lexical error from IN_BLOCK_COMMENT.
6212+
return;
6213+
}
6214+
// Dialects differ on whether a "/*" inside a comment opens a nested one.
6215+
// Nesting ran off the end of the input, so the inner "/*" was comment text
6216+
// in this dialect, not a delimiter: rewind to the first "*/" and close the
6217+
// comment there, which is what a parser without nesting would have done.
6218+
input_stream.backup(read - firstCloseEnd + 2);
62036219
}
62046220
}
62056221
}

‎src/test/java/net/sf/jsqlparser/statement/select/NestedCommentTest.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
package net.sf.jsqlparser.statement.select;
1111

12+
import net.sf.jsqlparser.JSQLParserException;
1213
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
1314
import org.junit.jupiter.api.Disabled;
1415
import org.junit.jupiter.api.Test;
@@ -102,4 +103,28 @@ void testMultilineNestedComment() {
102103
void testOracleHintPreserved() {
103104
assertParses("SELECT /*+ FULL(t) */ * FROM t");
104105
}
106+
107+
@Test
108+
void testUnbalancedNestingClosesAtFirstDelimiter() {
109+
// Dialects such as Snowflake do not nest, so the inner "/*" is comment text and
110+
// the comment ends at the only closing delimiter.
111+
assertParses("SELECT /* a /* b */ 1");
112+
}
113+
114+
@Test
115+
void testUnbalancedNestingAcrossLines() {
116+
assertParses(
117+
"SELECT * FROM t WHERE 1 = 1\n"
118+
+ "/* commented out:\n"
119+
+ " AND x = /* default 42\n"
120+
+ " AND y = 0\n"
121+
+ "*/\n"
122+
+ "AND z = 2");
123+
}
124+
125+
@Test
126+
void testUnterminatedBlockCommentStillFails() {
127+
assertThrows(JSQLParserException.class,
128+
() -> CCJSqlParserUtil.parse("SELECT /* never closed"));
129+
}
105130
}

0 commit comments

Comments
 (0)