Re: [patch] pg_dump/pg_restore zerror() and strerror() mishap

From: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
To: Kunshchikov Vladimir <Vladimir(dot)Kunshchikov(at)infotecs(dot)ru>
Cc: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: [patch] pg_dump/pg_restore zerror() and strerror() mishap
Date: 2017-07-28 16:25:52
Message-ID: 20170728162552.fhr5nfj5dvvxjhdh@alvherre.pgsql
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Kunshchikov Vladimir wrote:
> Hello Alvaro,
>
> here goes v4 version: removed unused header.
>
> Compilation of this code snippet with -Wall -Wexter -std=c89 doesn't produce any warnings.

Great, thanks.

+const char *
+get_cfp_error(cfp* fp)
+{
+#ifdef HAVE_LIBZ
+ if(fp->compressedfp){
+ int errnum;
+ static const char fallback[] = "Zlib error";
+ const int maxlen = 255;
+ const char *errmsg = gzerror(fp->compressedfp, &errnum);
+ if(!errmsg || !memchr(errmsg, 0, maxlen))
+ errmsg = fallback;
+
+ return errnum == Z_ERRNO ? strerror(errno) : errmsg;
+ }
#endif
+ return strerror(errno);
+}

This "maxlen" business and the fallback error message are strange. We
have roughly equivalent code in pg_basebackup.c, which has been working
since 2011 (commit 048d148fe631), and it looks like this:

#ifdef HAVE_LIBZ
static const char *
get_gz_error(gzFile gzf)
{
int errnum;
const char *errmsg;

errmsg = gzerror(gzf, &errnum);
if (errnum == Z_ERRNO)
return strerror(errno);
else
return errmsg;
}
#endif

Perhaps you can drop the memchr/fallback tricks and adopt the
pg_basebackup coding? Or is there a specific reason to have the memchr
check?

--
Álvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Geoghegan 2017-07-28 16:26:55 Re: LP_DEAD hinting and not holding on to a buffer pin on leaf page (Was: [WIP] Zipfian distribution in pgbench)
Previous Message Jeff Janes 2017-07-28 16:22:52 Re: tab complete for psql pset pager values