Remove various unnecessary (char *) casts
authorPeter Eisentraut <peter@eisentraut.org>
Thu, 20 Feb 2025 18:49:27 +0000 (19:49 +0100)
committerPeter Eisentraut <peter@eisentraut.org>
Thu, 20 Feb 2025 18:49:27 +0000 (19:49 +0100)
Remove a number of (char *) casts that are unnecessary.  Or in some
cases, rewrite the code to make the purpose of the cast clearer.

Reviewed-by: Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
Discussion: https://www.postgresql.org/message-id/flat/fd1fcedb-3492-4fc8-9e3e-74b97f2db6c7%40eisentraut.org

18 files changed:
contrib/ltree/ltxtquery_io.c
contrib/pg_trgm/trgm_gist.c
contrib/postgres_fdw/postgres_fdw.c
src/backend/access/transam/xlog.c
src/backend/libpq/auth.c
src/backend/nodes/copyfuncs.c
src/backend/nodes/makefuncs.c
src/backend/storage/page/bufpage.c
src/backend/utils/cache/relmapper.c
src/backend/utils/error/jsonlog.c
src/bin/pg_basebackup/pg_receivewal.c
src/bin/pg_dump/compress_gzip.c
src/bin/pg_rewind/pg_rewind.c
src/bin/psql/describe.c
src/common/controldata_utils.c
src/interfaces/ecpg/ecpglib/descriptor.c
src/interfaces/ecpg/ecpglib/execute.c
src/interfaces/ecpg/pgtypeslib/numeric.c

index 7b8fba17ff2ab466cc28199a11dcce2da57cb30d..ec331607793a7b287357fadf8ac3eb5831b1c000 100644 (file)
@@ -410,7 +410,7 @@ ltxtq_in(PG_FUNCTION_ARGS)
 {
    ltxtquery  *res;
 
-   if ((res = queryin((char *) PG_GETARG_POINTER(0), fcinfo->context)) == NULL)
+   if ((res = queryin(PG_GETARG_POINTER(0), fcinfo->context)) == NULL)
        PG_RETURN_NULL();
    PG_RETURN_POINTER(res);
 }
index 14285a919ad80f9651a709c66e0572857b4d4e24..5ba895217b0a914adc9ae2162f6bf8dac3189a4e 100644 (file)
@@ -106,7 +106,7 @@ makesign(BITVECP sign, TRGM *a, int siglen)
    SETBIT(sign, SIGLENBIT(siglen));    /* set last unused bit */
    for (k = 0; k < len; k++)
    {
-       CPTRGM(((char *) &tmp), ptr + k);
+       CPTRGM(&tmp, ptr + k);
        HASH(sign, tmp, siglen);
    }
 }
@@ -186,7 +186,7 @@ cnt_sml_sign_common(TRGM *qtrg, BITVECP sign, int siglen)
 
    for (k = 0; k < len; k++)
    {
-       CPTRGM(((char *) &tmp), ptr + k);
+       CPTRGM(&tmp, ptr + k);
        count += GETBIT(sign, HASHVAL(tmp, siglen));
    }
 
@@ -373,7 +373,7 @@ gtrgm_consistent(PG_FUNCTION_ARGS)
                res = true;
                for (k = 0; k < len; k++)
                {
-                   CPTRGM(((char *) &tmp), ptr + k);
+                   CPTRGM(&tmp, ptr + k);
                    if (!GETBIT(sign, HASHVAL(tmp, siglen)))
                    {
                        res = false;
@@ -426,7 +426,7 @@ gtrgm_consistent(PG_FUNCTION_ARGS)
                    check = (bool *) palloc(len * sizeof(bool));
                    for (k = 0; k < len; k++)
                    {
-                       CPTRGM(((char *) &tmp), ptr + k);
+                       CPTRGM(&tmp, ptr + k);
                        check[k] = GETBIT(sign, HASHVAL(tmp, siglen));
                    }
                    res = trigramsMatchGraph(cache->graph, check);
@@ -552,7 +552,7 @@ unionkey(BITVECP sbase, TRGM *add, int siglen)
 
        for (i = 0; i < ARRNELEM(add); i++)
        {
-           CPTRGM(((char *) &tmp), ptr + i);
+           CPTRGM(&tmp, ptr + i);
            HASH(sbase, tmp, siglen);
        }
    }
index b92e2a0fc9f80c055ff972884a5ad62a8dd256e0..de43727a2a051ff4411b401242e3c0bbbb469dfc 100644 (file)
@@ -5660,13 +5660,13 @@ postgresImportForeignSchema(ImportForeignSchemaStmt *stmt, Oid serverOid)
                attname = PQgetvalue(res, i, 1);
                typename = PQgetvalue(res, i, 2);
                attnotnull = PQgetvalue(res, i, 3);
-               attdefault = PQgetisnull(res, i, 4) ? (char *) NULL :
+               attdefault = PQgetisnull(res, i, 4) ? NULL :
                    PQgetvalue(res, i, 4);
-               attgenerated = PQgetisnull(res, i, 5) ? (char *) NULL :
+               attgenerated = PQgetisnull(res, i, 5) ? NULL :
                    PQgetvalue(res, i, 5);
-               collname = PQgetisnull(res, i, 6) ? (char *) NULL :
+               collname = PQgetisnull(res, i, 6) ? NULL :
                    PQgetvalue(res, i, 6);
-               collnamespace = PQgetisnull(res, i, 7) ? (char *) NULL :
+               collnamespace = PQgetisnull(res, i, 7) ? NULL :
                    PQgetvalue(res, i, 7);
 
                if (first_item)
index f9bf5ba750910d42fff6166416bfb58a69d03587..d10704360a6c842998c109aa5f089c8374bf8dd1 100644 (file)
@@ -4287,7 +4287,7 @@ WriteControlFile(void)
    /* Contents are protected with a CRC */
    INIT_CRC32C(ControlFile->crc);
    COMP_CRC32C(ControlFile->crc,
-               (char *) ControlFile,
+               ControlFile,
                offsetof(ControlFileData, crc));
    FIN_CRC32C(ControlFile->crc);
 
@@ -4405,7 +4405,7 @@ ReadControlFile(void)
    /* Now check the CRC. */
    INIT_CRC32C(crc);
    COMP_CRC32C(crc,
-               (char *) ControlFile,
+               ControlFile,
                offsetof(ControlFileData, crc));
    FIN_CRC32C(crc);
 
index 0f65014e64fd126706b406762ca961c2af4634b4..81e2f8184e30de3688bf0d5bd9aca09cebf11bdd 100644 (file)
@@ -2932,8 +2932,8 @@ PerformRadiusTransaction(const char *server, const char *secret, const char *por
    radius_packet radius_recv_pack;
    radius_packet *packet = &radius_send_pack;
    radius_packet *receivepacket = &radius_recv_pack;
-   char       *radius_buffer = (char *) &radius_send_pack;
-   char       *receive_buffer = (char *) &radius_recv_pack;
+   void       *radius_buffer = &radius_send_pack;
+   void       *receive_buffer = &radius_recv_pack;
    int32       service = pg_hton32(RADIUS_AUTHENTICATE_ONLY);
    uint8      *cryptvector;
    int         encryptedpasswordlen;
@@ -3204,7 +3204,9 @@ PerformRadiusTransaction(const char *server, const char *secret, const char *por
                                                                         * original packet */
        if (packetlength > RADIUS_HEADER_LENGTH)    /* there may be no
                                                     * attributes at all */
-           memcpy(cryptvector + RADIUS_HEADER_LENGTH, receive_buffer + RADIUS_HEADER_LENGTH, packetlength - RADIUS_HEADER_LENGTH);
+           memcpy(cryptvector + RADIUS_HEADER_LENGTH,
+                  (char *) receive_buffer + RADIUS_HEADER_LENGTH,
+                  packetlength - RADIUS_HEADER_LENGTH);
        memcpy(cryptvector + packetlength, secret, strlen(secret));
 
        if (!pg_md5_binary(cryptvector,
index 45915767825ce4ccbeb0430e44feb65a35c83479..475693b08bc5abd4024ed8cc62b1a8432498e478 100644 (file)
@@ -40,7 +40,7 @@
 
 /* Copy a field that is a pointer to a C string, or perhaps NULL */
 #define COPY_STRING_FIELD(fldname) \
-   (newnode->fldname = from->fldname ? pstrdup(from->fldname) : (char *) NULL)
+   (newnode->fldname = from->fldname ? pstrdup(from->fldname) : NULL)
 
 /* Copy a field that is an inline array */
 #define COPY_ARRAY_FIELD(fldname) \
index 007612563ca3a98c5f46b47f23e83668c9f0b51d..dbbc2f1e30d1b1bef0794a3de0b59583c06d1523 100644 (file)
@@ -51,7 +51,7 @@ makeSimpleA_Expr(A_Expr_Kind kind, char *name,
    A_Expr     *a = makeNode(A_Expr);
 
    a->kind = kind;
-   a->name = list_make1(makeString((char *) name));
+   a->name = list_make1(makeString(name));
    a->lexpr = lexpr;
    a->rexpr = rexpr;
    a->location = location;
index 91da73dda8b7f5243069092c5c3428c7819d2f66..ecc81aacfc3e400c549f8440a666093a53a8a2ea 100644 (file)
@@ -1502,7 +1502,7 @@ PageSetChecksumCopy(Page page, BlockNumber blkno)
 
    /* If we don't need a checksum, just return the passed-in data */
    if (PageIsNew(page) || !DataChecksumsEnabled())
-       return (char *) page;
+       return page;
 
    /*
     * We allocate the copy space once and use it over on each subsequent
index 7dcbf1d5c5cfede4c63efe7686bdbb332daf4902..abf89f0776e9946acdc00c18fcc3bbce6420318f 100644 (file)
@@ -854,7 +854,7 @@ read_relmap_file(RelMapFile *map, char *dbpath, bool lock_held, int elevel)
 
    /* verify the CRC */
    INIT_CRC32C(crc);
-   COMP_CRC32C(crc, (char *) map, offsetof(RelMapFile, crc));
+   COMP_CRC32C(crc, map, offsetof(RelMapFile, crc));
    FIN_CRC32C(crc);
 
    if (!EQ_CRC32C(crc, map->crc))
@@ -910,7 +910,7 @@ write_relmap_file(RelMapFile *newmap, bool write_wal, bool send_sinval,
        elog(ERROR, "attempt to write bogus relation mapping");
 
    INIT_CRC32C(newmap->crc);
-   COMP_CRC32C(newmap->crc, (char *) newmap, offsetof(RelMapFile, crc));
+   COMP_CRC32C(newmap->crc, newmap, offsetof(RelMapFile, crc));
    FIN_CRC32C(newmap->crc);
 
    /*
index 6533f1d6888da55fa5bc7da628a3dc7065ac5452..549e0f48ee82b82b4abc789167c18088a3820342 100644 (file)
@@ -206,7 +206,7 @@ write_jsonlog(ErrorData *edata)
    /* Error severity */
    if (edata->elevel)
        appendJSONKeyValue(&buf, "error_severity",
-                          (char *) error_severity(edata->elevel), true);
+                          error_severity(edata->elevel), true);
 
    /* SQL state code */
    if (edata->sqlerrcode)
index 176c40facad4120d1651b5211f8fed46878afa1f..de3584018b097152d11173daebd11b7b46a44dc4 100644 (file)
@@ -342,7 +342,7 @@ FindStreamingStart(uint32 *tli)
            if (lseek(fd, (off_t) (-4), SEEK_END) < 0)
                pg_fatal("could not seek in compressed file \"%s\": %m",
                         fullpath);
-           r = read(fd, (char *) buf, sizeof(buf));
+           r = read(fd, buf, sizeof(buf));
            if (r != sizeof(buf))
            {
                if (r < 0)
index f9e5f00aaa1ffb1ad8940fb460acbb21cde97852..23f617209e65e4bf2ccc0fbd5ac9f4ae5855931a 100644 (file)
@@ -129,7 +129,7 @@ DeflateCompressorCommon(ArchiveHandle *AH, CompressorState *cs, bool flush)
                 */
                size_t      len = gzipcs->outsize - zp->avail_out;
 
-               cs->writeF(AH, (char *) out, len);
+               cs->writeF(AH, out, len);
            }
            zp->next_out = out;
            zp->avail_out = gzipcs->outsize;
index cae81cd6cb1b3493a3f7ce97c3ad7b77e39aebbe..2ce99d06d1d96f6fcb3ce04748876f4fdeaaa65a 100644 (file)
@@ -1007,7 +1007,7 @@ checkControlFile(ControlFileData *ControlFile)
 
    /* Calculate CRC */
    INIT_CRC32C(crc);
-   COMP_CRC32C(crc, (char *) ControlFile, offsetof(ControlFileData, crc));
+   COMP_CRC32C(crc, ControlFile, offsetof(ControlFileData, crc));
    FIN_CRC32C(crc);
 
    /* And simply compare it */
index 3b7ba66fad05f9b2b7f1c7fb9d5b90558c6341ee..e6cf468ac9e9e50f76af81fe81e176fe797d221b 100644 (file)
@@ -1741,7 +1741,7 @@ describeOneTableDetails(const char *schemaname,
        *(PQgetvalue(res, 0, 13)) : 'd';
    if (pset.sversion >= 120000)
        tableinfo.relam = PQgetisnull(res, 0, 14) ?
-           (char *) NULL : pg_strdup(PQgetvalue(res, 0, 14));
+           NULL : pg_strdup(PQgetvalue(res, 0, 14));
    else
        tableinfo.relam = NULL;
    PQclear(res);
index 27ce27d76479e34364069cdd44be24503b1fbaf4..34d8a3a4e310bb009d9bb337b592c34278cdd4fd 100644 (file)
@@ -135,7 +135,7 @@ retry:
    /* Check the CRC. */
    INIT_CRC32C(crc);
    COMP_CRC32C(crc,
-               (char *) ControlFile,
+               ControlFile,
                offsetof(ControlFileData, crc));
    FIN_CRC32C(crc);
 
@@ -199,7 +199,7 @@ update_controlfile(const char *DataDir,
    /* Recalculate CRC of control file */
    INIT_CRC32C(ControlFile->crc);
    COMP_CRC32C(ControlFile->crc,
-               (char *) ControlFile,
+               ControlFile,
                offsetof(ControlFileData, crc));
    FIN_CRC32C(ControlFile->crc);
 
index 8525a6812f2ee163f4b5f1f86df174ea24144806..651d5c8b2ed3cc4c761da3b9b770d40188308acf 100644 (file)
@@ -198,7 +198,7 @@ get_char_item(int lineno, void *var, enum ECPGttype vartype, char *value, int va
        case ECPGt_char:
        case ECPGt_unsigned_char:
        case ECPGt_string:
-           strncpy((char *) var, value, varcharsize);
+           strncpy(var, value, varcharsize);
            break;
        case ECPGt_varchar:
            {
@@ -597,7 +597,7 @@ set_desc_attr(struct descriptor_item *desc_item, struct variable *var,
    }
 
    ecpg_free(desc_item->data); /* free() takes care of a potential NULL value */
-   desc_item->data = (char *) tobeinserted;
+   desc_item->data = tobeinserted;
 }
 
 
index b5089eac787c2ab29ca94b9fc8cb90ac6006696b..f52da06de9a1d4f4bc2052d6c3b3c2b686e21461 100644 (file)
@@ -278,7 +278,7 @@ ecpg_is_type_an_array(int type, const struct statement *stmt, const struct varia
            isarray = ECPG_ARRAY_NONE;
        else
        {
-           isarray = (atoi((char *) PQgetvalue(query, 0, 0)) == -1) ? ECPG_ARRAY_ARRAY : ECPG_ARRAY_VECTOR;
+           isarray = (atoi(PQgetvalue(query, 0, 0)) == -1) ? ECPG_ARRAY_ARRAY : ECPG_ARRAY_VECTOR;
            if (ecpg_dynamic_type(type) == SQL3_CHARACTER ||
                ecpg_dynamic_type(type) == SQL3_CHARACTER_VARYING)
            {
index 35e7b92da405ed12b51badb45056f43a665e906f..bb2a86981caa68f90d0019cd37648ea551761851 100644 (file)
@@ -232,7 +232,7 @@ get_str_from_var(numeric *var, int dscale)
 
    if (var->sign == NUMERIC_NAN)
    {
-       str = (char *) pgtypes_alloc(4);
+       str = pgtypes_alloc(4);
        if (str == NULL)
            return NULL;
        sprintf(str, "NaN");
@@ -269,7 +269,7 @@ get_str_from_var(numeric *var, int dscale)
    /*
     * Allocate space for the result
     */
-   if ((str = (char *) pgtypes_alloc(Max(0, dscale) + Max(0, var->weight) + 4)) == NULL)
+   if ((str = pgtypes_alloc(Max(0, dscale) + Max(0, var->weight) + 4)) == NULL)
        return NULL;
    cp = str;