diff --git a/meson.build b/meson.build index 363a594645c..531e3fe16c9 100644 --- a/meson.build +++ b/meson.build @@ -94,7 +94,7 @@ extended_warnings = [ '-Wbad-function-cast', # '-Wcast-align=strict', '-Wcast-function-type', - # '-Wcast-qual', + '-Wcast-qual', # '-Wconversion', '-Wdangling-else', '-Wdouble-promotion', diff --git a/src/gdb_main.c b/src/gdb_main.c index 4bde81c17b8..9cca19e0119 100644 --- a/src/gdb_main.c +++ b/src/gdb_main.c @@ -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 + * Copyright (C) 2022-2024 1BitSquared + * Modified by Rachel Mant * * 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 @@ -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) @@ -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); diff --git a/src/platforms/hosted/gdb_if.c b/src/platforms/hosted/gdb_if.c index 09bf16a4b1b..26d65ac8f80 100644 --- a/src/platforms/hosted/gdb_if.c +++ b/src/platforms/hosted/gdb_if.c @@ -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; } diff --git a/src/platforms/hosted/probe_info.c b/src/platforms/hosted/probe_info.c index 678f59084fe..b068b8dfbd6 100644 --- a/src/platforms/hosted/probe_info.c +++ b/src/platforms/hosted/probe_info.c @@ -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); } diff --git a/src/remote.c b/src/remote.c index ed04bcc8859..278fb43fead 100644 --- a/src/remote.c +++ b/src/remote.c @@ -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: @@ -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); diff --git a/src/target/adiv5_jtag.c b/src/target/adiv5_jtag.c index 024f40f4c4d..0ccb2d2c19b 100644 --- a/src/target/adiv5_jtag.c +++ b/src/target/adiv5_jtag.c @@ -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); diff --git a/src/target/cortexar.c b/src/target/cortexar.c index 7c313e712fe..30e320e46da 100644 --- a/src/target/cortexar.c +++ b/src/target/cortexar.c @@ -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 */ diff --git a/src/target/kinetis.c b/src/target/kinetis.c index 93464451e6b..c6dabf44462 100644 --- a/src/target/kinetis.c +++ b/src/target/kinetis.c @@ -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; diff --git a/src/target/lpc43xx.c b/src/target/lpc43xx.c index adbf8b6db08..2e6fe84e19d 100644 --- a/src/target/lpc43xx.c +++ b/src/target/lpc43xx.c @@ -3,7 +3,7 @@ * * Copyright (C) 2014 Allen Ibara * Copyright (C) 2015 Gareth McMullin - * Copyright (C) 2022 1BitSquared + * Copyright (C) 2022-2024 1BitSquared * Rewritten by Rachel Mant * * This program is free software: you can redistribute it and/or modify @@ -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 */ diff --git a/src/target/lpc_common.c b/src/target/lpc_common.c index 69e134df5b7..cef2ebeefb9 100644 --- a/src/target/lpc_common.c +++ b/src/target/lpc_common.c @@ -2,7 +2,7 @@ * This file is part of the Black Magic Debug project. * * Copyright (C) 2015 Gareth McMullin - * Copyright (C) 2022-2023 1BitSquared + * Copyright (C) 2022-2024 1BitSquared * Modified by Rachel Mant * * This program is free software: you can redistribute it and/or modify @@ -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); diff --git a/src/target/nxpke04.c b/src/target/nxpke04.c index 7e1a97ef46b..5bf31fc4cd3 100644 --- a/src/target/nxpke04.c +++ b/src/target/nxpke04.c @@ -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)) diff --git a/src/target/renesas_ra.c b/src/target/renesas_ra.c index c26ccbdf0d6..df23ebda0b0 100644 --- a/src/target/renesas_ra.c +++ b/src/target/renesas_ra.c @@ -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; diff --git a/src/target/riscv32.c b/src/target/riscv32.c index b721cd5de55..57e9472c844 100644 --- a/src/target/riscv32.c +++ b/src/target/riscv32.c @@ -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) { diff --git a/src/target/riscv64.c b/src/target/riscv64.c index d9fd4a898b0..b2fe72051e2 100644 --- a/src/target/riscv64.c +++ b/src/target/riscv64.c @@ -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) { diff --git a/src/target/semihosting.c b/src/target/semihosting.c index 8c5b8999596..1575064cfb5 100644 --- a/src/target/semihosting.c +++ b/src/target/semihosting.c @@ -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); @@ -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; } @@ -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], @@ -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); diff --git a/src/target/spi.c b/src/target/spi.c index ad4c789a090..523fef9a2cd 100644 --- a/src/target/spi.c +++ b/src/target/spi.c @@ -1,7 +1,7 @@ /* * This file is part of the Black Magic Debug project. * - * Copyright (C) 2023 1BitSquared + * Copyright (C) 2023-2024 1BitSquared * Written by Rachel Mant * All rights reserved. * @@ -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]);