aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric <ewild@sysmocom.de>2023-01-09 18:05:44 +0100
committerHoernchen <ewild@sysmocom.de>2023-01-10 17:13:09 +0000
commit864e1a2d565aac3afa833dbe6b61999c679088a4 (patch)
treeb5949e2ef872e584a87066c94057b6b2bedf7c33
parentb057535512046101250278c416b0ae5a1b41e251 (diff)
lapd_pcap: fix illegal VLA within struct
Another weird and unique gcc supported extension, clang complains: CC input/lapd_pcap.lo input/lapd_pcap.c:134:8: error: fields must have a constant size: 'variable length array in structure' extension will never be supported char buf[msg->len]; ^ The kernel has been VLA-free since 2018, let's try not to make it worse. Change-Id: I547d900ba88c60bae5b53345e1aee104bacd310d
-rw-r--r--src/input/lapd_pcap.c41
1 files changed, 22 insertions, 19 deletions
diff --git a/src/input/lapd_pcap.c b/src/input/lapd_pcap.c
index 6e1faf5..f9d17ef 100644
--- a/src/input/lapd_pcap.c
+++ b/src/input/lapd_pcap.c
@@ -128,44 +128,47 @@ int osmo_pcap_lapd_open(char *filename, mode_t mode)
int osmo_pcap_lapd_write(int fd, int direction, struct msgb *msg)
{
struct timeval tv;
- struct {
+ struct tmp_pkt {
struct pcap_rechdr pcap_rechdr;
struct pcap_lapdhdr header;
- char buf[msg->len];
- } __attribute__((packed)) s;
+ char buf[0];
+ } __attribute__((packed));
+ const unsigned int total_pkt_len = sizeof(struct tmp_pkt) + msg->len;
+
+ struct tmp_pkt *s = alloca(total_pkt_len);
/* PCAP file has not been opened, skip. */
if (fd < 0)
return 0;
- memset(&s, 0, sizeof(s));
+ memset(s, 0, total_pkt_len);
- s.pcap_rechdr.ts_sec = 0;
- s.pcap_rechdr.ts_usec = 0;
- s.pcap_rechdr.incl_len = msg->len + sizeof(struct pcap_lapdhdr);
- s.pcap_rechdr.orig_len = msg->len + sizeof(struct pcap_lapdhdr);
+ s->pcap_rechdr.ts_sec = 0;
+ s->pcap_rechdr.ts_usec = 0;
+ s->pcap_rechdr.incl_len = msg->len + sizeof(struct pcap_lapdhdr);
+ s->pcap_rechdr.orig_len = msg->len + sizeof(struct pcap_lapdhdr);
if (direction == OSMO_LAPD_PCAP_OUTPUT)
- s.header.pkttype = htons(LINUX_SLL_OUTGOING);
+ s->header.pkttype = htons(LINUX_SLL_OUTGOING);
else
- s.header.pkttype = htons(LINUX_SLL_HOST);
- s.header.hatype = 0;
- s.header.halen = 0;
- s.header.addr[0] = 0x01; /* we are the network side */
- s.header.protocol = ntohs(48);
+ s->header.pkttype = htons(LINUX_SLL_HOST);
+ s->header.hatype = 0;
+ s->header.halen = 0;
+ s->header.addr[0] = 0x01; /* we are the network side */
+ s->header.protocol = ntohs(48);
gettimeofday(&tv, NULL);
- s.pcap_rechdr.ts_sec = tv.tv_sec;
- s.pcap_rechdr.ts_usec = tv.tv_usec;
+ s->pcap_rechdr.ts_sec = tv.tv_sec;
+ s->pcap_rechdr.ts_usec = tv.tv_usec;
- memcpy(s.buf, msg->data, msg->len);
+ memcpy(s->buf, msg->data, msg->len);
- if (write(fd, &s, sizeof(s)) != sizeof(s)) {
+ if (write(fd, s, total_pkt_len) != total_pkt_len) {
LOGP(DLLAPD, LOGL_ERROR, "cannot write packet to PCAP: %s\n",
strerror(errno));
return -1;
}
- return sizeof(s);
+ return total_pkt_len;
}
int osmo_pcap_lapd_close(int fd)