Skip to content

Commit 08e222f

Browse files
edeadingtonclaude
andauthored
fix(DPC-64447): close a block comment at the first */ when nesting runs to end of input (#2965)
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 da7cf75 commit 08e222f

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6187,28 +6187,42 @@ TOKEN_MGR_DECLS : {
61876187
* Consumes the body of a block comment after the opening delimiter has been matched,
61886188
* honouring nesting, up to and including the outermost closing delimiter. Then backs
61896189
* up 2 characters so the MULTI_LINE_COMMENT rule matches the closing delimiter and
6190-
* terminates the MORE chain.
6190+
* terminates the MORE chain. If nesting runs past the end of the input and a closing
6191+
* delimiter was seen along the way, the comment is closed at the first one instead,
6192+
* which is how dialects that do not nest block comments read it.
61916193
*/
61926194
void consumeBlockCommentBody() {
61936195
int nesting = 1;
61946196
char prev = 0;
6197+
int read = 0;
6198+
int firstCloseEnd = -1;
61956199
try {
61966200
while (nesting > 0) {
61976201
char c = input_stream.readChar();
6202+
read++;
61986203
if (prev == '/' && c == '*') {
61996204
nesting++;
62006205
prev = 0;
62016206
} else if (prev == '*' && c == '/') {
62026207
nesting--;
6208+
if (firstCloseEnd < 0) {
6209+
firstCloseEnd = read;
6210+
}
62036211
prev = 0;
62046212
} else {
62056213
prev = c;
62066214
}
62076215
}
62086216
input_stream.backup(2);
62096217
} catch (java.io.IOException e) {
6210-
// Unterminated comment: leave the stream at EOF and let the token
6211-
// manager raise the lexical error from IN_BLOCK_COMMENT.
6218+
if (firstCloseEnd < 0) {
6219+
// Nothing closed the comment at all: leave the stream at EOF and let
6220+
// the token manager raise the lexical error from IN_BLOCK_COMMENT.
6221+
return;
6222+
}
6223+
// Nesting ran off the end of the input, so the inner "/*" was comment text,
6224+
// not a delimiter: close at the first "*/" as a non-nesting dialect would.
6225+
input_stream.backup(read - firstCloseEnd + 2);
62126226
}
62136227
}
62146228
}

‎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)