From b91143ff4258957189331cf63c2e58cace471614 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Mon, 1 Sep 2014 16:11:34 +0200 Subject: trafgen: Replace variable length arrays Use zmalloc()'ed buffer or statically sized array to replace usage of variable sized arrays. Found by the sparse static checker. Signed-off-by: Tobias Klauser --- trafgen.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/trafgen.c b/trafgen.c index 6a6bd02..03a165e 100644 --- a/trafgen.c +++ b/trafgen.c @@ -128,6 +128,8 @@ struct icmp_filter { }; #endif +#define SMOKE_N_PROBES 100 + static void signal_handler(int number) { switch (number) { @@ -355,10 +357,10 @@ static struct cpu_stats *setup_shared_var(unsigned long cpus) { int fd; size_t len = cpus * sizeof(struct cpu_stats); - char zbuff[len], file[256]; + char *zbuff, file[256]; struct cpu_stats *buff; - fmemset(zbuff, 0, len); + zbuff = xzmalloc(len); slprintf(file, sizeof(file), ".tmp_mmap.%u", (unsigned int) rand()); fd = creat(file, S_IRUSR | S_IWUSR); @@ -368,6 +370,7 @@ static struct cpu_stats *setup_shared_var(unsigned long cpus) fd = open_or_die_m(file, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); write_or_die(fd, zbuff, len); + xfree(zbuff); buff = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); @@ -433,8 +436,8 @@ static int xmit_smoke_setup(struct ctx *ctx) static int xmit_smoke_probe(int icmp_sock, struct ctx *ctx) { int ret; - unsigned int i, j = 0, probes = 100; - short ident, cnt = 1, idstore[probes]; + unsigned int i, j; + short ident, cnt = 1, idstore[SMOKE_N_PROBES]; uint8_t outpack[512], *data; struct icmphdr *icmp; struct iphdr *ip; @@ -447,10 +450,10 @@ static int xmit_smoke_probe(int icmp_sock, struct ctx *ctx) }; fmemset(idstore, 0, sizeof(idstore)); - while (probes-- > 0) { + for (j = 0; j < SMOKE_N_PROBES; j++) { while ((ident = htons((short) rand())) == 0) sleep(0); - idstore[j++] = ident; + idstore[j] = ident; memset(outpack, 0, sizeof(outpack)); icmp = (void *) outpack; -- cgit v1.2.3-54-g00ecf