From cf3a9cddc8f000622bd973263de2a95d54959912 Mon Sep 17 00:00:00 2001 From: Lucas De Marchi Date: Fri, 16 Mar 2018 10:10:23 -0700 Subject: [PATCH] AP_HAL_Linux: fix build error with flexible array Apparently this code came in part from libuavcan that defines this struct Control. They also had the same issue detailed on https://github.com/UAVCAN/libuavcan/issues/116. The solution here is much simpler though: stick to the design of cmsg() even if it's C. As per cmsg(3), use a union together with CMSG_SPACE(). --- libraries/AP_HAL_Linux/CAN.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/libraries/AP_HAL_Linux/CAN.cpp b/libraries/AP_HAL_Linux/CAN.cpp index e39031189e..632dbea3bd 100644 --- a/libraries/AP_HAL_Linux/CAN.cpp +++ b/libraries/AP_HAL_Linux/CAN.cpp @@ -371,18 +371,16 @@ int CAN::_read(uavcan::CanFrame& frame, uavcan::UtcTime& ts_utc, bool& loopback) auto sockcan_frame = can_frame(); iov.iov_base = &sockcan_frame; iov.iov_len = sizeof(sockcan_frame); - - struct Control - { - cmsghdr cm; - std::uint8_t data[sizeof(::timeval)]; + union { + uint8_t data[CMSG_SPACE(sizeof(::timeval))]; + struct cmsghdr align; } control; auto msg = msghdr(); msg.msg_iov = &iov; msg.msg_iovlen = 1; - msg.msg_control = &control; - msg.msg_controllen = sizeof(control); + msg.msg_control = control.data; + msg.msg_controllen = sizeof(control.data); const int res = recvmsg(_fd, &msg, MSG_DONTWAIT); if (res <= 0) {