Skip to content
Snippets Groups Projects
Commit 6923d699 authored by Tom Lane's avatar Tom Lane
Browse files

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
parent ba4cacf0
No related branches found
No related tags found
No related merge requests found
...@@ -378,6 +378,8 @@ my_decompress(PG_FUNCTION_ARGS) ...@@ -378,6 +378,8 @@ my_decompress(PG_FUNCTION_ARGS)
Returns a value indicating the <quote>cost</quote> of inserting the new Returns a value indicating the <quote>cost</quote> of inserting the new
entry into a particular branch of the tree. Items will be inserted entry into a particular branch of the tree. Items will be inserted
down the path of least <function>penalty</function> in the tree. 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>
<para> <para>
......
...@@ -13,6 +13,8 @@ ...@@ -13,6 +13,8 @@
*/ */
#include "postgres.h" #include "postgres.h"
#include <math.h>
#include "access/gist_private.h" #include "access/gist_private.h"
#include "access/reloptions.h" #include "access/reloptions.h"
#include "storage/freespace.h" #include "storage/freespace.h"
...@@ -526,16 +528,21 @@ gistpenalty(GISTSTATE *giststate, int attno, ...@@ -526,16 +528,21 @@ gistpenalty(GISTSTATE *giststate, int attno,
if (giststate->penaltyFn[attno].fn_strict == FALSE || if (giststate->penaltyFn[attno].fn_strict == FALSE ||
(isNullOrig == FALSE && isNullAdd == FALSE)) (isNullOrig == FALSE && isNullAdd == FALSE))
{
FunctionCall3Coll(&giststate->penaltyFn[attno], FunctionCall3Coll(&giststate->penaltyFn[attno],
giststate->supportCollation[attno], giststate->supportCollation[attno],
PointerGetDatum(orig), PointerGetDatum(orig),
PointerGetDatum(add), PointerGetDatum(add),
PointerGetDatum(&penalty)); PointerGetDatum(&penalty));
/* disallow negative or NaN penalty */
if (isnan(penalty) || penalty < 0.0)
penalty = 0.0;
}
else if (isNullOrig && isNullAdd) else if (isNullOrig && isNullAdd)
penalty = 0.0; penalty = 0.0;
else else
penalty = 1e10; /* try to prevent to mix null and non-null penalty = 1e10; /* try to prevent mixing null and non-null
* value */ * values */
return penalty; return penalty;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment