]> git.ipfire.org Git - thirdparty/postgresql.git/commit
Avoid divide-by-zero in regex_selectivity() with long fixed prefix.
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 12 Feb 2021 21:26:47 +0000 (16:26 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 12 Feb 2021 21:26:47 +0000 (16:26 -0500)
commita28df6fa3041523f9a96cf0d3c3929f8bdc7c797
tree85e86c7919e2c535bdb19da7bf84041a4468adae
parentdb5b64b978290e2c2bb0a18edbf2447c1405ccad
Avoid divide-by-zero in regex_selectivity() with long fixed prefix.

Given a regex pattern with a very long fixed prefix (approaching 500
characters), the result of pow(FIXED_CHAR_SEL, fixed_prefix_len) can
underflow to zero.  Typically the preceding selectivity calculation
would have underflowed as well, so that we compute 0/0 and get NaN.
In released branches this leads to an assertion failure later on.
That doesn't happen in HEAD, for reasons I've not explored yet,
but it's surely still a bug.

To fix, just skip the division when the pow() result is zero, so
that we'll (most likely) return a zero selectivity estimate.  In
the edge cases where "sel" didn't yet underflow, perhaps this
isn't desirable, but I'm not sure that the case is worth spending
a lot of effort on.  The results of regex_selectivity_sub() are
barely worth the electrons they're written on anyway :-(

Per report from Alexander Lakhin.  Back-patch to all supported versions.

Discussion: https://postgr.es/m/6de0a0c3-ada9-cd0c-3e4e-2fa9964b41e3@gmail.com
src/backend/utils/adt/selfuncs.c