Avoid casting void * function arguments
authorPeter Eisentraut <peter@eisentraut.org>
Mon, 12 Jan 2026 15:12:56 +0000 (16:12 +0100)
committerPeter Eisentraut <peter@eisentraut.org>
Mon, 12 Jan 2026 15:12:56 +0000 (16:12 +0100)
In many cases, the cast would silently drop a const qualifier.  To
fix, drop the unnecessary cast and let the compiler check the types
and qualifiers.  Add const to read-only local variables, preserving
the const qualifiers from the function signatures.

Co-authored-by: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Co-authored-by: Peter Eisentraut <peter@eisentraut.org>
Discussion: https://postgr.es/m/aUQHy/MmWq7c97wK%40ip-10-97-1-34.eu-west-3.compute.internal

16 files changed:
src/backend/access/brin/brin_minmax_multi.c
src/backend/access/common/heaptuple.c
src/backend/access/heap/pruneheap.c
src/backend/access/nbtree/nbtpage.c
src/backend/access/nbtree/nbtpreprocesskeys.c
src/backend/access/nbtree/nbtsplitloc.c
src/backend/access/spgist/spgkdtreeproc.c
src/backend/statistics/extended_stats.c
src/backend/statistics/mcv.c
src/backend/tsearch/spell.c
src/backend/utils/adt/rangetypes_gist.c
src/backend/utils/adt/rangetypes_spgist.c
src/backend/utils/adt/rangetypes_typanalyze.c
src/backend/utils/cache/typcache.c
src/bin/pg_dump/pg_dump.c
src/test/modules/injection_points/injection_points.c

index 6b86b1fd889fd254c5d1e5b56d1b6776bcbd9d6d..75dd8d5083bf96af6d14af5b4b682464a285156f 100644 (file)
@@ -857,8 +857,8 @@ brin_range_deserialize(int maxvalues, SerializedRanges *serialized)
 static int
 compare_expanded_ranges(const void *a, const void *b, void *arg)
 {
-       ExpandedRange *ra = (ExpandedRange *) a;
-       ExpandedRange *rb = (ExpandedRange *) b;
+       const ExpandedRange *ra = a;
+       const ExpandedRange *rb = b;
        Datum           r;
 
        compare_context *cxt = (compare_context *) arg;
@@ -895,8 +895,8 @@ compare_expanded_ranges(const void *a, const void *b, void *arg)
 static int
 compare_values(const void *a, const void *b, void *arg)
 {
-       Datum      *da = (Datum *) a;
-       Datum      *db = (Datum *) b;
+       const Datum *da = a;
+       const Datum *db = b;
        Datum           r;
 
        compare_context *cxt = (compare_context *) arg;
@@ -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 = a;
+       const DistanceValue *db = b;
 
        if (da->value < db->value)
                return 1;
index 61367b3235a529b2fdd8a7f1e3d052ed8e8f2ecb..11bec20e82e490b5cea245fdd95dc31a056a8109 100644 (file)
@@ -103,7 +103,7 @@ static HTAB *missing_cache = NULL;
 static uint32
 missing_hash(const void *key, Size keysize)
 {
-       const missing_cache_key *entry = (missing_cache_key *) key;
+       const missing_cache_key *entry = key;
 
        return hash_bytes((const unsigned char *) DatumGetPointer(entry->value), entry->len);
 }
@@ -111,8 +111,8 @@ missing_hash(const void *key, Size keysize)
 static int
 missing_match(const void *key1, const void *key2, Size keysize)
 {
-       const missing_cache_key *entry1 = (missing_cache_key *) key1;
-       const missing_cache_key *entry2 = (missing_cache_key *) key2;
+       const missing_cache_key *entry1 = key1;
+       const missing_cache_key *entry2 = key2;
 
        if (entry1->len != entry2->len)
                return entry1->len > entry2->len ? 1 : -1;
index af788b29714c3d69384f23d9941f539e551b2d58..632c24279528d879115e2ac34fd753c826f9948f 100644 (file)
@@ -2021,8 +2021,8 @@ heap_log_freeze_eq(xlhp_freeze_plan *plan, HeapTupleFreeze *frz)
 static int
 heap_log_freeze_cmp(const void *arg1, const void *arg2)
 {
-       HeapTupleFreeze *frz1 = (HeapTupleFreeze *) arg1;
-       HeapTupleFreeze *frz2 = (HeapTupleFreeze *) arg2;
+       const HeapTupleFreeze *frz1 = arg1;
+       const HeapTupleFreeze *frz2 = arg2;
 
        if (frz1->xmax < frz2->xmax)
                return -1;
index 2ff0085b96f362d6af7805dbe636852605a74c6e..4125c185e8bee66af7589e585c9b029073f4513d 100644 (file)
@@ -1462,8 +1462,8 @@ _bt_delitems_update(BTVacuumPosting *updatable, int nupdatable,
 static int
 _bt_delitems_cmp(const void *a, const void *b)
 {
-       TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
-       TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
+       const TM_IndexDelete *indexdelete1 = a;
+       const TM_IndexDelete *indexdelete2 = b;
 
        Assert(indexdelete1->id != indexdelete2->id);
 
index 5235ef9a8f544764719ade1e790db26861b66afc..b028b0c3e887f213a0ddd29a583877cbf5370d39 100644 (file)
@@ -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);
 }
index 523abd64575e2aabc2d3dd891ab60fbb5712e7e3..de9eca3c8b2eacee350bab42908e49cbbde4c20f 100644 (file)
@@ -594,8 +594,8 @@ _bt_deltasortsplits(FindSplitData *state, double fillfactormult,
 static int
 _bt_splitcmp(const void *arg1, const void *arg2)
 {
-       SplitPoint *split1 = (SplitPoint *) arg1;
-       SplitPoint *split2 = (SplitPoint *) arg2;
+       const SplitPoint *split1 = arg1;
+       const SplitPoint *split2 = arg2;
 
        return pg_cmp_s16(split1->curdelta, split2->curdelta);
 }
index 6acda3fdcffaf9f018a6206a0c0e2d0a49312ab6..1ec0a4f59f32bd6e8ce9d46b9949161a8e10d888 100644 (file)
@@ -84,8 +84,8 @@ typedef struct SortedPoint
 static int
 x_cmp(const void *a, const void *b)
 {
-       SortedPoint *pa = (SortedPoint *) a;
-       SortedPoint *pb = (SortedPoint *) b;
+       const SortedPoint *pa = a;
+       const SortedPoint *pb = b;
 
        if (pa->p->x == pb->p->x)
                return 0;
@@ -95,8 +95,8 @@ x_cmp(const void *a, const void *b)
 static int
 y_cmp(const void *a, const void *b)
 {
-       SortedPoint *pa = (SortedPoint *) a;
-       SortedPoint *pb = (SortedPoint *) b;
+       const SortedPoint *pa = a;
+       const SortedPoint *pb = b;
 
        if (pa->p->y == pb->p->y)
                return 0;
index c14d8d21bf060372eecc882617e3ec8edbe20dc2..b9a8f257042267d0503730f5186396ba8fc913c4 100644 (file)
@@ -862,8 +862,8 @@ int
 multi_sort_compare(const void *a, const void *b, void *arg)
 {
        MultiSortSupport mss = (MultiSortSupport) arg;
-       SortItem   *ia = (SortItem *) a;
-       SortItem   *ib = (SortItem *) b;
+       const SortItem *ia = a;
+       const SortItem *ib = b;
        int                     i;
 
        for (i = 0; i < mss->ndims; i++)
index 390ad83497aae0a2236790c665ae4ced4d64b116..9749871b18eeaabc76d2f1988ee838ed13f948f4 100644 (file)
@@ -402,8 +402,8 @@ count_distinct_groups(int numrows, SortItem *items, MultiSortSupport mss)
 static int
 compare_sort_item_count(const void *a, const void *b, void *arg)
 {
-       SortItem   *ia = (SortItem *) a;
-       SortItem   *ib = (SortItem *) b;
+       const SortItem *ia = a;
+       const SortItem *ib = b;
 
        if (ia->count == ib->count)
                return 0;
@@ -465,8 +465,8 @@ static int
 sort_item_compare(const void *a, const void *b, void *arg)
 {
        SortSupport ssup = (SortSupport) arg;
-       SortItem   *ia = (SortItem *) a;
-       SortItem   *ib = (SortItem *) b;
+       const SortItem *ia = a;
+       const SortItem *ib = b;
 
        return ApplySortComparator(ia->values[0], ia->isnull[0],
                                                           ib->values[0], ib->isnull[0],
index 2e96c86b0b3d3e936dc55ae5d252b15be85e87f4..e3436dbddd2dc5e0993bb8152f3e719a7982b4f8 100644 (file)
@@ -210,8 +210,8 @@ cmpspellaffix(const void *s1, const void *s2)
 static int
 cmpcmdflag(const void *f1, const void *f2)
 {
-       CompoundAffixFlag *fv1 = (CompoundAffixFlag *) f1,
-                          *fv2 = (CompoundAffixFlag *) f2;
+       const CompoundAffixFlag *fv1 = f1;
+       const CompoundAffixFlag *fv2 = f2;
 
        Assert(fv1->flagMode == fv2->flagMode);
 
index 53470cd50067f337431c490ce32e9b773e7b158e..cb9f769cdb21bc818c1ea43fc32ee2ddc9b5694b 100644 (file)
@@ -1729,9 +1729,9 @@ get_gist_range_class(RangeType *range)
 static int
 single_bound_cmp(const void *a, const void *b, void *arg)
 {
-       SingleBoundSortItem *i1 = (SingleBoundSortItem *) a;
-       SingleBoundSortItem *i2 = (SingleBoundSortItem *) b;
-       TypeCacheEntry *typcache = (TypeCacheEntry *) arg;
+       const SingleBoundSortItem *i1 = a;
+       const SingleBoundSortItem *i2 = b;
+       TypeCacheEntry *typcache = arg;
 
        return range_cmp_bounds(typcache, &i1->bound, &i2->bound);
 }
@@ -1742,9 +1742,9 @@ single_bound_cmp(const void *a, const void *b, void *arg)
 static int
 interval_cmp_lower(const void *a, const void *b, void *arg)
 {
-       NonEmptyRange *i1 = (NonEmptyRange *) a;
-       NonEmptyRange *i2 = (NonEmptyRange *) b;
-       TypeCacheEntry *typcache = (TypeCacheEntry *) arg;
+       const NonEmptyRange *i1 = a;
+       const NonEmptyRange *i2 = b;
+       TypeCacheEntry *typcache = arg;
 
        return range_cmp_bounds(typcache, &i1->lower, &i2->lower);
 }
@@ -1755,9 +1755,9 @@ interval_cmp_lower(const void *a, const void *b, void *arg)
 static int
 interval_cmp_upper(const void *a, const void *b, void *arg)
 {
-       NonEmptyRange *i1 = (NonEmptyRange *) a;
-       NonEmptyRange *i2 = (NonEmptyRange *) b;
-       TypeCacheEntry *typcache = (TypeCacheEntry *) arg;
+       const NonEmptyRange *i1 = a;
+       const NonEmptyRange *i2 = b;
+       TypeCacheEntry *typcache = arg;
 
        return range_cmp_bounds(typcache, &i1->upper, &i2->upper);
 }
index f9db6869d0a683da68fa4c60bb01ed4774c8f6bc..b25b468b83dd27e0256d60f6dda329a74f5c7ea1 100644 (file)
@@ -185,9 +185,9 @@ spg_range_quad_choose(PG_FUNCTION_ARGS)
 static int
 bound_cmp(const void *a, const void *b, void *arg)
 {
-       RangeBound *ba = (RangeBound *) a;
-       RangeBound *bb = (RangeBound *) b;
-       TypeCacheEntry *typcache = (TypeCacheEntry *) arg;
+       const RangeBound *ba = a;
+       const RangeBound *bb = b;
+       TypeCacheEntry *typcache = arg;
 
        return range_cmp_bounds(typcache, ba, bb);
 }
index 01f55b12ed5fb183bfadd61d411e36324d90b527..38d12dedbc5c5c1c7a2e2f1278281d05f609f027 100644 (file)
@@ -111,9 +111,9 @@ float8_qsort_cmp(const void *a1, const void *a2, void *arg)
 static int
 range_bound_qsort_cmp(const void *a1, const void *a2, void *arg)
 {
-       RangeBound *b1 = (RangeBound *) a1;
-       RangeBound *b2 = (RangeBound *) a2;
-       TypeCacheEntry *typcache = (TypeCacheEntry *) arg;
+       const RangeBound *b1 = a1;
+       const RangeBound *b2 = a2;
+       TypeCacheEntry *typcache = arg;
 
        return range_cmp_bounds(typcache, b1, b2);
 }
index 96bbd64de2c8da4d3a1a60cfa04cd5682252fac4..dc4b1a56414cbb1732578e12e9a0e75751e68519 100644 (file)
@@ -235,8 +235,8 @@ shared_record_table_compare(const void *a, const void *b, size_t size,
                                                        void *arg)
 {
        dsa_area   *area = (dsa_area *) arg;
-       SharedRecordTableKey *k1 = (SharedRecordTableKey *) a;
-       SharedRecordTableKey *k2 = (SharedRecordTableKey *) b;
+       const SharedRecordTableKey *k1 = a;
+       const SharedRecordTableKey *k2 = b;
        TupleDesc       t1;
        TupleDesc       t2;
 
@@ -259,8 +259,8 @@ shared_record_table_compare(const void *a, const void *b, size_t size,
 static uint32
 shared_record_table_hash(const void *a, size_t size, void *arg)
 {
-       dsa_area   *area = (dsa_area *) arg;
-       SharedRecordTableKey *k = (SharedRecordTableKey *) a;
+       dsa_area   *area = arg;
+       const SharedRecordTableKey *k = a;
        TupleDesc       t;
 
        if (k->shared)
@@ -2013,7 +2013,7 @@ lookup_rowtype_tupdesc_domain(Oid type_id, int32 typmod, bool noError)
 static uint32
 record_type_typmod_hash(const void *data, size_t size)
 {
-       RecordCacheEntry *entry = (RecordCacheEntry *) data;
+       const RecordCacheEntry *entry = data;
 
        return hashRowType(entry->tupdesc);
 }
@@ -2024,8 +2024,8 @@ record_type_typmod_hash(const void *data, size_t size)
 static int
 record_type_typmod_compare(const void *a, const void *b, size_t size)
 {
-       RecordCacheEntry *left = (RecordCacheEntry *) a;
-       RecordCacheEntry *right = (RecordCacheEntry *) b;
+       const RecordCacheEntry *left = a;
+       const RecordCacheEntry *right = b;
 
        return equalRowTypes(left->tupdesc, right->tupdesc) ? 0 : 1;
 }
index d8a6b8ab0d5dd95c40985a300ee24f155ede8c5b..687dc98e46db1ce19051547c26e0cc903c5fa455 100644 (file)
@@ -11102,7 +11102,7 @@ fetchAttributeStats(Archive *fout)
 static char *
 dumpRelationStats_dumper(Archive *fout, const void *userArg, const TocEntry *te)
 {
-       const RelStatsInfo *rsinfo = (RelStatsInfo *) userArg;
+       const RelStatsInfo *rsinfo = userArg;
        static PGresult *res;
        static int      rownum;
        PQExpBuffer query;
index 9c3806f5958fa157b16d6f34ee077f8acd9cbffb..3de0491e0ec4e5a7859f33437c196e52d51d3c64 100644 (file)
@@ -189,7 +189,7 @@ injection_init_shmem(void)
  * otherwise.
  */
 static bool
-injection_point_allowed(InjectionPointCondition *condition)
+injection_point_allowed(const InjectionPointCondition *condition)
 {
        bool            result = true;
 
@@ -232,8 +232,8 @@ injection_points_cleanup(int code, Datum arg)
 void
 injection_error(const char *name, const void *private_data, void *arg)
 {
-       InjectionPointCondition *condition = (InjectionPointCondition *) private_data;
-       char       *argstr = (char *) arg;
+       const InjectionPointCondition *condition = private_data;
+       char       *argstr = arg;
 
        if (!injection_point_allowed(condition))
                return;
@@ -248,8 +248,8 @@ injection_error(const char *name, const void *private_data, void *arg)
 void
 injection_notice(const char *name, const void *private_data, void *arg)
 {
-       InjectionPointCondition *condition = (InjectionPointCondition *) private_data;
-       char       *argstr = (char *) arg;
+       const InjectionPointCondition *condition = private_data;
+       char       *argstr = arg;
 
        if (!injection_point_allowed(condition))
                return;
@@ -268,7 +268,7 @@ injection_wait(const char *name, const void *private_data, void *arg)
        uint32          old_wait_counts = 0;
        int                     index = -1;
        uint32          injection_wait_event = 0;
-       InjectionPointCondition *condition = (InjectionPointCondition *) private_data;
+       const InjectionPointCondition *condition = private_data;
 
        if (inj_state == NULL)
                injection_init_shmem();