diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2011-05-31 17:54:01 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2011-05-31 17:54:01 -0400 |
commit | 8057b7554bde98d887f02f5c0c7aeca01c8b52c9 (patch) | |
tree | 17181ab434a846067ecca843e33cdf23374cea14 /src/backend/access/gist/gistutil.c | |
parent | 0699d053ba8eede356e59e6e220716891cbee7fe (diff) | |
download | postgresql-8057b7554bde98d887f02f5c0c7aeca01c8b52c9.tar.gz postgresql-8057b7554bde98d887f02f5c0c7aeca01c8b52c9.zip |
Protect GIST logic that assumes penalty values can't be negative.
Apparently sane-looking penalty code might return small negative values,
for example because of roundoff error. This will confuse places like
gistchoose(). Prevent problems by clamping negative penalty values to
zero. (Just to be really sure, I also made it force NaNs to zero.)
Back-patch to all supported branches.
Alexander Korotkov
Diffstat (limited to 'src/backend/access/gist/gistutil.c')
-rw-r--r-- | src/backend/access/gist/gistutil.c | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/src/backend/access/gist/gistutil.c b/src/backend/access/gist/gistutil.c index a2e8fe0cfb9..2e70b5e5c7d 100644 --- a/src/backend/access/gist/gistutil.c +++ b/src/backend/access/gist/gistutil.c @@ -13,6 +13,8 @@ */ #include "postgres.h" +#include <math.h> + #include "access/gist_private.h" #include "access/reloptions.h" #include "storage/freespace.h" @@ -532,16 +534,22 @@ gistpenalty(GISTSTATE *giststate, int attno, { float penalty = 0.0; - if (giststate->penaltyFn[attno].fn_strict == FALSE || (isNullOrig == FALSE && isNullAdd == FALSE)) + if (giststate->penaltyFn[attno].fn_strict == FALSE || + (isNullOrig == FALSE && isNullAdd == FALSE)) + { FunctionCall3(&giststate->penaltyFn[attno], PointerGetDatum(orig), PointerGetDatum(add), PointerGetDatum(&penalty)); + /* disallow negative or NaN penalty */ + if (isnan(penalty) || penalty < 0.0) + penalty = 0.0; + } else if (isNullOrig && isNullAdd) penalty = 0.0; else - penalty = 1e10; /* try to prevent to mix null and non-null - * value */ + penalty = 1e10; /* try to prevent mixing null and non-null + * values */ return penalty; } |