From: Sami Kerola Date: Mon, 29 Aug 2016 09:54:55 +0000 (+0100) Subject: warnings: avoid vla when malloc() is more appropriate X-Git-Tag: v0.88~26^2~28 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ad710b93a2cd0f00daedebbaf0cd5288169217d8;p=thirdparty%2Fmtr.git warnings: avoid vla when malloc() is more appropriate Assuming allocatin failure will happen use of vla will cause very strange looking error. In this occasion it is better to use malloc, and check it was successful. Found with smatch scan. --- diff --git a/net.c b/net.c index 6779da2..7a77af0 100644 --- a/net.c +++ b/net.c @@ -254,8 +254,13 @@ static int checksum(void *data, int sz) static int udp_checksum(struct mtr_ctl *ctl, void *pheader, void *udata, int psize, int dsize, int alt_checksum) { - unsigned int tsize = psize + dsize; - char csumpacket[tsize]; + size_t tsize = psize + dsize; + char *csumpacket; + int ret; + + csumpacket = malloc(tsize); + if (!csumpacket) + error(EXIT_FAILURE, errno, "cannot allocate %zu bytes", tsize); memset(csumpacket, (unsigned char) abs(ctl->bitpattern), tsize); if (alt_checksum && dsize >= 2) { csumpacket[psize + sizeof(struct UDPHeader)] = 0; @@ -277,7 +282,9 @@ static int udp_checksum(struct mtr_ctl *ctl, void *pheader, void *udata, content->length = udpdata->length; content->checksum = udpdata->checksum; - return checksum(csumpacket,tsize); + ret = checksum(csumpacket, tsize); + free(csumpacket); + return ret; }