Re: incompatible pointer types with newer zlib

Lists: pgsql-hackers
From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: incompatible pointer types with newer zlib
Date: 2012-02-23 09:16:11
Message-ID: 1329988571.6474.9.camel@vanquo.pezone.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

According to me update logs, somewhere between zlib versions 1.2.3.4 and
1.2.6, the definition of the gzFile type was changed from void * to
struct gzFile_s *, an opaque struct. Note that gzFile is already the
pointer in either case. Our code has assumed, however, that you use
gzFile like FILE, namely that the APIs take a pointer to the type, but
that is not the case. So code like

gzFile *handle = gzopen(...)

is wrong.

This used to pass silently because you can assign a void* to a void**,
but with the newer definition you get a bunch of warnings like

pg_backup_files.c: In function ‘_StartData’:
pg_backup_files.c:256:11: warning: assignment from incompatible pointer type [enabled by default]
pg_backup_files.c: In function ‘_WriteData’:
pg_backup_files.c:271:2: warning: passing argument 1 of ‘gzwrite’ from incompatible pointer type [enabled by default]
/usr/include/zlib.h:1318:12: note: expected ‘gzFile’ but argument is of type ‘struct gzFile_s **’

Affected are pg_dump and pg_basebackup.

Fixing most of this is not difficult, see attached patch. The only
ugliness is in pg_backup_archiver.h

FILE *FH; /* General purpose file handle */

which is used throughout pg_dump as sometimes a real FILE* and sometimes
a gzFile handle. There are also some fileno() calls on this, so just
replacing this with an #ifdef isn't going to work. This might need some
more restructuring to make the code truly type-safe. My quick patch
replaces the type with void*, thus sort of restoring the original
situation that allowed this to work.

Note that these are only warnings, so we probably don't need to worry
about backpatching this in a hurry.

Attachment Content-Type Size
gzfile.patch text/x-patch 2.3 KB

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: incompatible pointer types with newer zlib
Date: 2012-02-23 15:17:28
Message-ID: 13579.1330010248@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Peter Eisentraut <peter_e(at)gmx(dot)net> writes:
> Fixing most of this is not difficult, see attached patch. The only
> ugliness is in pg_backup_archiver.h

> FILE *FH; /* General purpose file handle */

> which is used throughout pg_dump as sometimes a real FILE* and sometimes
> a gzFile handle. There are also some fileno() calls on this, so just
> replacing this with an #ifdef isn't going to work. This might need some
> more restructuring to make the code truly type-safe. My quick patch
> replaces the type with void*, thus sort of restoring the original
> situation that allowed this to work.

void * seems entirely reasonable given the two different usages, but
I would be happier if the patch added explicit casts whereever FH is
set to or used as one of these two types.

regards, tom lane


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: incompatible pointer types with newer zlib
Date: 2012-02-24 10:01:13
Message-ID: 1330077673.32452.9.camel@vanquo.pezone.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On tor, 2012-02-23 at 10:17 -0500, Tom Lane wrote:
> Peter Eisentraut <peter_e(at)gmx(dot)net> writes:
> > Fixing most of this is not difficult, see attached patch. The only
> > ugliness is in pg_backup_archiver.h
>
> > FILE *FH; /* General purpose file handle */
>
> > which is used throughout pg_dump as sometimes a real FILE* and sometimes
> > a gzFile handle. There are also some fileno() calls on this, so just
> > replacing this with an #ifdef isn't going to work. This might need some
> > more restructuring to make the code truly type-safe. My quick patch
> > replaces the type with void*, thus sort of restoring the original
> > situation that allowed this to work.
>
> void * seems entirely reasonable given the two different usages, but
> I would be happier if the patch added explicit casts whereever FH is
> set to or used as one of these two types.

That would add about 70 casts all over the place. I don't think that
will make things clearer or more robust. I think we either leave it as
my first patch for now or find a more robust solution with a union or
something.


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: incompatible pointer types with newer zlib
Date: 2012-02-24 16:10:16
Message-ID: 20273.1330099816@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Peter Eisentraut <peter_e(at)gmx(dot)net> writes:
> On tor, 2012-02-23 at 10:17 -0500, Tom Lane wrote:
>> void * seems entirely reasonable given the two different usages, but
>> I would be happier if the patch added explicit casts whereever FH is
>> set to or used as one of these two types.

> That would add about 70 casts all over the place. I don't think that
> will make things clearer or more robust. I think we either leave it as
> my first patch for now or find a more robust solution with a union or
> something.

Hm. Could we just create two separate fields? It's not real clear to
me that forcing both these usages into a generic pointer buys much.

regards, tom lane


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: incompatible pointer types with newer zlib
Date: 2012-03-01 17:19:54
Message-ID: 1330622394.23823.6.camel@vanquo.pezone.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On fre, 2012-02-24 at 11:10 -0500, Tom Lane wrote:
> Peter Eisentraut <peter_e(at)gmx(dot)net> writes:
> > On tor, 2012-02-23 at 10:17 -0500, Tom Lane wrote:
> >> void * seems entirely reasonable given the two different usages, but
> >> I would be happier if the patch added explicit casts whereever FH is
> >> set to or used as one of these two types.
>
> > That would add about 70 casts all over the place. I don't think that
> > will make things clearer or more robust. I think we either leave it as
> > my first patch for now or find a more robust solution with a union or
> > something.
>
> Hm. Could we just create two separate fields? It's not real clear to
> me that forcing both these usages into a generic pointer buys much.

It's used in confusing ways.

In pg_backup_files.c _PrintFileData(), it's a compile-time decision
whether FH is a FILE or a gzFile:

#ifdef HAVE_LIBZ
AH->FH = gzopen(filename, "rb");
#else
AH->FH = fopen(filename, PG_BINARY_R);
#endif

But if we changed FH to be gzFile under HAVE_LIBZ, then tons of other
places will complain that use fread(), fwrite(), fileno(), etc. directly
on FH.

Considering the volume of who complains in one way versus the other, I
think _PrintFileData() is at fault.

I think the best fix would be to rearrange _PrintFileData() so that it
doesn't use FH at all. Instead, we could define a separate
ArchiveHandle field IF that works more like OF, and then change
ahwrite() to use that.


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: incompatible pointer types with newer zlib
Date: 2012-03-17 07:58:49
Message-ID: 1331971129.32357.1.camel@vanquo.pezone.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On tor, 2012-03-01 at 19:19 +0200, Peter Eisentraut wrote:
> I think the best fix would be to rearrange _PrintFileData() so that it
> doesn't use FH at all. Instead, we could define a separate
> ArchiveHandle field IF that works more like OF, and then change
> ahwrite() to use that.

Here is a patch that might fix this. I haven't been able to test this
properly, so this is just from tracing the code. It looks like
_PrintFileData() doesn't need to use FH at all, so it could use a local
file handle variable instead. Could someone verify this please?

Attachment Content-Type Size
pg_dump-zlib-fix.patch text/x-patch 1.0 KB

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: incompatible pointer types with newer zlib
Date: 2012-03-19 16:50:54
Message-ID: CA+TgmoZpXhZd+5+Aqw2Nfub=egWp_rQcbwQ9_RH-cz-3eu4r=A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Sat, Mar 17, 2012 at 3:58 AM, Peter Eisentraut <peter_e(at)gmx(dot)net> wrote:
> On tor, 2012-03-01 at 19:19 +0200, Peter Eisentraut wrote:
>> I think the best fix would be to rearrange _PrintFileData() so that it
>> doesn't use FH at all.  Instead, we could define a separate
>> ArchiveHandle field IF that works more like OF, and then change
>> ahwrite() to use that.
>
> Here is a patch that might fix this.  I haven't been able to test this
> properly, so this is just from tracing the code.  It looks like
> _PrintFileData() doesn't need to use FH at all, so it could use a local
> file handle variable instead.  Could someone verify this please?

It looks like this code can be used via the undocumented -Ff option to
pg_dump. But considering this code was added in 2000 as demonstration
code and has apparently never been documented, and considering also
that we now have the "directory" archive format which is presumably
quite a similar idea but documented and intended for production use,
maybe we should just rip out pg_backup_files/archFiles altogether.
pg_dump is crufty enough without supporting undocumented and obsolete
options for multiple decades.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Peter Eisentraut <peter_e(at)gmx(dot)net>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: incompatible pointer types with newer zlib
Date: 2012-03-19 18:53:37
Message-ID: 16334.1332183217@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas <robertmhaas(at)gmail(dot)com> writes:
> maybe we should just rip out pg_backup_files/archFiles altogether.
> pg_dump is crufty enough without supporting undocumented and obsolete
> options for multiple decades.

+1

regards, tom lane


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, Peter Eisentraut <peter_e(at)gmx(dot)net>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: incompatible pointer types with newer zlib
Date: 2012-03-19 19:06:28
Message-ID: 4F6783B4.3030607@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 03/19/2012 02:53 PM, Tom Lane wrote:
> Robert Haas<robertmhaas(at)gmail(dot)com> writes:
>> maybe we should just rip out pg_backup_files/archFiles altogether.
>> pg_dump is crufty enough without supporting undocumented and obsolete
>> options for multiple decades.
> +1
>
>

Yeah, go for it.

cheers

andrew