Skip to content

Commit c3b51e0

Browse files
committed
Bug: backend crashes in btbeginscan()->btrescan()->_bt_orderkeys()
when btree used in innerscan with run-time key which value passed by pointer. Fix: keys ordering stuff moved to _bt_first(). Pointed by Thomas Lockhart.
1 parent 917abdd commit c3b51e0

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

src/backend/access/nbtree/nbtree.c

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtree.c,v 1.18 1997/04/18 03:37:53 vadim Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtree.c,v 1.19 1997/05/05 03:41:17 vadim Exp $
1212
*
1313
* NOTES
1414
* This file contains only the public interface routines.
@@ -410,7 +410,6 @@ btrescan(IndexScanDesc scan, bool fromEnd, ScanKey scankey)
410410
{
411411
ItemPointer iptr;
412412
BTScanOpaque so;
413-
StrategyNumber strat;
414413

415414
so = (BTScanOpaque) scan->opaque;
416415

@@ -439,32 +438,18 @@ btrescan(IndexScanDesc scan, bool fromEnd, ScanKey scankey)
439438
scan->flags = 0x0;
440439
}
441440

442-
/* reset the scan key */
441+
/*
442+
* Reset the scan keys. Note that keys ordering stuff
443+
* moved to _bt_first. - vadim 05/05/97
444+
*/
443445
so->numberOfKeys = scan->numberOfKeys;
444-
so->numberOfFirstKeys = 0; /* may be changed by _bt_orderkeys */
445-
so->qual_ok = 1; /* may be changed by _bt_orderkeys */
446446
if (scan->numberOfKeys > 0) {
447447
memmove(scan->keyData,
448448
scankey,
449449
scan->numberOfKeys * sizeof(ScanKeyData));
450450
memmove(so->keyData,
451451
scankey,
452452
so->numberOfKeys * sizeof(ScanKeyData));
453-
/* order the keys in the qualification */
454-
_bt_orderkeys(scan->relation, so);
455-
}
456-
457-
/* finally, be sure that the scan exploits the tree order */
458-
scan->scanFromEnd = false;
459-
if ( so->numberOfKeys > 0 ) {
460-
strat = _bt_getstrat(scan->relation, 1 /* XXX */,
461-
so->keyData[0].sk_procedure);
462-
463-
if (strat == BTLessStrategyNumber
464-
|| strat == BTLessEqualStrategyNumber)
465-
scan->scanFromEnd = true;
466-
} else {
467-
scan->scanFromEnd = true;
468453
}
469454

470455
}

src/backend/access/nbtree/nbtsearch.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtsearch.c,v 1.18 1997/04/24 15:46:44 vadim Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtsearch.c,v 1.19 1997/05/05 03:41:19 vadim Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -751,16 +751,38 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
751751
ScanKeyData skdata;
752752
Size keysok;
753753

754+
rel = scan->relation;
754755
so = (BTScanOpaque) scan->opaque;
755-
if ( so->qual_ok == 0 ) /* may be set by _bt_orderkeys */
756+
757+
/*
758+
* Order the keys in the qualification and be sure
759+
* that the scan exploits the tree order.
760+
*/
761+
so->numberOfFirstKeys = 0; /* may be changed by _bt_orderkeys */
762+
so->qual_ok = 1; /* may be changed by _bt_orderkeys */
763+
scan->scanFromEnd = false;
764+
if ( so->numberOfKeys > 0 )
765+
{
766+
_bt_orderkeys(rel, so);
767+
768+
strat = _bt_getstrat(rel, 1, so->keyData[0].sk_procedure);
769+
770+
/* NOTE: it assumes ForwardScanDirection */
771+
if ( strat == BTLessStrategyNumber ||
772+
strat == BTLessEqualStrategyNumber )
773+
scan->scanFromEnd = true;
774+
}
775+
else
776+
scan->scanFromEnd = true;
777+
778+
if ( so->qual_ok == 0 )
756779
return ((RetrieveIndexResult) NULL);
757780

758781
/* if we just need to walk down one edge of the tree, do that */
759782
if (scan->scanFromEnd)
760783
return (_bt_endpoint(scan, dir));
761784

762-
rel = scan->relation;
763-
itupdesc = RelationGetTupleDescriptor(scan->relation);
785+
itupdesc = RelationGetTupleDescriptor(rel);
764786
current = &(scan->currentItemData);
765787

766788
/*

0 commit comments

Comments
 (0)