6

How can I do this:

*(int *)CMSG_DATA(hdr) = fd2pass;

Without GCC raising this:

error: dereferencing type-punned pointer will break strict-aliasing rules

In a way compatible with these options:

-Wall -Werror -pedantic

2 Answers2

6

Unless something is very wrong, there is no actual aliasing going on -- the object referred to by *(int *)CMSG_DATA(hdr) is not an alias for hdr -- it's past the end of hdr. The warning is incorrect.

You can work around it with memcpy:

memcpy(CMSG_DATA(hdr), &fd2pass, sizeof(int));

Don't use -fno-strict-aliasing: that disables optimizations that assume strict aliasing; it could generate considerably worse code.

For technical details, see glibc bug 16197.

elmarco
  • 31,633
  • 21
  • 64
  • 68
Andy Lutomirski
  • 1,343
  • 12
  • 15
  • 1
    I agree with you, but if you're not concerned with performance, the `-fno-strict-aliasing` option is a fine solution, as long as all of the compilers you use support it. – Adam Rosenfield Jan 04 '12 at 23:07
  • 1
    Ignoring compiler warnings without understanding what is going on is lazy and dangerous. A lot of people will come across this question and just proceed with the accepted answer without understanding the implications of -fno-strict-aliasing. The memcpy solution seems like a more acceptable general recommendation to me. – petrsnd Feb 15 '13 at 17:06
-2

Try passing -fno-strict-aliasing to gcc.

To shed a light on the strict aliasing topic, check this question.

Community
  • 1
  • 1
Gonzalo
  • 20,805
  • 3
  • 75
  • 78
  • Thanks, Gonzalo. I didn't want to remedy it in the Makefile, since I'm just adding a feature to an existing library, but now that I'm considering it, it doesn't look like a problem. –  Feb 09 '10 at 20:23
  • Ha. Easy. I just need to get over my fear of Makefiles. –  Feb 09 '10 at 20:33
  • 2
    Disabling features to get around compiler warnings is generally a bad idea. It is better to understand the warning and modify your code. When you just slap -fno-strict-aliasing on your compile line you are saying the compiler is wrong. In general, it is a good idea to assume your code is the problem. That being said, every code sample for passing file descriptors via domain sockets I have ever seen was written using the dereferencing scheme in the original question. GCC just started warning about it recently. – petrsnd Feb 15 '13 at 18:36
  • Wow, so three years after the answer was accepted you come here to give a personal opinion on how people should use compiler options and then cast a negative vote. Impressive. Keep it going. – Gonzalo Feb 16 '13 at 05:37
  • To be fair, this answer isn't really correct. -fno-strict-aliasing changes code generation, which is *not* what you want to do here. – Andy Lutomirski Jun 21 '13 at 00:51
  • This is the correct answer to the "How can I do this", "Without GCC raising this" from the question. The link should clear doubts about how disabling that error affects the behavior of the compiler and the code it generates. I answered the question without passing any judgement on whether that's the right thing to do or not. – Gonzalo Jun 21 '13 at 01:26
  • People reading this response are unlikely to follow the link to learn that the answer is almost certainly going to have undesired effects. Here's my downvote, too -- you've had plenty of time to fix it. – Andy Lutomirski Mar 20 '14 at 01:27
  • People following the link and reading the page will have the opportunity to learn about those side effects and decide whether it is worth for them or not. It was certainly worth it for the OP, who selected this answer as the one that worked for him. – Gonzalo Mar 21 '14 at 03:44