Skip to content

Commit 1578f50

Browse files
committed
fixed gz_error/strerror mishap
1 parent d14c85e commit 1578f50

File tree

4 files changed

+44
-24
lines changed

4 files changed

+44
-24
lines changed

src/bin/pg_dump/compress_io.c

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -422,23 +422,6 @@ WriteDataToArchiveNone(ArchiveHandle *AH, CompressorState *cs,
422422
}
423423

424424

425-
/*----------------------
426-
* Compressed stream API
427-
*----------------------
428-
*/
429-
430-
/*
431-
* cfp represents an open stream, wrapping the underlying FILE or gzFile
432-
* pointer. This is opaque to the callers.
433-
*/
434-
struct cfp
435-
{
436-
FILE *uncompressedfp;
437-
#ifdef HAVE_LIBZ
438-
gzFile compressedfp;
439-
#endif
440-
};
441-
442425
#ifdef HAVE_LIBZ
443426
static int hasSuffix(const char *filename, const char *suffix);
444427
#endif
@@ -593,7 +576,7 @@ cfread(void *ptr, int size, cfp *fp)
593576
ret = gzread(fp->compressedfp, ptr, size);
594577
if (ret != size && !gzeof(fp->compressedfp))
595578
exit_horribly(modulename,
596-
"could not read from input file: %s\n", strerror(errno));
579+
"could not read from input file: %s\n", get_gz_error(fp->compressedfp));
597580
}
598581
else
599582
#endif
@@ -629,7 +612,7 @@ cfgetc(cfp *fp)
629612
{
630613
if (!gzeof(fp->compressedfp))
631614
exit_horribly(modulename,
632-
"could not read from input file: %s\n", strerror(errno));
615+
"could not read from input file: %s\n", get_gz_error(fp->compressedfp));
633616
else
634617
exit_horribly(modulename,
635618
"could not read from input file: end of file\n");
@@ -710,4 +693,16 @@ hasSuffix(const char *filename, const char *suffix)
710693
suffixlen) == 0;
711694
}
712695

696+
const char *
697+
get_gz_error(gzFile gzf)
698+
{
699+
int errnum;
700+
static const char fallback[] = "Zlib error";
701+
const int maxlen = 255;
702+
const char *errmsg = gzerror(gzf, &errnum);
703+
if(!errmsg || !memchr(errmsg, 0, maxlen))
704+
errmsg = fallback;
705+
706+
return errnum == Z_ERRNO ? strerror(errno) : errmsg;
707+
}
713708
#endif

src/bin/pg_dump/compress_io.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,24 @@ typedef enum
2727
COMPR_ALG_LIBZ
2828
} CompressionAlgorithm;
2929

30+
/*----------------------
31+
* Compressed stream API
32+
*----------------------
33+
*/
34+
35+
/*
36+
* cfp represents an open stream, wrapping the underlying FILE or gzFile
37+
* pointer. This is opaque to the callers.
38+
*/
39+
typedef struct
40+
{
41+
FILE *uncompressedfp;
42+
#ifdef HAVE_LIBZ
43+
gzFile compressedfp;
44+
#endif
45+
} cfp;
46+
47+
3048
/* Prototype for callback function to WriteDataToArchive() */
3149
typedef void (*WriteFunc) (ArchiveHandle *AH, const char *buf, size_t len);
3250

@@ -52,10 +70,6 @@ extern void ReadDataFromArchive(ArchiveHandle *AH, int compression,
5270
extern void WriteDataToArchive(ArchiveHandle *AH, CompressorState *cs,
5371
const void *data, size_t dLen);
5472
extern void EndCompressor(ArchiveHandle *AH, CompressorState *cs);
55-
56-
57-
typedef struct cfp cfp;
58-
5973
extern cfp *cfopen(const char *path, const char *mode, int compression);
6074
extern cfp *cfopen_read(const char *path, const char *mode);
6175
extern cfp *cfopen_write(const char *path, const char *mode, int compression);
@@ -65,5 +79,6 @@ extern int cfgetc(cfp *fp);
6579
extern char *cfgets(cfp *fp, char *buf, int len);
6680
extern int cfclose(cfp *fp);
6781
extern int cfeof(cfp *fp);
82+
extern const char * get_gz_error(gzFile gzf);
6883

6984
#endif

src/bin/pg_dump/pg_backup_directory.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@
4242
#include <dirent.h>
4343
#include <sys/stat.h>
4444

45+
#ifdef WRITE_ERROR_EXIT
46+
#undef WRITE_ERROR_EXIT
47+
#endif
48+
#define WRITE_ERROR_EXIT \
49+
do { \
50+
exit_horribly(modulename, "could not write to output file: %s\n", \
51+
ctx && ctx->dataFH && ctx->dataFH->compressedfp? get_gz_error(ctx->dataFH->compressedfp) : strerror(errno)); \
52+
} while (0)
53+
4554
typedef struct
4655
{
4756
/*

src/bin/pg_dump/pg_backup_tar.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "pgtar.h"
3636
#include "common/file_utils.h"
3737
#include "fe_utils/string_utils.h"
38+
#include "compress_io.h"
3839

3940
#include <sys/stat.h>
4041
#include <ctype.h>
@@ -556,7 +557,7 @@ _tarReadRaw(ArchiveHandle *AH, void *buf, size_t len, TAR_MEMBER *th, FILE *fh)
556557
res = GZREAD(&((char *) buf)[used], 1, len, th->zFH);
557558
if (res != len && !GZEOF(th->zFH))
558559
exit_horribly(modulename,
559-
"could not read from input file: %s\n", strerror(errno));
560+
"could not read from input file: %s\n", get_gz_error(th->zFH));
560561
}
561562
else
562563
{

0 commit comments

Comments
 (0)