Re: Fix accidentally cast away qualifiers

From: Bertrand Drouvot <bertranddrouvot(dot)pg(at)gmail(dot)com>
To: Peter Eisentraut <peter(at)eisentraut(dot)org>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Fix accidentally cast away qualifiers
Date: 2026-01-21 14:24:58
Message-ID: aXDhus3a7j+dTyUe@ip-10-97-1-34.eu-west-3.compute.internal
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

On Wed, Jan 21, 2026 at 12:31:27PM +0100, Peter Eisentraut wrote:
> On 20.01.26 13:10, Bertrand Drouvot wrote:
> > On Tue, Jan 20, 2026 at 08:54:18AM +0100, Peter Eisentraut wrote:
> > > This patch fixes cases where a qualifier (const, in all cases here) was
> > > dropped by a cast, but the cast was otherwise necessary or desirable, so the
> > > straightforward fix is to add the qualifier into the cast.
> > >
> > > This was checked with gcc -Wcast-qual, but it doesn't fix all such warnings,
> > > only the trivially fixable ones.
> >
> > diff --git a/src/include/varatt.h b/src/include/varatt.h
> > index eccd3ca04d6..03e9d1869aa 100644
> > --- a/src/include/varatt.h
> > +++ b/src/include/varatt.h
> >
> > It looks like those changes produce:
> >
> > ../../../src/include/varatt.h: In function ‘VARDATA’:
> > ../../../src/include/varatt.h:261:33: warning: return discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
> > 261 | #define VARDATA_4B(PTR) (((const varattrib_4b *) (PTR))->va_4byte.va_data)
>
> Strange, I don't see that. What compiler is this, and do you use any
> special options?

It's gcc 14.1.0 with "CFLAGS=-O0 -ggdb3 -fno-omit-frame-pointer -gdwarf-2 -g3
-Wdiscarded-qualifiers -Wunused-value -Werror=maybe-uninitialized
-Werror=format -Wreturn-type"

But I just tested with a simple test case, some compilers and some options and I've
always seen the warning (see [1]), so I'm not sure why you don't.

>
> > Also [1], detected a few more trivially fixable ones (see attached).
>
> Yes, these should be included.
>
> The one in spgquadtreeproc.c was #ifdef'ed out, so my testing didn't see it.
> I suppose the other one is only compiled when you run the unicode tests.

Yeah, that could lead to the same "concern" that Tom had in [2] (and using
a tool like Coccinelle could be an answer).

[1]: https://godbolt.org/z/T9hffKaPW
[2]: https://postgr.es/m/aVJCoNqO6ybov7eW%40ip-10-97-1-34.eu-west-3.compute.internal

Regards,

--
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message zengman 2026-01-21 14:37:23 [PATCH] Align verify_heapam.c error message offset with test expectations
Previous Message Peter Eisentraut 2026-01-21 14:19:17 Re: AIX support