| From: | Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com> |
|---|---|
| To: | Bertrand Drouvot <bertranddrouvot(dot)pg(at)gmail(dot)com> |
| Cc: | Peter Eisentraut <peter(at)eisentraut(dot)org>, pgsql-hackers(at)lists(dot)postgresql(dot)org |
| Subject: | Re: Don't cast away const where possible |
| Date: | 2025-12-29 09:28:06 |
| Message-ID: | 5D9A5B79-CA5B-4AAF-8602-A709C37D171A@gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
> On Dec 29, 2025, at 17:01, Bertrand Drouvot <bertranddrouvot(dot)pg(at)gmail(dot)com> wrote:
>
> Hi,
>
> On Mon, Dec 22, 2025 at 12:53:03PM +0100, Peter Eisentraut wrote:
>> On 18.12.25 14:55, Bertrand Drouvot wrote:
>>> Some functions are casting away the const qualifiers from their signatures in
>>> local variables.
>>
>> @@ -1304,8 +1304,8 @@ merge_overlapping_ranges(FmgrInfo *cmp, Oid colloid,
>> static int
>> compare_distances(const void *a, const void *b)
>> {
>> - DistanceValue *da = (DistanceValue *) a;
>> - DistanceValue *db = (DistanceValue *) b;
>> + const DistanceValue *da = (const DistanceValue *) a;
>> + const DistanceValue *db = (const DistanceValue *) b;
>>
>> I wonder if the better fix here wouldn't be to get rid of the cast. It's not
>> necessary, and without it the compiler would automatically warn about
>> qualifier mismatches.
>
> Yeah, that looks better as it provides an extra safety check should the function
> signature change.
>
>> These comparison functions seem to be a common
>> pattern.
>
> Right, in the attached I applied your proposal on all those places.
>
> Regards,
>
> --
> Bertrand Drouvot
> PostgreSQL Contributors Team
> RDS Open Source Databases
> Amazon Web Services: https://aws.amazon.com
> <v2-0001-Don-t-cast-away-const-where-possible.patch><v2-0002-Add-const-to-read-only-TableInfo-pointers-in-pg_d.patch><v2-0003-Separate-read-and-write-pointers-in-pg_saslprep.patch>
I have similar patch at https://docs.qq.com/sheet/DR0JRQ3lPVGtCWW5q?tab=000001&_t=1761030496005&nlc=1 doing the exact same thing in pg_dump_sort.c.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Chao Li | 2025-12-29 09:44:22 | Re: Refactor to eliminate cast-away-const in pg_dump object sort comparator |
| Previous Message | Chao Li | 2025-12-29 09:21:26 | lsyscache: free IndexAmRoutine objects returned by GetIndexAmRoutineByAmId() |