Fix casting away const in _bt_reorder_array_cmp

Started by Chao Li12 days ago2 messages
#1Chao Li
li.evan.chao@gmail.com
1 attachment(s)

Hi Hackers,

There have been several recent efforts to eliminate cast-away-const usages.
While reviewing another patch, I happened to notice one such occurrence, so
I’m posting a trivial patch to fix it.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachments:

v1-0001-Fix-casting-away-const-in-_bt_reorder_array_cmp.patchapplication/octet-stream; name=v1-0001-Fix-casting-away-const-in-_bt_reorder_array_cmp.patchDownload
From a3dd9618e551f9a9c1ae888e987183e15011c87d Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <lic@highgo.com>
Date: Wed, 31 Dec 2025 15:45:51 +0800
Subject: [PATCH v1] Fix casting away const in _bt_reorder_array_cmp.

Author: Chao Li <lic@highgo.com>
---
 src/backend/access/nbtree/nbtpreprocesskeys.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/backend/access/nbtree/nbtpreprocesskeys.c b/src/backend/access/nbtree/nbtpreprocesskeys.c
index 5b251c5058d..a45641b6e97 100644
--- a/src/backend/access/nbtree/nbtpreprocesskeys.c
+++ b/src/backend/access/nbtree/nbtpreprocesskeys.c
@@ -1792,8 +1792,8 @@ _bt_unmark_keys(IndexScanDesc scan, int *keyDataMap)
 static int
 _bt_reorder_array_cmp(const void *a, const void *b)
 {
-	BTArrayKeyInfo *arraya = (BTArrayKeyInfo *) a;
-	BTArrayKeyInfo *arrayb = (BTArrayKeyInfo *) b;
+	const BTArrayKeyInfo *arraya = a;
+	const BTArrayKeyInfo *arrayb = b;
 
 	return pg_cmp_s32(arraya->scan_key, arrayb->scan_key);
 }
-- 
2.39.5 (Apple Git-154)

#2Bertrand Drouvot
bertranddrouvot.pg@gmail.com
In reply to: Chao Li (#1)
Re: Fix casting away const in _bt_reorder_array_cmp

Hi,

On Wed, Dec 31, 2025 at 03:51:55PM +0800, Chao Li wrote:

Hi Hackers,

There have been several recent efforts to eliminate cast-away-const usages.
While reviewing another patch, I happened to notice one such occurrence, so
I’m posting a trivial patch to fix it.

Thanks for the patch!

This one is one that I choose to discard in [1]/messages/by-id/aUQHy/MmWq7c97wK@ip-10-97-1-34.eu-west-3.compute.internal because this is just a thin
wrapper. Not saying that keeping cast away const was a good thing but the idea
was also to reduce the number of changes in the patch.

The criteria was:

"
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"

That said, also removing explicit casts when assigning from void pointers
(relying on implicit conversion instead) is also valuable and your patch does
exactly this.

That could be part of a larger project though (as mentioned in [2]/messages/by-id/aVTiCHBalaFCneYD@ip-10-97-1-34.eu-west-3.compute.internal).

Thoughts?

[1]: /messages/by-id/aUQHy/MmWq7c97wK@ip-10-97-1-34.eu-west-3.compute.internal
[2]: /messages/by-id/aVTiCHBalaFCneYD@ip-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