From 04c5afcded0308208a57bce0965f2b302ba73d84 Mon Sep 17 00:00:00 2001 From: Shihao Date: Fri, 25 Sep 2026 00:55:47 -0400 Subject: [PATCH v1 2/2] Add tests for LIKE patterns that end with an escape There were no tests for this error outside the nondeterministic collation case. Cover inputs where matching never reaches the end of the pattern, plus a pattern ending in an escaped backslash, which must still work. Discussion: https://postgr.es/m/19699-dbaa58bbf8db1859@postgresql.org --- src/test/regress/expected/strings.out | 27 +++++++++++++++++++++++++++ src/test/regress/sql/strings.sql | 12 ++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/test/regress/expected/strings.out b/src/test/regress/expected/strings.out index fa29abfd829..ef997409bed 100644 --- a/src/test/regress/expected/strings.out +++ b/src/test/regress/expected/strings.out @@ -1832,6 +1832,33 @@ SELECT 'be_r' NOT LIKE '__e__r' ESCAPE '_' AS "true"; t (1 row) +-- escape at end of pattern is an error, even if matching doesn't reach it +SELECT '' LIKE '\' AS "error"; +ERROR: LIKE pattern must not end with escape character +SELECT 'x' LIKE 'x\' AS "error"; +ERROR: LIKE pattern must not end with escape character +SELECT 'x' NOT LIKE 'y\' AS "error"; +ERROR: LIKE pattern must not end with escape character +SELECT '' LIKE '#' ESCAPE '#' AS "error"; +ERROR: LIKE pattern must not end with escape character +SELECT '' ILIKE '\' AS "error"; +ERROR: LIKE pattern must not end with escape character +SELECT ''::bytea LIKE '\\'::bytea AS "error"; +ERROR: LIKE pattern must not end with escape character +SELECT 'x\' LIKE 'x\\\' AS "error"; +ERROR: LIKE pattern must not end with escape character +SELECT 'x\' LIKE 'x\\' AS "true"; + true +------ + t +(1 row) + +SELECT 'x\' NOT LIKE 'x\\' AS "false"; + false +------- + f +(1 row) + -- -- test ILIKE (case-insensitive LIKE) -- Be sure to form every test as an ILIKE/NOT ILIKE pair. diff --git a/src/test/regress/sql/strings.sql b/src/test/regress/sql/strings.sql index 7d9c7275a02..286c7bccab7 100644 --- a/src/test/regress/sql/strings.sql +++ b/src/test/regress/sql/strings.sql @@ -500,6 +500,18 @@ SELECT 'be_r' NOT LIKE 'b_e__r' ESCAPE '_' AS "false"; SELECT 'be_r' LIKE '__e__r' ESCAPE '_' AS "false"; SELECT 'be_r' NOT LIKE '__e__r' ESCAPE '_' AS "true"; +-- escape at end of pattern is an error, even if matching doesn't reach it +SELECT '' LIKE '\' AS "error"; +SELECT 'x' LIKE 'x\' AS "error"; +SELECT 'x' NOT LIKE 'y\' AS "error"; +SELECT '' LIKE '#' ESCAPE '#' AS "error"; +SELECT '' ILIKE '\' AS "error"; +SELECT ''::bytea LIKE '\\'::bytea AS "error"; +SELECT 'x\' LIKE 'x\\\' AS "error"; + +SELECT 'x\' LIKE 'x\\' AS "true"; +SELECT 'x\' NOT LIKE 'x\\' AS "false"; + -- -- test ILIKE (case-insensitive LIKE) -- 2.37.1 (Apple Git-137.1)