Skip to content

Commit c86f8f3

Browse files
committed
Fix failure to honor -Z compression level option in pg_dump -Fd.
cfopen() and cfopen_write() failed to pass the compression level through to zlib, so that you always got the default compression level if you got any at all. In passing, also fix these and related functions so that the correct errno is reliably returned on failure; the original coding supposes that free() cannot change errno, which is untrue on at least some platforms. Per bug #12779 from Christoph Berg. Back-patch to 9.1 where the faulty code was introduced. Michael Paquier
1 parent d068609 commit c86f8f3

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

src/bin/pg_dump/compress_io.c

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -455,13 +455,25 @@ struct cfp
455455
static int hasSuffix(const char *filename, const char *suffix);
456456
#endif
457457

458+
/* free() without changing errno; useful in several places below */
459+
static void
460+
free_keep_errno(void *p)
461+
{
462+
int save_errno = errno;
463+
464+
free(p);
465+
errno = save_errno;
466+
}
467+
458468
/*
459469
* Open a file for reading. 'path' is the file to open, and 'mode' should
460470
* be either "r" or "rb".
461471
*
462472
* If the file at 'path' does not exist, we append the ".gz" suffix (if 'path'
463473
* doesn't already have it) and try again. So if you pass "foo" as 'path',
464474
* this will open either "foo" or "foo.gz".
475+
*
476+
* On failure, return NULL with an error code in errno.
465477
*/
466478
cfp *
467479
cfopen_read(const char *path, const char *mode)
@@ -483,7 +495,7 @@ cfopen_read(const char *path, const char *mode)
483495

484496
snprintf(fname, fnamelen, "%s%s", path, ".gz");
485497
fp = cfopen(fname, mode, 1);
486-
free(fname);
498+
free_keep_errno(fname);
487499
}
488500
#endif
489501
}
@@ -496,8 +508,10 @@ cfopen_read(const char *path, const char *mode)
496508
* ("w", "wb", "a", or "ab").
497509
*
498510
* If 'compression' is non-zero, a gzip compressed stream is opened, and
499-
* and 'compression' indicates the compression level used. The ".gz" suffix
511+
* 'compression' indicates the compression level used. The ".gz" suffix
500512
* is automatically added to 'path' in that case.
513+
*
514+
* On failure, return NULL with an error code in errno.
501515
*/
502516
cfp *
503517
cfopen_write(const char *path, const char *mode, int compression)
@@ -513,8 +527,8 @@ cfopen_write(const char *path, const char *mode, int compression)
513527
char *fname = pg_malloc(fnamelen);
514528

515529
snprintf(fname, fnamelen, "%s%s", path, ".gz");
516-
fp = cfopen(fname, mode, 1);
517-
free(fname);
530+
fp = cfopen(fname, mode, compression);
531+
free_keep_errno(fname);
518532
#else
519533
exit_horribly(modulename, "not built with zlib support\n");
520534
fp = NULL; /* keep compiler quiet */
@@ -525,7 +539,9 @@ cfopen_write(const char *path, const char *mode, int compression)
525539

526540
/*
527541
* Opens file 'path' in 'mode'. If 'compression' is non-zero, the file
528-
* is opened with libz gzopen(), otherwise with plain fopen()
542+
* is opened with libz gzopen(), otherwise with plain fopen().
543+
*
544+
* On failure, return NULL with an error code in errno.
529545
*/
530546
cfp *
531547
cfopen(const char *path, const char *mode, int compression)
@@ -535,11 +551,15 @@ cfopen(const char *path, const char *mode, int compression)
535551
if (compression != 0)
536552
{
537553
#ifdef HAVE_LIBZ
538-
fp->compressedfp = gzopen(path, mode);
554+
char mode_compression[32];
555+
556+
snprintf(mode_compression, sizeof(mode_compression), "%s%d",
557+
mode, compression);
558+
fp->compressedfp = gzopen(path, mode_compression);
539559
fp->uncompressedfp = NULL;
540560
if (fp->compressedfp == NULL)
541561
{
542-
free(fp);
562+
free_keep_errno(fp);
543563
fp = NULL;
544564
}
545565
#else
@@ -554,7 +574,7 @@ cfopen(const char *path, const char *mode, int compression)
554574
fp->uncompressedfp = fopen(path, mode);
555575
if (fp->uncompressedfp == NULL)
556576
{
557-
free(fp);
577+
free_keep_errno(fp);
558578
fp = NULL;
559579
}
560580
}
@@ -629,7 +649,7 @@ cfclose(cfp *fp)
629649
result = fclose(fp->uncompressedfp);
630650
fp->uncompressedfp = NULL;
631651
}
632-
free(fp);
652+
free_keep_errno(fp);
633653

634654
return result;
635655
}

0 commit comments

Comments
 (0)