diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2019-10-28 12:21:13 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2019-10-28 12:21:13 -0400 |
commit | bd1ef5799b04168d8a869197dd9b85935d5d5da9 (patch) | |
tree | 857390a02ae564c83a9f951b9a70f656900803fe /src/backend/utils/adt/varlena.c | |
parent | 61ecea45e50bcd3b87d4e905719e63e41d6321ce (diff) | |
download | postgresql-bd1ef5799b04168d8a869197dd9b85935d5d5da9.tar.gz postgresql-bd1ef5799b04168d8a869197dd9b85935d5d5da9.zip |
Handle empty-string edge cases correctly in strpos().
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
Diffstat (limited to 'src/backend/utils/adt/varlena.c')
-rw-r--r-- | src/backend/utils/adt/varlena.c | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 722b2c722d9..69165eb3116 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -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) |