[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[netCDF #XKJ-645277]: Type punning in swap4b()



Hi Larry,

Thanks for the bug report; I've squashed similar ones in the past year, and 
will take a look at this for the upcoming release!

Have a great day,

-Ward

> When I call nc_get_vara_double() to read floats from a dataset I get
> zeros rather than the values in the dataset.
> 
> I tracked it down to a typecast in the function swap4b() in
> libsrc/ncx.m4:205.  Here's the line of code:
> *(float *)op = *(float *)(&tempOut);
> This does type punning, accessing an unsigned value (tempOut) via a cast
> to a float*, which the C standard says results in undefined behavior.
> When I compile with GCC's -Wall, the compiler points out the bug in the
> source:
> 
> ncx.m4:205: warning: dereferencing type-punned pointer will break
> strict-aliasing rules
> 
> There are also four lines in ncdump/ncdump.c that cause the same warning.
> 
> I can work around the problem by building the output of libsrc/ncx.m4
> with the GCC option -fno-strict-aliasing.  Compiled with no special
> options, the function ends like this and results in zeros:
> mov    -0x4(%rsp),%eax
> or     %edx,%ecx
> mov    %ecx,-0x4(%rsp)
> mov    %eax,(%rdi)
> retq
> Complied with -fno-strict-aliasing, it ends like this and works:
> or     %edx,%ecx
> mov    %ecx,-0x4(%rsp)
> mov    -0x4(%rsp),%eax
> mov    %eax,(%rdi)
> retq
> 
> I'm building netCDF 4.4.1 with GCC 4.9.2 for Linux.
> 
> -Larry
> 
> 


Ticket Details
===================
Ticket ID: XKJ-645277
Department: Support netCDF
Priority: Normal
Status: Closed
===================
NOTE: All email exchanges with Unidata User Support are recorded in the Unidata 
inquiry tracking system and then made publicly available through the web.  If 
you do not want to have your interactions made available in this way, you must 
let us know in each email you send to us.