Skip to content

Commit ec03f2d

Browse files
committed
Fix pg_restore's misdesigned code for detecting archive file format.
Despite the clear comments pointing out that the duplicative code segments in ReadHead() and _discoverArchiveFormat() needed to be in sync, they were not: the latter did not bother to apply any of the sanity checks in the former. We'd missed noticing this partly because none of those checks would fail in scenarios we customarily test, and partly because the oversight would be masked if both segments execute, which they would in cases other than needing to autodetect the format of a non-seekable stdin source. However, in a case meeting all these requirements --- for example, trying to read a newer-than-supported archive format from non-seekable stdin --- pg_restore missed applying the version check and would likely dump core or otherwise misbehave. The whole thing is silly anyway, because there seems little reason to duplicate the logic beyond the one-line verification that the file starts with "PGDMP". There seems to have been an undocumented assumption that multiple major formats (major enough to require separate reader modules) would nonetheless share the first half-dozen fields of the custom-format header. This seems unlikely, so let's fix it by just nuking the duplicate logic in _discoverArchiveFormat(). Also get rid of the pointless attempt to seek back to the start of the file after successful autodetection. That wastes cycles and it means we have four behaviors to verify not two. Per bug #16951 from Sergey Koposov. This has been broken for decades, so back-patch to all supported versions. Discussion: https://postgr.es/m/[email protected]
1 parent 91e7c90 commit ec03f2d

File tree

3 files changed

+51
-110
lines changed

3 files changed

+51
-110
lines changed

src/bin/pg_dump/pg_backup_archiver.c

+39-98
Original file line numberDiff line numberDiff line change
@@ -2071,6 +2071,7 @@ _discoverArchiveFormat(ArchiveHandle *AH)
20712071
if (AH->lookahead)
20722072
free(AH->lookahead);
20732073

2074+
AH->readHeader = 0;
20742075
AH->lookaheadSize = 512;
20752076
AH->lookahead = pg_malloc0(512);
20762077
AH->lookaheadLen = 0;
@@ -2142,62 +2143,9 @@ _discoverArchiveFormat(ArchiveHandle *AH)
21422143

21432144
if (strncmp(sig, "PGDMP", 5) == 0)
21442145
{
2145-
int byteread;
2146-
char vmaj,
2147-
vmin,
2148-
vrev;
2149-
2150-
/*
2151-
* Finish reading (most of) a custom-format header.
2152-
*
2153-
* NB: this code must agree with ReadHead().
2154-
*/
2155-
if ((byteread = fgetc(fh)) == EOF)
2156-
READ_ERROR_EXIT(fh);
2157-
2158-
vmaj = byteread;
2159-
2160-
if ((byteread = fgetc(fh)) == EOF)
2161-
READ_ERROR_EXIT(fh);
2162-
2163-
vmin = byteread;
2164-
2165-
/* Save these too... */
2166-
AH->lookahead[AH->lookaheadLen++] = vmaj;
2167-
AH->lookahead[AH->lookaheadLen++] = vmin;
2168-
2169-
/* Check header version; varies from V1.0 */
2170-
if (vmaj > 1 || (vmaj == 1 && vmin > 0)) /* Version > 1.0 */
2171-
{
2172-
if ((byteread = fgetc(fh)) == EOF)
2173-
READ_ERROR_EXIT(fh);
2174-
2175-
vrev = byteread;
2176-
AH->lookahead[AH->lookaheadLen++] = vrev;
2177-
}
2178-
else
2179-
vrev = 0;
2180-
2181-
AH->version = MAKE_ARCHIVE_VERSION(vmaj, vmin, vrev);
2182-
2183-
if ((AH->intSize = fgetc(fh)) == EOF)
2184-
READ_ERROR_EXIT(fh);
2185-
AH->lookahead[AH->lookaheadLen++] = AH->intSize;
2186-
2187-
if (AH->version >= K_VERS_1_7)
2188-
{
2189-
if ((AH->offSize = fgetc(fh)) == EOF)
2190-
READ_ERROR_EXIT(fh);
2191-
AH->lookahead[AH->lookaheadLen++] = AH->offSize;
2192-
}
2193-
else
2194-
AH->offSize = AH->intSize;
2195-
2196-
if ((byteread = fgetc(fh)) == EOF)
2197-
READ_ERROR_EXIT(fh);
2198-
2199-
AH->format = byteread;
2200-
AH->lookahead[AH->lookaheadLen++] = AH->format;
2146+
/* It's custom format, stop here */
2147+
AH->format = archCustom;
2148+
AH->readHeader = 1;
22012149
}
22022150
else
22032151
{
@@ -2234,22 +2182,15 @@ _discoverArchiveFormat(ArchiveHandle *AH)
22342182
AH->format = archTar;
22352183
}
22362184

2237-
/* If we can't seek, then mark the header as read */
2238-
if (fseeko(fh, 0, SEEK_SET) != 0)
2239-
{
2240-
/*
2241-
* NOTE: Formats that use the lookahead buffer can unset this in their
2242-
* Init routine.
2243-
*/
2244-
AH->readHeader = 1;
2245-
}
2246-
else
2247-
AH->lookaheadLen = 0; /* Don't bother since we've reset the file */
2248-
2249-
/* Close the file */
2185+
/* Close the file if we opened it */
22502186
if (wantClose)
2187+
{
22512188
if (fclose(fh) != 0)
22522189
fatal("could not close input file: %m");
2190+
/* Forget lookahead, since we'll re-read header after re-opening */
2191+
AH->readHeader = 0;
2192+
AH->lookaheadLen = 0;
2193+
}
22532194

22542195
return AH->format;
22552196
}
@@ -3790,7 +3731,9 @@ WriteHead(ArchiveHandle *AH)
37903731
void
37913732
ReadHead(ArchiveHandle *AH)
37923733
{
3793-
char tmpMag[7];
3734+
char vmaj,
3735+
vmin,
3736+
vrev;
37943737
int fmt;
37953738
struct tm crtm;
37963739

@@ -3802,48 +3745,46 @@ ReadHead(ArchiveHandle *AH)
38023745
*/
38033746
if (!AH->readHeader)
38043747
{
3805-
char vmaj,
3806-
vmin,
3807-
vrev;
3748+
char tmpMag[7];
38083749

38093750
AH->ReadBufPtr(AH, tmpMag, 5);
38103751

38113752
if (strncmp(tmpMag, "PGDMP", 5) != 0)
38123753
fatal("did not find magic string in file header");
3754+
}
38133755

3814-
vmaj = AH->ReadBytePtr(AH);
3815-
vmin = AH->ReadBytePtr(AH);
3756+
vmaj = AH->ReadBytePtr(AH);
3757+
vmin = AH->ReadBytePtr(AH);
38163758

3817-
if (vmaj > 1 || (vmaj == 1 && vmin > 0)) /* Version > 1.0 */
3818-
vrev = AH->ReadBytePtr(AH);
3819-
else
3820-
vrev = 0;
3759+
if (vmaj > 1 || (vmaj == 1 && vmin > 0)) /* Version > 1.0 */
3760+
vrev = AH->ReadBytePtr(AH);
3761+
else
3762+
vrev = 0;
38213763

3822-
AH->version = MAKE_ARCHIVE_VERSION(vmaj, vmin, vrev);
3764+
AH->version = MAKE_ARCHIVE_VERSION(vmaj, vmin, vrev);
38233765

3824-
if (AH->version < K_VERS_1_0 || AH->version > K_VERS_MAX)
3825-
fatal("unsupported version (%d.%d) in file header",
3826-
vmaj, vmin);
3766+
if (AH->version < K_VERS_1_0 || AH->version > K_VERS_MAX)
3767+
fatal("unsupported version (%d.%d) in file header",
3768+
vmaj, vmin);
38273769

3828-
AH->intSize = AH->ReadBytePtr(AH);
3829-
if (AH->intSize > 32)
3830-
fatal("sanity check on integer size (%lu) failed",
3831-
(unsigned long) AH->intSize);
3770+
AH->intSize = AH->ReadBytePtr(AH);
3771+
if (AH->intSize > 32)
3772+
fatal("sanity check on integer size (%lu) failed",
3773+
(unsigned long) AH->intSize);
38323774

3833-
if (AH->intSize > sizeof(int))
3834-
pg_log_warning("archive was made on a machine with larger integers, some operations might fail");
3775+
if (AH->intSize > sizeof(int))
3776+
pg_log_warning("archive was made on a machine with larger integers, some operations might fail");
38353777

3836-
if (AH->version >= K_VERS_1_7)
3837-
AH->offSize = AH->ReadBytePtr(AH);
3838-
else
3839-
AH->offSize = AH->intSize;
3778+
if (AH->version >= K_VERS_1_7)
3779+
AH->offSize = AH->ReadBytePtr(AH);
3780+
else
3781+
AH->offSize = AH->intSize;
38403782

3841-
fmt = AH->ReadBytePtr(AH);
3783+
fmt = AH->ReadBytePtr(AH);
38423784

3843-
if (AH->format != fmt)
3844-
fatal("expected format (%d) differs from format found in file (%d)",
3845-
AH->format, fmt);
3846-
}
3785+
if (AH->format != fmt)
3786+
fatal("expected format (%d) differs from format found in file (%d)",
3787+
AH->format, fmt);
38473788

38483789
if (AH->version >= K_VERS_1_2)
38493790
{

src/bin/pg_dump/pg_backup_archiver.h

+12-6
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,21 @@ struct _archiveHandle
253253
time_t createDate; /* Date archive created */
254254

255255
/*
256-
* Fields used when discovering header. A format can always get the
257-
* previous read bytes from here...
256+
* Fields used when discovering archive format. For tar format, we load
257+
* the first block into the lookahead buffer, and verify that it looks
258+
* like a tar header. The tar module must then consume bytes from the
259+
* lookahead buffer before reading any more from the file. For custom
260+
* format, we load only the "PGDMP" marker into the buffer, and then set
261+
* readHeader after confirming it matches. The buffer is vestigial in
262+
* this case, as the subsequent code just checks readHeader and doesn't
263+
* examine the buffer.
258264
*/
259-
int readHeader; /* Used if file header has been read already */
265+
int readHeader; /* Set if we already read "PGDMP" marker */
260266
char *lookahead; /* Buffer used when reading header to discover
261267
* format */
262-
size_t lookaheadSize; /* Size of allocated buffer */
263-
size_t lookaheadLen; /* Length of data in lookahead */
264-
pgoff_t lookaheadPos; /* Current read position in lookahead buffer */
268+
size_t lookaheadSize; /* Allocated size of buffer */
269+
size_t lookaheadLen; /* Length of valid data in lookahead */
270+
size_t lookaheadPos; /* Current read position in lookahead buffer */
265271

266272
ArchiveEntryPtrType ArchiveEntryPtr; /* Called for each metadata object */
267273
StartDataPtrType StartDataPtr; /* Called when table data is about to be

src/bin/pg_dump/pg_backup_tar.c

-6
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,6 @@ InitArchiveFmt_Tar(ArchiveHandle *AH)
228228

229229
ctx->hasSeek = checkSeek(ctx->tarFH);
230230

231-
/*
232-
* Forcibly unmark the header as read since we use the lookahead
233-
* buffer
234-
*/
235-
AH->readHeader = 0;
236-
237231
ctx->FH = (void *) tarOpen(AH, "toc.dat", 'r');
238232
ReadHead(AH);
239233
ReadToc(AH);

0 commit comments

Comments
 (0)