Handle empty-string edge cases correctly in strpos().
authorTom Lane
Mon, 28 Oct 2019 16:21:13 +0000 (12:21 -0400)
committerTom Lane
Mon, 28 Oct 2019 16:21:13 +0000 (12:21 -0400)
Commit 9556aa01c rearranged the innards of text_position() in a way
that would make it not work for empty search strings.  Which is fine,
because all callers of that code special-case an empty pattern in
some way.  However, the primary use-case (text_position itself) got
special-cased incorrectly: historically it's returned 1 not 0 for
an empty search string.  Restore the historical behavior.

Per complaint from Austin Drenski (via Shay Rojansky).
Back-patch to v12 where it got broken.

Discussion: https://postgr.es/m/CADT4RqAz7oN4vkPir86Kg1_mQBmBxCp-L_=9vRpgSNPJf0KRkw@mail.gmail.com

src/backend/utils/adt/varlena.c
src/test/regress/expected/strings.out
src/test/regress/sql/strings.sql

index 722b2c722d931a252b4aac5896ca5a8ae09e0405..69165eb3116857eb645bace88bc7df952e46d9ce 100644 (file)
@@ -1118,7 +1118,12 @@ text_position(text *t1, text *t2, Oid collid)
    TextPositionState state;
    int         result;
 
-   if (VARSIZE_ANY_EXHDR(t1) < 1 || VARSIZE_ANY_EXHDR(t2) < 1)
+   /* Empty needle always matches at position 1 */
+   if (VARSIZE_ANY_EXHDR(t2) < 1)
+       return 1;
+
+   /* Otherwise, can't match if haystack is shorter than needle */
+   if (VARSIZE_ANY_EXHDR(t1) < VARSIZE_ANY_EXHDR(t2))
        return 0;
 
    text_position_setup(t1, t2, collid, &state);
@@ -1272,6 +1277,9 @@ text_position_setup(text *t1, text *t2, Oid collid, TextPositionState *state)
  * Advance to the next match, starting from the end of the previous match
  * (or the beginning of the string, on first call).  Returns true if a match
  * is found.
+ *
+ * Note that this refuses to match an empty-string needle.  Most callers
+ * will have handled that case specially and we'll never see it here.
  */
 static bool
 text_position_next(TextPositionState *state)
index 24839665765268e88d81ffe2a5888be8985bd7b7..6d96843e5b58a7a493a29fd69b0cbecb49577f54 100644 (file)
@@ -1420,6 +1420,24 @@ SELECT strpos('abcdef', 'xy') AS "pos_0";
      0
 (1 row)
 
+SELECT strpos('abcdef', '') AS "pos_1";
+ pos_1 
+-------
+     1
+(1 row)
+
+SELECT strpos('', 'xy') AS "pos_0";
+ pos_0 
+-------
+     0
+(1 row)
+
+SELECT strpos('', '') AS "pos_1";
+ pos_1 
+-------
+     1
+(1 row)
+
 --
 -- test replace
 --
index b5e75c344f239063f82ee36a3cc073e0d4bfd16c..0afb94964b1f129b3efe8d00b9fd72c4331c42b6 100644 (file)
@@ -492,6 +492,12 @@ SELECT strpos('abcdef', 'cd') AS "pos_3";
 
 SELECT strpos('abcdef', 'xy') AS "pos_0";
 
+SELECT strpos('abcdef', '') AS "pos_1";
+
+SELECT strpos('', 'xy') AS "pos_0";
+
+SELECT strpos('', '') AS "pos_1";
+
 --
 -- test replace
 --