Skip to content

Commit

Permalink
misc: replace VLAs with alloca()
Browse files Browse the repository at this point in the history
Replace all variable-length arrays with a call to `alloca()`. This
doesn't solve the problem, and merely papers over it. However, there are
two benefits to this approach:

1. We can now grep for `alloca()` in order to find all uses of VLAs
2. This helps to build under MSVC, which simply doesn't support VLAs

Signed-off-by: Sean Cross <[email protected]>
  • Loading branch information
xobs committed Nov 29, 2023
1 parent 2bda2af commit 672dad7
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 10 deletions.
12 changes: 6 additions & 6 deletions src/gdb_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ int gdb_main_loop(target_controller_s *tc, char *pbuf, size_t pbuf_size, size_t
ERROR_IF_NO_TARGET();
const size_t reg_size = target_regs_size(cur_target);
if (reg_size) {
uint8_t gp_regs[reg_size];
uint8_t *gp_regs = alloca(reg_size);
target_regs_read(cur_target, gp_regs);
gdb_putpacket(hexify(pbuf, gp_regs, reg_size), reg_size * 2U);
} else {
Expand All @@ -160,7 +160,7 @@ int gdb_main_loop(target_controller_s *tc, char *pbuf, size_t pbuf_size, size_t
break;
}
DEBUG_GDB("m packet: addr = %" PRIx32 ", len = %" PRIx32 "\n", addr, len);
uint8_t mem[len];
uint8_t *mem = alloca(len);
if (target_mem_read(cur_target, mem, addr, len))
gdb_putpacketz("E01");
else
Expand All @@ -171,7 +171,7 @@ int gdb_main_loop(target_controller_s *tc, char *pbuf, size_t pbuf_size, size_t
ERROR_IF_NO_TARGET();
const size_t reg_size = target_regs_size(cur_target);
if (reg_size) {
uint8_t gp_regs[reg_size];
uint8_t *gp_regs = alloca(reg_size);
unhexify(gp_regs, &pbuf[1], reg_size);
target_regs_write(cur_target, gp_regs);
}
Expand All @@ -189,7 +189,7 @@ int gdb_main_loop(target_controller_s *tc, char *pbuf, size_t pbuf_size, size_t
break;
}
DEBUG_GDB("M packet: addr = %" PRIx32 ", len = %" PRIx32 "\n", addr, len);
uint8_t mem[len];
uint8_t *mem = alloca(len);
unhexify(mem, pbuf + hex, len);
if (target_mem_write(cur_target, addr, mem, len))
gdb_putpacketz("E01");
Expand Down Expand Up @@ -268,7 +268,7 @@ int gdb_main_loop(target_controller_s *tc, char *pbuf, size_t pbuf_size, size_t
int n;
sscanf(pbuf, "P%" SCNx32 "=%n", &reg, &n);
// TODO: FIXME, VLAs considered harmful.
uint8_t val[strlen(pbuf + n) / 2U];
uint8_t *val = alloca(strlen(pbuf + n) / 2U);
unhexify(val, pbuf + n, sizeof(val));
if (target_reg_write(cur_target, reg, val, sizeof(val)) > 0)
gdb_putpacketz("OK");
Expand Down Expand Up @@ -403,7 +403,7 @@ static void exec_q_rcmd(const char *packet, const size_t length)
else {
const char *const response = "Failed\n";
const size_t response_length = strlen(response);
char pbuf[response_length * 2 + 1];
char *pbuf = alloca(response_length * 2 + 1);
gdb_putpacket(hexify(pbuf, response, response_length), 2 * response_length);
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/include/general.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
#define __USE_MINGW_ANSI_STDIO 1
#endif
#if defined(_WIN32) || defined(__CYGWIN__)
#include <malloc.h>
#else
#include <alloca.h>
#endif
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
Expand Down
4 changes: 2 additions & 2 deletions src/rtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ static uint32_t fast_search(target_s *const cur_target, const uint32_t ram_start
static const uint64_t r = 0x73b07d01;
static const uint32_t stride = 128;
uint64_t t = 0;
uint8_t srch_buf[m + stride];
uint8_t *srch_buf = alloca(m + stride);

memset(srch_buf, 0, sizeof(srch_buf));
memset(srch_buf, 0, m + stride);

for (uint32_t addr = ram_start; addr < ram_end; addr += stride) {
uint32_t buf_siz = MIN(stride, ram_end - addr);
Expand Down
2 changes: 1 addition & 1 deletion src/target/adiv5.c
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ static void adiv5_component_probe(
}

#if defined(ENABLE_DEBUG)
char indent[recursion + 1U];
char *indent = alloca(recursion + 1U);

for (size_t i = 0; i < recursion; i++)
indent[i] = ' ';
Expand Down
2 changes: 1 addition & 1 deletion src/target/msp432p4.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ static bool msp432_flash_write(target_flash_s *f, target_addr_t dest, const void
DEBUG_WARN("Flash protect: 0x%08" PRIX32 "\n", target_mem_read32(t, mf->flash_protect_register));

/* Prepare input data */
uint32_t regs[t->regs_size / sizeof(uint32_t)]; // Use of VLA
uint32_t *regs = alloca(t->regs_size / sizeof(uint32_t)); // Use of VLA
target_regs_read(t, regs);
regs[0] = SRAM_WRITE_BUFFER; // Address of buffer to be flashed in R0
regs[1] = dest; // Flash address to be write to in R1
Expand Down

0 comments on commit 672dad7

Please sign in to comment.