Re: Fix accidentally cast away qualifiers - Mailing list pgsql-hackers

From Bertrand Drouvot
Subject Re: Fix accidentally cast away qualifiers
Date
Msg-id aW9w0cD60CVVqheF@ip-10-97-1-34.eu-west-3.compute.internal
Whole thread Raw
In response to Fix accidentally cast away qualifiers  (Peter Eisentraut <peter@eisentraut.org>)
Responses Re: Fix accidentally cast away qualifiers
List 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

pgsql-hackers by date:

Previous
From: Akshay Joshi
Date:
Subject: Re: [PATCH] Add pg_get_database_ddl() function to reconstruct CREATE DATABASE statement
Next
From: Álvaro Herrera
Date:
Subject: Re: Add IS_INDEX macro to brin and gist index