summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2011-05-31 17:53:55 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2011-05-31 17:53:55 -0400
commitb26d8fda6535799be6eee1e93a8f11756b11af95 (patch)
treecb0f8f88d02de074b57e4420352dc52cdb6184a6
parente73bd1e343c07970ed7d31c83b3d5035abd4e996 (diff)
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
-rw-r--r--doc/src/sgml/gist.sgml2
-rw-r--r--src/backend/access/gist/gistutil.c14
2 files changed, 13 insertions, 3 deletions
diff --git a/doc/src/sgml/gist.sgml b/doc/src/sgml/gist.sgml
index 5757ce80025..65084842d49 100644
--- a/doc/src/sgml/gist.sgml
+++ b/doc/src/sgml/gist.sgml
@@ -374,6 +374,8 @@ my_decompress(PG_FUNCTION_ARGS)
Returns a value indicating the <quote>cost</quote> of inserting the new
entry into a particular branch of the tree. Items will be inserted
down the path of least <function>penalty</function> in the tree.
+ Values returned by <function>penalty</function> should be non-negative.
+ If a negative value is returned, it will be treated as zero.
</para>
<para>
diff --git a/src/backend/access/gist/gistutil.c b/src/backend/access/gist/gistutil.c
index a23047642fd..48c44157be0 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;
}