the.jxc has asked for the wisdom of the Perl Monks concerning the following question:

Hey Monks,

I have an XS module which has worked for years, but is giving me warnings with a new compiler:

for (i = 0; i < n; i++) { HE *he = hv_iternext (hv); STRLEN key_len; >>> char *key = HePV (he, key_len);
Utils.xs:263:13: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]

The offending line is: "char *key = HePV (he, key_len);"

I've resolved some similar errors by changing the type of the LHS variable. But in this case, (char *) seems to be the absolutely correct type. Presumably it's because HePV is a macro that maps to a complicated nested ternary variable structure which maybe contains a subtle mix of types?

Can I do anything, other than perhaps just turning off warnings for this?

  • Comment on Calling HePV - "warning: dereferencing type-punned pointer will break strict-aliasing rules"
  • Select or Download Code

Replies are listed 'Best First'.
Re: Calling HePV - "warning: dereferencing type-punned pointer will break strict-aliasing rules"
by dave_the_m (Monsignor) on Jul 25, 2018 at 07:51 UTC
    perl is normally built with -fno-strict-aliasing, so I'm not sure why your compiler is checking for strict aliasing

    Dave.

      This is an external XS module that I am compiling.

      Searching on that error gives me all sorts of doom and gloom about how this can lead to program malfunction, addressing memory by two different pointer types!

      Not sure I believe it. Am tempted just to turn off strict-aliasing for that section. :)

        This is an external XS module that I am compiling
        Yes, but XS modules are normally compiled using a similar set of compiler flags to that used to compile perl itself. Which is why I'm surprised that -fno-strict-aliasing isn't being passed. Compiling XS code without that flag while using perl macros which are intended to be used with that flag, is asking for subtle optimiser bugs to be introduced into your compiled code.

        Dave.

        This is an external XS module that I am compiling

        But if perl was built with -fno-strict-aliasing then one would expect that the compiler would not emit the warning when building an external XS module.
        If you provide us with the output of perl -V and tell us where we can view the entire source of the XS module, then we can perhaps shed some light on the matter.

        Cheers,
        Rob