Don't cast away const where possible

From: Bertrand Drouvot <bertranddrouvot(dot)pg(at)gmail(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Don't cast away const where possible
Date: 2025-12-18 13:55:23
Message-ID: aUQHy/MmWq7c97wK@ip-10-97-1-34.eu-west-3.compute.internal
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi hackers,

Some functions are casting away the const qualifiers from their signatures in
local variables.

These 3 patches add const to read only local variables, preserving the const
qualifiers from the function signatures.

0001: those are simple changes in 6 files (16 changes in total)

0002: Add const to read only TableInfo pointers in pg_dump

Functions that dump table data receive their parameters through const void *
but were casting away const. Add const qualifiers to functions that only read
the table information.

Also change getRootTableInfo to return const TableInfo *, since it only traverses
the parent chain without modifying any TableInfo structures. This allows the dump
functions to maintain const correctness when calling it.

0003: Separate read and write pointers in pg_saslprep

Use separate pointers for reading const input ('p') and writing
to mutable output ('outp'), avoiding the need to cast away const on the input
parameter.

It has been done with the help of [1], but not all the changes proposed by it have
been implemented. Indeed, I did some filtering and decided not to change the ones
that:

- are just thin wrappers
- would require public API changes
- rely on external functions (such as LZ4F_compressUpdate())
- would require changes beyond the scope of this cleanup

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

Thoughts?

Regards,

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

Attachment Content-Type Size
v1-0001-Don-t-cast-away-const-where-possible.patch text/x-diff 6.1 KB
v1-0002-Add-const-to-read-only-TableInfo-pointers-in-pg_d.patch text/x-diff 3.5 KB
v1-0003-Separate-read-and-write-pointers-in-pg_saslprep.patch text/x-diff 1.7 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Konstantin Knizhnik 2025-12-18 13:57:40 Re: index prefetching
Previous Message Robert Haas 2025-12-18 13:36:44 Re: pg_plan_advice