Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cW6px-0004nL-DW for pgsql-bugs@arkaria.postgresql.org; Tue, 24 Jan 2017 19:33:01 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cW6pw-0001ON-VH for pgsql-bugs@arkaria.postgresql.org; Tue, 24 Jan 2017 19:33:01 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cW6pH-0000fN-QJ for pgsql-bugs@postgresql.org; Tue, 24 Jan 2017 19:32:19 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cW6pE-0006am-V6 for pgsql-bugs@postgresql.org; Tue, 24 Jan 2017 19:32:18 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id v0OJWEkr010288; Tue, 24 Jan 2017 14:32:14 -0500 From: Tom Lane To: "David G. Johnston" cc: =?UTF-8?Q?Vojt=C4=9Bch_Rylko?= , "pgsql-bugs@postgresql.org" Subject: Re: BUG #14512: Backslashes in LIKE In-reply-to: References: <20170124172505.1431.56735@wrigleys.postgresql.org> <23478.1485280945@sss.pgh.pa.us> Comments: In-reply-to "David G. Johnston" message dated "Tue, 24 Jan 2017 11:15:08 -0700" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <10257.1485286282.0@sss.pgh.pa.us> Date: Tue, 24 Jan 2017 14:32:14 -0500 Message-ID: <10287.1485286334@sss.pgh.pa.us> X-Pg-Spam-Score: -5.1 (-----) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <10257.1485286282.1@sss.pgh.pa.us> "David G. Johnston" writes: > Then consider a feature request that a malformed pattern be detected and > fail independent of the data being checked. Such non-deterministic failure > is at least a POLA violation and makes what should be a basically > compile-time error into a run-time one. Meh. We could do something like the attached, but I think it would be a net performance drag in practically all cases, and I doubt it is worth it. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="check-all-of-LIKE-pattern.patch"; charset="us-ascii" Content-ID: <10257.1485286282.2@sss.pgh.pa.us> Content-Description: check-all-of-LIKE-pattern.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/utils/adt/like.c b/src/backend/utils/adt/like.c index 91fe109..e94e64a 100644 *** a/src/backend/utils/adt/like.c --- b/src/backend/utils/adt/like.c *************** SB_lower_char(unsigned char c, pg_locale *** 146,155 **** --- 146,181 ---- =20=20 #include "like_match.c" =20=20 + /* + * Check that pattern is legal (which reduces to checking that there is no + * backslash at the end). We do this separately so that we'll throw an e= rror + * for any invalid pattern, even if the matching logic doesn't ever exami= ne + * all of it. In consequence, the matching logic need not defend itself + * against invalid patterns. + */ + static inline void + check_like_pattern(char *p, int plen) + { + /* This can be stupid even in multibyte encodings. */ + while (plen-- > 0) + { + if (*p++ =3D=3D '\\') + { + if (unlikely(plen =3D=3D 0)) + ereport(ERROR, + (errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE), + errmsg("LIKE pattern must not end with escape character"))); + /* otherwise ignore next character, even if it's backslash */ + p++, plen--; + } + } + } +=20 /* Generic for all cases not requiring inline case-folding */ static inline int GenericMatchText(char *s, int slen, char *p, int plen) { + check_like_pattern(p, plen); if (pg_database_encoding_max_length() =3D=3D 1) return SB_MatchText(s, slen, p, plen, 0, true); else if (GetDatabaseEncoding() =3D=3D PG_UTF8) *************** Generic_Text_IC_like(text *str, text *pa *** 179,184 **** --- 205,211 ---- PointerGetDatum(pat))); p =3D VARDATA(pat); plen =3D (VARSIZE(pat) - VARHDRSZ); + check_like_pattern(p, plen); str =3D DatumGetTextP(DirectFunctionCall1Coll(lower, collation, PointerGetDatum(str))); s =3D VARDATA(str); *************** Generic_Text_IC_like(text *str, text *pa *** 217,222 **** --- 244,250 ---- =20=20 p =3D VARDATA_ANY(pat); plen =3D VARSIZE_ANY_EXHDR(pat); + check_like_pattern(p, plen); s =3D VARDATA_ANY(str); slen =3D VARSIZE_ANY_EXHDR(str); return SB_IMatchText(s, slen, p, plen, locale, locale_is_c); *************** bytealike(PG_FUNCTION_ARGS) *** 326,331 **** --- 354,360 ---- slen =3D VARSIZE_ANY_EXHDR(str); p =3D VARDATA_ANY(pat); plen =3D VARSIZE_ANY_EXHDR(pat); + check_like_pattern(p, plen); =20=20 result =3D (SB_MatchText(s, slen, p, plen, 0, true) =3D=3D LIKE_TRUE); =20=20 *************** byteanlike(PG_FUNCTION_ARGS) *** 347,352 **** --- 376,382 ---- slen =3D VARSIZE_ANY_EXHDR(str); p =3D VARDATA_ANY(pat); plen =3D VARSIZE_ANY_EXHDR(pat); + check_like_pattern(p, plen); =20=20 result =3D (SB_MatchText(s, slen, p, plen, 0, true) !=3D LIKE_TRUE); =20=20 diff --git a/src/backend/utils/adt/like_match.c b/src/backend/utils/adt/lik= e_match.c index 1c37229..a69b41b 100644 *** a/src/backend/utils/adt/like_match.c --- b/src/backend/utils/adt/like_match.c *************** MatchText(char *t, int tlen, char *p, in *** 99,110 **** if (*p =3D=3D '\\') { /* Next pattern byte must match literally, whatever it is */ NextByte(p, plen); - /* ... and there had better be one, per SQL standard */ - if (plen <=3D 0) - ereport(ERROR, - (errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE), - errmsg("LIKE pattern must not end with escape character"))); if (GETCHAR(*p) !=3D GETCHAR(*t)) return LIKE_FALSE; } --- 99,106 ---- if (*p =3D=3D '\\') { /* Next pattern byte must match literally, whatever it is */ + /* (and check_like_pattern() checked that there is one) */ NextByte(p, plen); if (GETCHAR(*p) !=3D GETCHAR(*t)) return LIKE_FALSE; } *************** MatchText(char *t, int tlen, char *p, in *** 160,172 **** * end of the text. */ if (*p =3D=3D '\\') ! { ! if (plen < 2) ! ereport(ERROR, ! (errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE), ! errmsg("LIKE pattern must not end with escape character"))); ! firstpat =3D GETCHAR(p[1]); ! } else firstpat =3D GETCHAR(*p); =20=20 --- 156,162 ---- * end of the text. */ if (*p =3D=3D '\\') ! firstpat =3D GETCHAR(p[1]); /* we know this is safe */ else firstpat =3D GETCHAR(*p); =20=20 ------- =_aaaaaaaaaa0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs ------- =_aaaaaaaaaa0--