Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "flexible array member X not at end of struct" compiling with GCC-11 #348

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions gloo/common/linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,29 +192,28 @@ const std::string& infinibandToBusID(const std::string& name) {
static int getInterfaceSpeedGLinkSettings(int sock, struct ifreq* ifr) {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,6,0)
constexpr auto link_mode_data_nwords = 3 * 127;
struct {
struct ethtool_link_settings req;
__u32 link_mode_data[link_mode_data_nwords];
} ecmd;
constexpr auto bufsize = sizeof(struct ethtool_link_settings) + sizeof(__u32)*link_mode_data_nwords;
char buf[bufsize];
struct ethtool_link_settings& ecmd = *(struct ethtool_link_settings*)buf;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this is technically a strict aliasing violation and hence UB. At least buf needs to be aligned as struct ethtool_link_settings and possibly a placement new needs to be used.

Idea: Using an union to enforce the size might work:

  constexpr auto bufsize = sizeof(struct ethtool_link_settings) + sizeof(__u32)*link_mode_data_nwords;
  union {
    struct ethtool_link_settings req;
    char buf[bufsize];
  } ecmd;

int rv;

ifr->ifr_data = (__caddr_t)&ecmd;
memset(&ecmd, 0, sizeof(ecmd));
ecmd.req.cmd = ETHTOOL_GLINKSETTINGS;
memset(buf, 0, bufsize);
ecmd.cmd = ETHTOOL_GLINKSETTINGS;

rv = ioctl(sock, SIOCETHTOOL, ifr);
if (rv < 0 || ecmd.req.link_mode_masks_nwords >= 0) {
if (rv < 0 || ecmd.link_mode_masks_nwords >= 0) {
return SPEED_UNKNOWN;
}

ecmd.req.cmd = ETHTOOL_GLINKSETTINGS;
ecmd.req.link_mode_masks_nwords = -ecmd.req.link_mode_masks_nwords;
ecmd.cmd = ETHTOOL_GLINKSETTINGS;
ecmd.link_mode_masks_nwords = -ecmd.link_mode_masks_nwords;
rv = ioctl(sock, SIOCETHTOOL, ifr);
if (rv < 0) {
return SPEED_UNKNOWN;
}

return ecmd.req.speed;
return ecmd.speed;
#else
(void)sock;
(void)ifr;
Expand Down