Skip to content

Commit 8c8a267

Browse files
committed
Fix freeing old values in index_store_float8_orderby_distances()
6cae9d2 has added an error in freeing old values in index_store_float8_orderby_distances() function. It looks for old value in scan->xs_orderbynulls[i] after setting a new value there. This commit fixes that. Also it removes short-circuit in handling distances == NULL situation. Now distances == NULL will be treated the same way as array with all null distances. That is, previous values will be freed if any. Reported-by: Tom Lane, Nikita Glukhov Discussion: https://postgr.es/m/CAPpHfdu2wcoAVAm3Ek66rP%3Duo_C-D84%2B%2Buf1VEcbyi_caBXWCA%40mail.gmail.com Discussion: https://postgr.es/m/426580d3-a668-b9d1-7b8e-f74d1a6524e0%40postgrespro.ru Backpatch-through: 12
1 parent 6cae9d2 commit 8c8a267

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

src/backend/access/index/indexam.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -852,37 +852,29 @@ index_store_float8_orderby_distances(IndexScanDesc scan, Oid *orderByTypes,
852852
{
853853
int i;
854854

855-
scan->xs_recheckorderby = recheckOrderBy;
856-
857-
if (!distances)
858-
{
859-
Assert(!scan->xs_recheckorderby);
860-
861-
for (i = 0; i < scan->numberOfOrderBys; i++)
862-
{
863-
scan->xs_orderbyvals[i] = (Datum) 0;
864-
scan->xs_orderbynulls[i] = true;
865-
}
855+
Assert(distances || !recheckOrderBy);
866856

867-
return;
868-
}
857+
scan->xs_recheckorderby = recheckOrderBy;
869858

870859
for (i = 0; i < scan->numberOfOrderBys; i++)
871860
{
872-
scan->xs_orderbynulls[i] = distances[i].isnull;
873-
874-
if (scan->xs_orderbynulls[i])
875-
scan->xs_orderbyvals[i] = (Datum) 0;
876-
877861
if (orderByTypes[i] == FLOAT8OID)
878862
{
879863
#ifndef USE_FLOAT8_BYVAL
880864
/* must free any old value to avoid memory leakage */
881865
if (!scan->xs_orderbynulls[i])
882866
pfree(DatumGetPointer(scan->xs_orderbyvals[i]));
883867
#endif
884-
if (!scan->xs_orderbynulls[i])
868+
if (distances && !distances[i].isnull)
869+
{
885870
scan->xs_orderbyvals[i] = Float8GetDatum(distances[i].value);
871+
scan->xs_orderbynulls[i] = false;
872+
}
873+
else
874+
{
875+
scan->xs_orderbyvals[i] = (Datum) 0;
876+
scan->xs_orderbynulls[i] = true;
877+
}
886878
}
887879
else if (orderByTypes[i] == FLOAT4OID)
888880
{
@@ -892,8 +884,16 @@ index_store_float8_orderby_distances(IndexScanDesc scan, Oid *orderByTypes,
892884
if (!scan->xs_orderbynulls[i])
893885
pfree(DatumGetPointer(scan->xs_orderbyvals[i]));
894886
#endif
895-
if (!scan->xs_orderbynulls[i])
887+
if (distances && !distances[i].isnull)
888+
{
896889
scan->xs_orderbyvals[i] = Float4GetDatum((float4) distances[i].value);
890+
scan->xs_orderbynulls[i] = false;
891+
}
892+
else
893+
{
894+
scan->xs_orderbyvals[i] = (Datum) 0;
895+
scan->xs_orderbynulls[i] = true;
896+
}
897897
}
898898
else
899899
{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy