From fe87d2b6117de1e32769b388fe1704440e339d27 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Fri, 20 Oct 2017 15:19:53 +0200 Subject: [PATCH] astraceroute: use switch instead of lookup table for short proto id Avoid having a 58 entry array on stack of which only 3 are ever used. Just look up the short protocol identifier via a good'ol switch. Fixes Coverity CID 1381806 Signed-off-by: Tobias Klauser --- astraceroute.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/astraceroute.c b/astraceroute.c index 91674e6d..21e9a90e 100644 --- a/astraceroute.c +++ b/astraceroute.c @@ -746,6 +746,19 @@ static int timevalcmp(const void *t1, const void *t2) return 0; } +static const char *proto_short(int proto) +{ + switch (proto) { + case IPPROTO_TCP: + return "t"; + case IPPROTO_ICMP: + case IPPROTO_ICMPV6: + return "i"; + default: + return "?"; + } +} + static int __process_time(struct ctx *ctx, int fd, int fd_cap, int ttl, int inner_proto, uint8_t *pkt_snd, uint8_t *pkt_rcv, const struct sockaddr_storage *ss, @@ -756,11 +769,6 @@ static int __process_time(struct ctx *ctx, int fd, int fd_cap, int ttl, struct timeval probes[9], *tmp, sum, res; uint8_t *trash = xmalloc(ctx->rcvlen); char *cwait[] = { "-", "\\", "|", "/" }; - const char *proto_short[] = { - [IPPROTO_TCP] = "t", - [IPPROTO_ICMP] = "i", - [IPPROTO_ICMPV6] = "i", - }; memset(probes, 0, sizeof(probes)); for (i = 0; i < array_size(probes) && sigint == 0; ++i) { @@ -800,7 +808,7 @@ static int __process_time(struct ctx *ctx, int fd, int fd_cap, int ttl, qsort(tmp, j, sizeof(struct timeval), timevalcmp); - printf("\r%2d: %s[", ttl, proto_short[inner_proto]); + printf("\r%2d: %s[", ttl, proto_short(inner_proto)); idx = j / 2; switch (j % 2) { case 0: -- 2.11.4.GIT