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-20 12:10:57
Message-ID: aW9w0cD60CVVqheF@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 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)
| ^
../../../src/include/varatt.h:307:16: note: in expansion of macro ‘VARDATA_4B’
307 | return VARDATA_4B(PTR);
| ^~~~~~~~~~
../../../src/include/varatt.h: In function ‘VARDATA_SHORT’:
../../../src/include/varatt.h:263:33: warning: return discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
263 | #define VARDATA_1B(PTR) (((const varattrib_1b *) (PTR))->va_data)
| ^
../../../src/include/varatt.h:321:16: note: in expansion of macro ‘VARDATA_1B’
321 | return VARDATA_1B(PTR);
| ^~~~~~~~~~
../../../src/include/varatt.h: In function ‘VARDATA_EXTERNAL’:
../../../src/include/varatt.h:264:33: warning: return discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
264 | #define VARDATA_1B_E(PTR) (((const varattrib_1b_e *) (PTR))->va_data)
| ^
../../../src/include/varatt.h:342:16: note: in expansion of macro ‘VARDATA_1B_E’
342 | return VARDATA_1B_E(PTR);
| ^~~~~~~~~~~~
../../../src/include/varatt.h: In function ‘VARDATA_ANY’:
../../../src/include/varatt.h:488:52: warning: return discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
488 | return VARATT_IS_1B(PTR) ? VARDATA_1B(PTR) : VARDATA_4B(PTR);

as they are still used by functions that return non const:

"
static inline char *
VARDATA(const void *PTR)
{
return VARDATA_4B(PTR);
}
"

> - RangeType *r1 = *(RangeType **) key1;
> - RangeType *r2 = *(RangeType **) key2;
> + RangeType *r1 = *(RangeType *const *) key1;
> + RangeType *r2 = *(RangeType *const *) key2;

That's correct. My improved cocci script [1] would incorrectly produce:

- RangeType *r1 = *(RangeType **) key1;
- RangeType *r2 = *(RangeType **) key2;
+ RangeType *r1 = *(const RangeType **)key1;
+ RangeType *r2 = *(const RangeType **)key2;

Same for:

> - file_entry_t *fa = *((file_entry_t **) a);
> - file_entry_t *fb = *((file_entry_t **) b);
> + file_entry_t *fa = *((file_entry_t *const *) a);
> + file_entry_t *fb = *((file_entry_t *const *) b);
>
>

Same for:

> - Step *stepa = *((Step **) a);
> - Step *stepb = *((Step **) b);
> + Step *stepa = *((Step *const *) a);
> + Step *stepb = *((Step *const *) b);
>
> - Step *step = *((Step **) b);
> + Step *step = *((Step *const *) b);
>
> diff --git a/src/test/modules/libpq_pipeline/libpq_pipeline.c b/src/test/modules/libpq_pipeline/libpq_pipeline.c
> index 03371721460..4a9bb65b9bb 100644
> --- a/src/test/modules/libpq_pipeline/libpq_pipeline.c
> +++ b/src/test/modules/libpq_pipeline/libpq_pipeline.c
> @@ -1576,7 +1576,7 @@ test_singlerowmode(PGconn *conn)
> "SELECT generate_series(42, $1)",
> 1,
> NULL,
> - (const char **) param,
> + (const char *const *) param,
> NULL,
> NULL,
> 0) != 1)

Correct, also according to:

"
int
PQsendQueryParams(PGconn *conn,
const char *command,
int nParams,
const Oid *paramTypes,
const char *const *paramValues,
const int *paramLengths,
const int *paramFormats,
int resultFormat)
{
"

Also [1], detected a few more trivially fixable ones (see attached).

[1]: https://github.com/bdrouvot/coccinelle_on_pg/blob/main/misc/search_const_away.cocci

Regards,

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

Attachment Content-Type Size
v1-0001-More-Fix-accidentally-cast-away-qualifiers.txt text/plain 1.8 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Previous Message Akshay Joshi 2026-01-20 11:36:53 Re: [PATCH] Add pg_get_database_ddl() function to reconstruct CREATE DATABASE statement