Skip to content

Commit 980d24a

Browse files
committed
fix: reject DELETE statements missing a target table
DELETE, DELETE FROM, DELETE FROM WHERE ... and DELETE FROM USING ... all parsed successfully into a Delete with a null table, and toString() then printed the literal text "null" (DELETE FROM null WHERE 1 = 1). The grammar's bare-FROM alternative never required a table to follow. The production now validates that a target table was supplied in one of the three supported positions (before FROM, after FROM, or via the table list) through a requireDeleteSyntax() helper, matching the existing requireAccessSyntax()/requireReplicationSyntax() pattern.
1 parent f6ac257 commit 980d24a

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,6 +2013,10 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
20132013
if (!valid) { throw new ParseException(message); }
20142014
}
20152015

2016+
private static void requireDeleteSyntax(boolean valid, String message) throws ParseException {
2017+
if (!valid) { throw new ParseException(message); }
2018+
}
2019+
20162020
private static <E extends Enum<E>> E replicationEnum(Class<E> type, String value) throws ParseException {
20172021
try { return Enum.valueOf(type, MultiPartName.unquote(value).toUpperCase(Locale.ROOT)); }
20182022
catch (IllegalArgumentException exception) { throw new ParseException("Unknown " + type.getSimpleName() + ": " + value); }
@@ -7193,6 +7197,8 @@ Delete Delete():
71937197
if (joins != null && joins.size() > 0) {
71947198
delete.setJoins(joins);
71957199
}
7200+
requireDeleteSyntax(table != null || !tables.isEmpty(),
7201+
"DELETE requires a target table");
71967202
return delete.withWithItemsList(with)
71977203
.withTables(tables)
71987204
.withTable(table)

‎src/test/java/net/sf/jsqlparser/statement/delete/DeleteTest.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@ public void testDeleteDoesNotAllowLimitOffset() {
7474
() -> parserManager.parse(new StringReader(statement)));
7575
}
7676

77+
@Test
78+
public void testDeleteRequiresTargetTable() {
79+
// a missing target must not parse into a Delete whose toString()
80+
// prints a literal "null" for the absent table
81+
for (String statement : new String[] {
82+
"DELETE",
83+
"DELETE FROM",
84+
"DELETE FROM WHERE a = 1",
85+
"DELETE FROM USING t"}) {
86+
assertThrows(JSQLParserException.class,
87+
() -> parserManager.parse(new StringReader(statement)));
88+
}
89+
}
90+
7791
@Test
7892
public void testDeleteWithOrderBy() throws JSQLParserException {
7993
String stmt = "DELETE FROM tablename WHERE a = 1 AND b = 1 ORDER BY col";

0 commit comments

Comments
 (0)