src/winsup/cygwin ChangeLog Makefile.in autolo ...

Ryan Johnson ryan.johnson@cs.utoronto.ca
Sat Apr 23 11:00:00 GMT 2011


On 22/04/2011 3:49 PM, Yaakov (Cygwin/X) wrote:
>> CVSROOT:	/cvs/src
>> Module name:	src
>> Changes by:	corinna@...	2011-04-19 10:02:07
>>
>> 	* registry.cc (reg_key): Rewrite reg_key class to use native NT registry
>> 	functions.  Use WCHAR string parameters throughout.  Use PCWSTR rather
>> 	than const WCHAR.  Drop multibyte char functionality.  Drop unused
>> 	methods.
> This change introduced a compiler warning with GCC 4.5:
>
> cc1plus: warnings being treated as errors
> registry.cc: In member function ‘int reg_key::get_int(const WCHAR*,
> int)’:
> registry.cc:145:32: error: dereferencing type-punned pointer will break
> strict-aliasing rules
For the benefit of folks reading the archives later:

*(DWORD*) is asking for trouble, and technically, even the cast from 
alloca's void* breaks C++ strict aliasing rules.

The common suggestion is to compile with -fno-strict-aliasing, but gcc 
allows to turn off strict aliasing on a case-by-case basis by converting 
casts into members of a union:

-  PKEY_VALUE_PARTIAL_INFORMATION vbuf = (PKEY_VALUE_PARTIAL_INFORMATION)
-                      alloca (size);
+  union { void* v; PKEY_VALUE_PARTIAL_INFORMATION vbuf; DWORD *dst; } u =
+                      {alloca (size)};
...
-  status = NtQueryValueKey (key, &uname, KeyValuePartialInformation, vbuf,
+  status = NtQueryValueKey (key, &uname, KeyValuePartialInformation, 
u.vbuf,
...
-  DWORD dst = *(DWORD *) vbuf->Data;
-  return (int) dst;
+  return (int) *u.dst;

In my experience, the union also allows the compiler to generate better 
code in some cases.

Ryan




More information about the Cygwin-developers mailing list