Skip to content

Commit

Permalink
misc: Fixed the cast qualification warnings and enabled -Wcast-qual
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonmux committed Oct 18, 2024
1 parent 567cb6b commit f3f4447
Show file tree
Hide file tree
Showing 16 changed files with 54 additions and 26 deletions.
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ extended_warnings = [
'-Wbad-function-cast',
# '-Wcast-align=strict',
'-Wcast-function-type',
# '-Wcast-qual',
'-Wcast-qual',
# '-Wconversion',
'-Wdangling-else',
'-Wdouble-promotion',
Expand Down
9 changes: 7 additions & 2 deletions src/gdb_main.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/*
* This file is part of the Black Magic Debug project.
*
* Copyright (C) 2011 Black Sphere Technologies Ltd.
* Copyright (C) 2011 Black Sphere Technologies Ltd.
* Written by Gareth McMullin <[email protected]>
* Copyright (C) 2022-2024 1BitSquared <[email protected]>
* Modified by Rachel Mant <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -488,7 +490,10 @@ static void exec_q_feature_read(const char *packet, const size_t length)
}
const char *const description = target_regs_description(target);
handle_q_string_reply(description ? description : "", packet);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
free((void *)description);
#pragma GCC diagnostic pop
}

static void exec_q_crc(const char *packet, const size_t length)
Expand Down Expand Up @@ -789,7 +794,7 @@ static void exec_v_flash_write(const char *packet, const size_t length)
/* Write Flash Memory */
const uint32_t count = length - (size_t)(rest - packet);
DEBUG_GDB("Flash Write %08" PRIX32 " %08" PRIX32 "\n", addr, count);
if (cur_target && target_flash_write(cur_target, addr, (uint8_t *)rest, count))
if (cur_target && target_flash_write(cur_target, addr, (const uint8_t *)rest, count))
gdb_putpacketz("OK");
else {
target_flash_complete(cur_target);
Expand Down
2 changes: 1 addition & 1 deletion src/platforms/hosted/gdb_if.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ int gdb_if_init(void)
DEBUG_WARN("Listening on IPv6 only.\n");
}

if (bind(gdb_if_serv, (sockaddr_s *)&addr, family_to_size(addr.ss_family)) == -1) {
if (bind(gdb_if_serv, (const sockaddr_s *)&addr, family_to_size(addr.ss_family)) == -1) {
handle_error(gdb_if_serv, "binding socket");
continue;
}
Expand Down
9 changes: 6 additions & 3 deletions src/platforms/hosted/probe_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,26 @@ size_t probe_info_count(const probe_info_s *const list)
return probes;
}

void probe_info_free(probe_info_s *const probe_info)
void probe_info_free(const probe_info_s *const probe_info)
{
#if HOSTED_BMP_ONLY == 0
if (probe_info->device)
libusb_unref_device(probe_info->device);
#endif
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
free((void *)probe_info->manufacturer);
free((void *)probe_info->product);
free((void *)probe_info->serial);
free((void *)probe_info->version);
free(probe_info);
free((void *)probe_info);
#pragma GCC diagnostic pop
}

void probe_info_list_free(const probe_info_s *list)
{
while (list) {
probe_info_s *probe_info = (probe_info_s *)list;
const probe_info_s *const probe_info = (const probe_info_s *)list;
list = probe_info->next;
probe_info_free(probe_info);
}
Expand Down
6 changes: 3 additions & 3 deletions src/remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,12 @@ static void remote_packet_process_general(char *packet, const size_t packet_len)
remote_respond(REMOTE_RESP_OK, platform_nrst_get_val());
break;
case REMOTE_FREQ_SET:
platform_max_frequency_set(hex_string_to_num(8, packet + 2));
platform_max_frequency_set(hex_string_to_num(8U, packet + 2U));
remote_respond(REMOTE_RESP_OK, 0);
break;
case REMOTE_FREQ_GET: {
const uint32_t freq = platform_max_frequency_get();
remote_respond_buf(REMOTE_RESP_OK, (uint8_t *)&freq, 4);
remote_respond_buf(REMOTE_RESP_OK, (const uint8_t *)&freq, 4U);
break;
}
case REMOTE_PWR_SET:
Expand All @@ -288,7 +288,7 @@ static void remote_packet_process_general(char *packet, const size_t packet_len)
remote_respond(REMOTE_RESP_ERR, 0);
} else {
const bool result = platform_target_set_power(packet[2] == '1');
remote_respond(result ? REMOTE_RESP_OK : REMOTE_RESP_ERR, 0);
remote_respond(result ? REMOTE_RESP_OK : REMOTE_RESP_ERR, 0U);
}
#else
remote_respond(REMOTE_RESP_NOTSUP, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/target/adiv5_jtag.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ uint32_t adiv5_jtag_raw_access(adiv5_debug_port_s *dp, uint8_t rnw, uint16_t add
platform_timeout_set(&timeout, 250);
do {
uint64_t response;
jtag_dev_shift_dr(dp->dev_index, (uint8_t *)&response, (uint8_t *)&request, 35);
jtag_dev_shift_dr(dp->dev_index, (uint8_t *)&response, (const uint8_t *)&request, 35);
result = response >> 3U;
ack = response & 0x07U;
} while (!platform_timeout_is_expired(&timeout) && ack == JTAGDP_ACK_WAIT);
Expand Down
2 changes: 1 addition & 1 deletion src/target/cortexar.c
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,7 @@ static bool cortexar_mem_write_slow(
offset += 2U;
}
/* Use the fast path to write as much as possible before doing a slow path fixup at the end */
if (!cortexar_mem_write_fast(target, (uint32_t *)(data + offset), (length - offset) >> 2U))
if (!cortexar_mem_write_fast(target, (const uint32_t *)(data + offset), (length - offset) >> 2U))
return false;
const uint8_t remainder = (length - offset) & 3U;
/* If the remainder needs at least 2 more bytes write, do this first */
Expand Down
6 changes: 4 additions & 2 deletions src/target/kinetis.c
Original file line number Diff line number Diff line change
Expand Up @@ -474,9 +474,11 @@ static bool kinetis_flash_cmd_write(target_flash_s *f, target_addr_t dest, const
kinetis_flash_s *const kf = (kinetis_flash_s *)f;

/* Ensure we don't write something horrible over the security byte */
if (!f->t->unsafe_enabled && dest <= FLASH_SECURITY_BYTE_ADDRESS && dest + len > FLASH_SECURITY_BYTE_ADDRESS) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
if (!f->t->unsafe_enabled && dest <= FLASH_SECURITY_BYTE_ADDRESS && dest + len > FLASH_SECURITY_BYTE_ADDRESS)
((uint8_t *)src)[FLASH_SECURITY_BYTE_ADDRESS - dest] = FLASH_SECURITY_BYTE_UNSECURED;
}
#pragma GCC diagnostic pop

/* Determine write command based on the alignment. */
uint8_t write_cmd;
Expand Down
4 changes: 2 additions & 2 deletions src/target/lpc43xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* Copyright (C) 2014 Allen Ibara <aibara>
* Copyright (C) 2015 Gareth McMullin <[email protected]>
* Copyright (C) 2022 1BitSquared <[email protected]>
* Copyright (C) 2022-2024 1BitSquared <[email protected]>
* Rewritten by Rachel Mant <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -908,7 +908,7 @@ static void lpc43x0_spi_write(target_s *const target, const uint16_t command, co
target_mem32_write32(target, LPC43xx_GPIO_PORT0_SET, 1U << 6U);
lpc43x0_ssp0_setup_command(target, command, address);
/* And finally do the meat and potatoes of the transfer */
uint8_t *const data = (uint8_t *)buffer;
const uint8_t *const data = (const uint8_t *)buffer;
for (size_t i = 0; i < length; ++i)
lpc43x0_ssp0_transfer(target, data[i]);
/* Deselect the Flash */
Expand Down
9 changes: 6 additions & 3 deletions src/target/lpc_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* This file is part of the Black Magic Debug project.
*
* Copyright (C) 2015 Gareth McMullin <[email protected]>
* Copyright (C) 2022-2023 1BitSquared <[email protected]>
* Copyright (C) 2022-2024 1BitSquared <[email protected]>
* Modified by Rachel Mant <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -358,13 +358,16 @@ bool lpc_flash_write_magic_vect(target_flash_s *f, target_addr_t dest, const voi
{
if (dest == 0) {
/* Fill in the magic vector to allow booting the flash */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
uint32_t *const vectors = (uint32_t *)src;
#pragma GCC diagnostic pop
uint32_t sum = 0;

/* compute checksum of first 7 vectors */
/* Compute checksum of first 7 vectors */
for (size_t i = 0; i < 7U; ++i)
sum += vectors[i];
/* two's complement is written to 8'th vector */
/* Two's complement is written to 8'th vector */
vectors[7] = ~sum + 1U;
}
return lpc_flash_write(f, dest, src, len);
Expand Down
3 changes: 3 additions & 0 deletions src/target/nxpke04.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,11 @@ static bool ke04_flash_write(target_flash_s *f, target_addr_t dest, const void *
/* Ensure we don't write something horrible over the security byte */
target_s *t = f->t;
const uint8_t *data = src;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
if (!t->unsafe_enabled && dest <= FLASH_SECURITY_BYTE_ADDRESS && dest + len > FLASH_SECURITY_BYTE_ADDRESS)
((uint8_t *)data)[FLASH_SECURITY_BYTE_ADDRESS - dest] = FLASH_SECURITY_BYTE_UNSECURED;
#pragma GCC diagnostic pop

for (size_t offset = 0; offset < len; offset += KE04_WRITE_LEN) {
if (!ke04_command(f->t, CMD_PROGRAM_FLASH, dest + offset, data + offset))
Expand Down
2 changes: 1 addition & 1 deletion src/target/renesas_ra.c
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ static bool renesas_rv40_flash_write(target_flash_s *const flash, target_addr_t
/* Write one chunk */
for (size_t i = 0U; i < (write_size / 2U); i++) {
/* Copy data from source address to destination */
target_mem32_write16(target, RV40_CMD, *(uint16_t *)src);
target_mem32_write16(target, RV40_CMD, *(const uint16_t *)src);

/* 2 bytes of data */
src = (const uint8_t *)src + 2U;
Expand Down
2 changes: 1 addition & 1 deletion src/target/riscv32.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ static void riscv32_regs_write(target_s *const target, const void *const data)
{
/* Grab the hart structure and figure out how many registers need reading out */
riscv_hart_s *const hart = riscv_hart_struct(target);
riscv32_regs_s *const regs = (riscv32_regs_s *)data;
const riscv32_regs_s *const regs = (const riscv32_regs_s *)data;
const size_t gprs_count = hart->extensions & RV_ISA_EXT_EMBEDDED ? 16U : 32U;
/* Loop through writing out the GPRs, except for the first which is always 0 */
for (size_t gpr = 1; gpr < gprs_count; ++gpr) {
Expand Down
2 changes: 1 addition & 1 deletion src/target/riscv64.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ static void riscv64_regs_write(target_s *const target, const void *const data)
{
/* Grab the hart structure and figure out how many registers need reading out */
riscv_hart_s *const hart = riscv_hart_struct(target);
riscv64_regs_s *const regs = (riscv64_regs_s *)data;
const riscv64_regs_s *const regs = (const riscv64_regs_s *)data;
const size_t gprs_count = hart->extensions & RV_ISA_EXT_EMBEDDED ? 16U : 32U;
/* Loop through writing out the GPRs, except for the first which is always 0 */
for (size_t gpr = 1; gpr < gprs_count; ++gpr) {
Expand Down
16 changes: 14 additions & 2 deletions src/target/semihosting.c
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,10 @@ int32_t semihosting_open(target_s *const target, const semihosting_s *const requ

const int32_t result = open(file_name, native_open_mode | O_NOCTTY, 0644);
target->tc->gdb_errno = semihosting_errno();
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
free((void *)file_name);
#pragma GCC diagnostic pop
#else
gdb_putpacket_f("Fopen,%08" PRIX32 "/%08" PRIX32 ",%08" PRIX32 ",%08X", file_name_taddr, file_name_length + 1U,
open_mode, 0644U);
Expand Down Expand Up @@ -451,13 +454,13 @@ int32_t semihosting_read(target_s *const target, const semihosting_s *const requ
target_mem32_write(target, buf_taddr, semihosting_features + semihosting_features_offset, amount);
semihosting_features_offset += amount;
/* Return how much was left from what we transferred */
return buf_len - amount;
return (int32_t)(buf_len - amount);
}

const int32_t fd = request->params[0] - 1;
const int32_t result = semihosting_remote_read(target, fd, buf_taddr, buf_len);
if (result >= 0)
return buf_len - result;
return (int32_t)(buf_len - result);
return result;
}

Expand Down Expand Up @@ -550,13 +553,19 @@ int32_t semihosting_rename(target_s *const target, const semihosting_s *const re
return -1;
const char *const new_file_name = semihosting_read_string(target, request->params[2], request->params[3]);
if (new_file_name == NULL) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
free((void *)old_file_name);
#pragma GCC diagnostic pop
return -1;
}
const int32_t result = rename(old_file_name, new_file_name);
target->tc->gdb_errno = semihosting_errno();
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
free((void *)old_file_name);
free((void *)new_file_name);
#pragma GCC diagnostic pop
return result;
#else
gdb_putpacket_f("Frename,%08" PRIX32 "/%08" PRIX32 ",%08" PRIX32 "/%08" PRIX32, request->params[0],
Expand All @@ -573,7 +582,10 @@ int32_t semihosting_remove(target_s *const target, const semihosting_s *const re
return -1;
const int32_t result = remove(file_name);
target->tc->gdb_errno = semihosting_errno();
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
free((void *)file_name);
#pragma GCC diagnostic pop
return result;
#else
gdb_putpacket_f("Funlink,%08" PRIX32 "/%08" PRIX32, request->params[0], request->params[1] + 1U);
Expand Down
4 changes: 2 additions & 2 deletions src/target/spi.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of the Black Magic Debug project.
*
* Copyright (C) 2023 1BitSquared <[email protected]>
* Copyright (C) 2023-2024 1BitSquared <[email protected]>
* Written by Rachel Mant <[email protected]>
* All rights reserved.
*
Expand Down Expand Up @@ -80,7 +80,7 @@ void bmp_spi_write(const spi_bus_e bus, const uint8_t device, const uint16_t com
/* Setup the transaction */
bmp_spi_setup_xfer(bus, device, command, address);
/* Now write out back the data requested */
uint8_t *const data = (uint8_t *const)buffer;
const uint8_t *const data = (const uint8_t *)buffer;
for (size_t i = 0; i < length; ++i)
/* Do a write to read */
platform_spi_xfer(bus, data[i]);
Expand Down

0 comments on commit f3f4447

Please sign in to comment.