Avoid possible overflow with ltsGetFreeBlock() in logtape.c
authorMichael Paquier
Tue, 29 Aug 2023 23:03:51 +0000 (08:03 +0900)
committerMichael Paquier
Tue, 29 Aug 2023 23:03:51 +0000 (08:03 +0900)
nFreeBlocks, defined as a long, stores the number of free blocks in a
logical tape.  ltsGetFreeBlock() has been using an int to store the
value of nFreeBlocks, which could lead to overflows on platforms where
long and int are not the same size (in short everything except Windows
where long is 4 bytes).

The problematic intermediate variable is switched to be a long instead
of an int.

Issue introduced by c02fdc9223015, so backpatch down to 13.

Author: Ranier vilela
Reviewed-by: Peter Geoghegan, David Rowley
Discussion: https://postgr.es/m/CAEudQApLDWCBR_xmwNjGBrDo+f+S4E87x3s7-+hoaKqYdtC4JQ@mail.gmail.com
Backpatch-through: 13

src/backend/utils/sort/logtape.c

index b518a4b9c5ac3a38b0e5a4ee2b4db2e4bef3b94b..31db75d6a44c9f8d05d36a5bbc65de17c11a4227 100644 (file)
@@ -379,7 +379,7 @@ ltsGetFreeBlock(LogicalTapeSet *lts)
 {
    long       *heap = lts->freeBlocks;
    long        blocknum;
-   int         heapsize;
+   long        heapsize;
    long        holeval;
    unsigned long holepos;