logtape.c: allocate read buffer even for an empty tape.
authorJeff Davis <jdavis@postgresql.org>
Tue, 18 Feb 2020 20:31:24 +0000 (12:31 -0800)
committerJeff Davis <jdavis@postgresql.org>
Wed, 19 Feb 2020 18:04:17 +0000 (10:04 -0800)
Prior to this commit, the read buffer was allocated at the time the tape
was rewound; but as an optimization, would not be allocated at all if
the tape was empty.

That optimization meant that it was valid to have a rewound tape with
the buffer set to NULL, but only if a number of conditions were met
and only if the API was used properly. After 7fdd919a refactored the
code to support lazily-allocating the buffer, Coverity started
complaining.

The optimization for empty tapes doesn't seem important, so just
allocate the buffer whether the tape has any data or not.

Discussion: https://postgr.es/m/20351.1581868306%40sss.pgh.pa.us

src/backend/utils/sort/logtape.c

index fd7624c23128b8b153856830a6fb50b24c8888f9..4f78b55fbafb0c97d6a46332873197b73e9a0749 100644 (file)
@@ -544,11 +544,8 @@ ltsConcatWorkerTapes(LogicalTapeSet *lts, TapeShare *shared,
 static void
 ltsInitReadBuffer(LogicalTapeSet *lts, LogicalTape *lt)
 {
-   if (lt->firstBlockNumber != -1L)
-   {
-       Assert(lt->buffer_size > 0);
-       lt->buffer = palloc(lt->buffer_size);
-   }
+   Assert(lt->buffer_size > 0);
+   lt->buffer = palloc(lt->buffer_size);
 
    /* Read the first block, or reset if tape is empty */
    lt->nextBlockNumber = lt->firstBlockNumber;
@@ -839,13 +836,10 @@ LogicalTapeRewindForRead(LogicalTapeSet *lts, int tapenum, size_t buffer_size)
    /* Allocate a read buffer (unless the tape is empty) */
    if (lt->buffer)
        pfree(lt->buffer);
+
+   /* the buffer is lazily allocated, but set the size here */
    lt->buffer = NULL;
-   lt->buffer_size = 0;
-   if (lt->firstBlockNumber != -1L)
-   {
-       /* the buffer is lazily allocated, but set the size here */
-       lt->buffer_size = buffer_size;
-   }
+   lt->buffer_size = buffer_size;
 }
 
 /*