public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/45289]  New: gcc lacks a "posix" option for "-std" since POSIX 2008 defines special behavior
@ 2010-08-15 19:28 reddwarf at opensuse dot org
  2010-08-15 19:31 ` [Bug c/45289] " pinskia at gcc dot gnu dot org
                   ` (10 more replies)
  0 siblings, 11 replies; 14+ messages in thread
From: reddwarf at opensuse dot org @ 2010-08-15 19:28 UTC (permalink / raw)
  To: gcc-bugs

As originally reported at
http://lists.opensuse.org/opensuse-packaging/2010-08/msg00038.html POSIX 2008
says
(http://www.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_12_03):

"2.12.3 Pointer Types

All function pointer types shall have the same representation as the type
pointer to void. Conversion of a function pointer to void * shall not alter the
representation. A void * value resulting from such a conversion can be
converted back to the original function pointer type, using an explicit cast,
without loss of information.

Note:
    The ISO C standard does not require this, but it is required for POSIX
conformance."

When compiling the example code for dlsym
(http://www.opengroup.org/onlinepubs/9699919799/functions/dlsym.html) gcc
complains with: "warning: dereferencing type-punned pointer will break
strict-aliasing rules".

Not sure how this applies for C++, but g++ outputs the same warning.


The suggested fix is to add a posix/posix2008 mode and make that the default
for "platforms that are supposed to be POSIX", specifically Linux.


-- 
           Summary: gcc lacks a "posix" option for "-std" since POSIX 2008
                    defines special behavior
           Product: gcc
           Version: 4.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: reddwarf at opensuse dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45289


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug c/45289] gcc lacks a "posix" option for "-std" since POSIX 2008 defines special behavior
  2010-08-15 19:28 [Bug c/45289] New: gcc lacks a "posix" option for "-std" since POSIX 2008 defines special behavior reddwarf at opensuse dot org
@ 2010-08-15 19:31 ` pinskia at gcc dot gnu dot org
  2010-08-15 19:38 ` redi at gcc dot gnu dot org
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2010-08-15 19:31 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2010-08-15 19:31 -------
> "*(void **)(&comp_compress) = dlsym(handle, "comp_compress");"

That is not alias safe. Try instead:
comp_compress = (__typeof__(comp_compress)) dlsym(handle, "comp_compress");

Which is alias safe.  In fact reading what posix standard says it talks about
conversion between pointer to function types being safe but not access to a
function pointer type via a pointer to void type.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45289


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug c/45289] gcc lacks a "posix" option for "-std" since POSIX 2008 defines special behavior
  2010-08-15 19:28 [Bug c/45289] New: gcc lacks a "posix" option for "-std" since POSIX 2008 defines special behavior reddwarf at opensuse dot org
  2010-08-15 19:31 ` [Bug c/45289] " pinskia at gcc dot gnu dot org
@ 2010-08-15 19:38 ` redi at gcc dot gnu dot org
  2010-08-15 19:47 ` jakub at gcc dot gnu dot org
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: redi at gcc dot gnu dot org @ 2010-08-15 19:38 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from redi at gcc dot gnu dot org  2010-08-15 19:38 -------
I don't think adding -std=posix is the right solution, since dlsym needs to be
usable if users choose other options such as -std=c++0x or -std=gnu99


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45289


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug c/45289] gcc lacks a "posix" option for "-std" since POSIX 2008 defines special behavior
  2010-08-15 19:28 [Bug c/45289] New: gcc lacks a "posix" option for "-std" since POSIX 2008 defines special behavior reddwarf at opensuse dot org
  2010-08-15 19:31 ` [Bug c/45289] " pinskia at gcc dot gnu dot org
  2010-08-15 19:38 ` redi at gcc dot gnu dot org
@ 2010-08-15 19:47 ` jakub at gcc dot gnu dot org
  2010-08-15 20:21 ` reddwarf at opensuse dot org
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: jakub at gcc dot gnu dot org @ 2010-08-15 19:47 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from jakub at gcc dot gnu dot org  2010-08-15 19:47 -------
Yes, POSIX adds additional requirements about pointer representation and
conversion, but AFAIK all targets GCC support and have POSIXish runtime satisfy
that.  The conversion between pointer types is not the problem in your code, it
is aliasing violation, and there is nothing in POSIX that says your code is
valid.


-- 

jakub at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45289


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug c/45289] gcc lacks a "posix" option for "-std" since POSIX 2008 defines special behavior
  2010-08-15 19:28 [Bug c/45289] New: gcc lacks a "posix" option for "-std" since POSIX 2008 defines special behavior reddwarf at opensuse dot org
                   ` (2 preceding siblings ...)
  2010-08-15 19:47 ` jakub at gcc dot gnu dot org
@ 2010-08-15 20:21 ` reddwarf at opensuse dot org
  2010-08-15 21:07 ` matz at gcc dot gnu dot org
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: reddwarf at opensuse dot org @ 2010-08-15 20:21 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from reddwarf at opensuse dot org  2010-08-15 20:21 -------
> Yes, POSIX adds additional requirements about pointer representation and
> conversion, but AFAIK all targets GCC support and have POSIXish runtime satisfy
> that.  The conversion between pointer types is not the problem in your code, it
> is aliasing violation, and there is nothing in POSIX that says your code is
> valid.

Uhm. Sorry, indeed the example code in posix 2008 dlsym() documentation is
wrong because of aliasing (where should this be reported? is also wrong in
http://www.kernel.org/doc/man-pages/). I was confused because that code was
wrong in posix 2004 but the fix from comment #1 was also wrong (not anymore in
posix 2008).


But using the fix from comment #1 gcc complains with "warning: ISO C forbids
conversion of object pointer to function pointer type", at least when using
-pedantic.
The *message* in the warning is correct. But it is complaining about something
that isn't a conversion from "object pointer to function pointer type" but a
conversion from "void* pointer to function pointer type", that posix 2008
explicitly allows.


-- 

reddwarf at opensuse dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|INVALID                     |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45289


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug c/45289] gcc lacks a "posix" option for "-std" since POSIX 2008 defines special behavior
  2010-08-15 19:28 [Bug c/45289] New: gcc lacks a "posix" option for "-std" since POSIX 2008 defines special behavior reddwarf at opensuse dot org
                   ` (3 preceding siblings ...)
  2010-08-15 20:21 ` reddwarf at opensuse dot org
@ 2010-08-15 21:07 ` matz at gcc dot gnu dot org
  2010-08-15 21:34 ` jakub at gcc dot gnu dot org
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: matz at gcc dot gnu dot org @ 2010-08-15 21:07 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from matz at gcc dot gnu dot org  2010-08-15 21:07 -------
First, yes, the work-around from the official POSIX man-pages is alias-unsafe.
They added this example because ISO C doesn't allow conversion of void*
pointers to function pointer, but dlsym returns a void* pointer.

There are two possibilities to use dlsym:
1) ptrtofntype f = (ptrtofntype) dlsym (...);
2) *(void**)&f = dlsym (...);

The former is invalid ISO C (conversion between void* and function pointer
not allowed), the latter is invalid ISO C (alias violation).

So they were between a rock and a hard place, the dlsym interface was
impossible to use with a strict ISO C compiler when it was used to get at
addresses of functions.

That's why they added language to POSIX 2008 to make conversions between
void* and function-pointer types valid.  They could have added a new
interface in parallel to dlsym that would return a function pointer from
the start.  Well, they chose to add a new requirement for POSIX systems
making variant (1) valid.

GCC, at least on POSIX systems, should reflect this, and also make this
conversion valid.  In fact we're already doing the right thing for such
conversions, so we only need to specify that this isn't going to change
in the future and disable the warning even in pedantic mode.

That means we wouldn't be a strict ISO compiler in this mode anymore.
It's what happen when there are two standards in conflict with each other.
I think that by default, even with -pedantic, we should not warn for the
void* <-> fnptr conversion (note that POSIX only specifically allows the
conversion from/to void*, not to any arbitrary data pointer type).


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45289


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug c/45289] gcc lacks a "posix" option for "-std" since POSIX 2008 defines special behavior
  2010-08-15 19:28 [Bug c/45289] New: gcc lacks a "posix" option for "-std" since POSIX 2008 defines special behavior reddwarf at opensuse dot org
                   ` (4 preceding siblings ...)
  2010-08-15 21:07 ` matz at gcc dot gnu dot org
@ 2010-08-15 21:34 ` jakub at gcc dot gnu dot org
  2010-08-15 21:38 ` rguenth at gcc dot gnu dot org
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: jakub at gcc dot gnu dot org @ 2010-08-15 21:34 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from jakub at gcc dot gnu dot org  2010-08-15 21:33 -------
There are more possibilities, like:
3) void (*fnptr) (void); void *p = dlsym (...); memcpy (&fnptr, &p, sizeof
(p)); fnptr ();
The POSIX standard wording doesn't talk about void * and function pointers
being compatible types as used in ISO C99, 6.5.  It only talks about the same
representation and conversion preserving it.  Thus, from this wording, I'd say
the 1) is also valid for POSIX, but not 2).  When the representation is the
same, memcpy should be safe, both from aliasing POV and should DTRT, as
workaround for the warning in 1).


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45289


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug c/45289] gcc lacks a "posix" option for "-std" since POSIX 2008 defines special behavior
  2010-08-15 19:28 [Bug c/45289] New: gcc lacks a "posix" option for "-std" since POSIX 2008 defines special behavior reddwarf at opensuse dot org
                   ` (5 preceding siblings ...)
  2010-08-15 21:34 ` jakub at gcc dot gnu dot org
@ 2010-08-15 21:38 ` rguenth at gcc dot gnu dot org
  2010-08-16  0:55 ` matz at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2010-08-15 21:38 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from rguenth at gcc dot gnu dot org  2010-08-15 21:38 -------
Note that this is a common mistake anyways and for example with LTO we
unify all pointer alias-sets to that of void *.  In fact I proposed that
for all languages anyway (look at the hack the C++ FE has for pointer-to-member
function pointers for example).

So, I'll just take this one and finally commit the pending patch(es).


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         AssignedTo|unassigned at gcc dot gnu   |rguenth at gcc dot gnu dot
                   |dot org                     |org
             Status|UNCONFIRMED                 |ASSIGNED
     Ever Confirmed|0                           |1
   Last reconfirmed|0000-00-00 00:00:00         |2010-08-15 21:38:37
               date|                            |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45289


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug c/45289] gcc lacks a "posix" option for "-std" since POSIX 2008 defines special behavior
  2010-08-15 19:28 [Bug c/45289] New: gcc lacks a "posix" option for "-std" since POSIX 2008 defines special behavior reddwarf at opensuse dot org
                   ` (6 preceding siblings ...)
  2010-08-15 21:38 ` rguenth at gcc dot gnu dot org
@ 2010-08-16  0:55 ` matz at gcc dot gnu dot org
  2010-08-16 15:05 ` rwild at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: matz at gcc dot gnu dot org @ 2010-08-16  0:55 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from matz at gcc dot gnu dot org  2010-08-16 00:55 -------
Well, okay, (3) indeed is valid ISO C (no warning) and works on POSIX 2008.
I'd find it very awkward to write such work-around for (1) just so the
warning in strict ISO C mode is silenced.  I find this case different from
other cases where such warnings about standard violations are explicitely
wanted even though perhaps the code happens to work.  I think it's different
from those cases because POSIX (mostly concerned with portability) is a 
standard too, one that we'd probably like to adhere to on some systems.

Hence, I still would suggest to not warn about solution (1), even in ISO C
mode.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45289


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug c/45289] gcc lacks a "posix" option for "-std" since POSIX 2008 defines special behavior
  2010-08-15 19:28 [Bug c/45289] New: gcc lacks a "posix" option for "-std" since POSIX 2008 defines special behavior reddwarf at opensuse dot org
                   ` (7 preceding siblings ...)
  2010-08-16  0:55 ` matz at gcc dot gnu dot org
@ 2010-08-16 15:05 ` rwild at gcc dot gnu dot org
  2010-08-16 15:05 ` rwild at gcc dot gnu dot org
  2010-08-27 19:26 ` rguenth at gcc dot gnu dot org
  10 siblings, 0 replies; 14+ messages in thread
From: rwild at gcc dot gnu dot org @ 2010-08-16 15:05 UTC (permalink / raw)
  To: gcc-bugs



-- 

rwild at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rwild at gcc dot gnu dot org
         AssignedTo|rguenth at gcc dot gnu dot  |unassigned at gcc dot gnu
                   |org                         |dot org
             Status|ASSIGNED                    |NEW


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45289


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug c/45289] gcc lacks a "posix" option for "-std" since POSIX 2008 defines special behavior
  2010-08-15 19:28 [Bug c/45289] New: gcc lacks a "posix" option for "-std" since POSIX 2008 defines special behavior reddwarf at opensuse dot org
                   ` (8 preceding siblings ...)
  2010-08-16 15:05 ` rwild at gcc dot gnu dot org
@ 2010-08-16 15:05 ` rwild at gcc dot gnu dot org
  2010-08-27 19:26 ` rguenth at gcc dot gnu dot org
  10 siblings, 0 replies; 14+ messages in thread
From: rwild at gcc dot gnu dot org @ 2010-08-16 15:05 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #9 from rwild at gcc dot gnu dot org  2010-08-16 15:05 -------
hit the wrong button, apologies


-- 

rwild at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         AssignedTo|unassigned at gcc dot gnu   |rguenth at gcc dot gnu dot
                   |dot org                     |org
             Status|NEW                         |ASSIGNED


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45289


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug c/45289] gcc lacks a "posix" option for "-std" since POSIX 2008 defines special behavior
  2010-08-15 19:28 [Bug c/45289] New: gcc lacks a "posix" option for "-std" since POSIX 2008 defines special behavior reddwarf at opensuse dot org
                   ` (9 preceding siblings ...)
  2010-08-16 15:05 ` rwild at gcc dot gnu dot org
@ 2010-08-27 19:26 ` rguenth at gcc dot gnu dot org
  10 siblings, 0 replies; 14+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2010-08-27 19:26 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #10 from rguenth at gcc dot gnu dot org  2010-08-27 19:25 -------
Not working on the C frontend change.  But the warning should be gone on trunk.


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         AssignedTo|rguenth at gcc dot gnu dot  |unassigned at gcc dot gnu
                   |org                         |dot org
             Status|ASSIGNED                    |NEW


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45289


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug c/45289] gcc lacks a "posix" option for "-std" since POSIX 2008 defines special behavior
       [not found] <bug-45289-4@http.gcc.gnu.org/bugzilla/>
  2014-07-08  0:38 ` bugdal at aerifal dot cx
@ 2014-07-08  9:22 ` rguenth at gcc dot gnu.org
  1 sibling, 0 replies; 14+ messages in thread
From: rguenth at gcc dot gnu.org @ 2014-07-08  9:22 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=45289

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |INVALID

--- Comment #12 from Richard Biener <rguenth at gcc dot gnu.org> ---
Closed as fixed in POSIX.


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug c/45289] gcc lacks a "posix" option for "-std" since POSIX 2008 defines special behavior
       [not found] <bug-45289-4@http.gcc.gnu.org/bugzilla/>
@ 2014-07-08  0:38 ` bugdal at aerifal dot cx
  2014-07-08  9:22 ` rguenth at gcc dot gnu.org
  1 sibling, 0 replies; 14+ messages in thread
From: bugdal at aerifal dot cx @ 2014-07-08  0:38 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=45289

Rich Felker <bugdal at aerifal dot cx> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugdal at aerifal dot cx

--- Comment #11 from Rich Felker <bugdal at aerifal dot cx> ---
This bug in POSIX (recommending the aliasing violation) was fixed. Now they
(correctly) recommend the cast. So I think this bug could possibly be closed,
unless we want to re-apply it to things like #61739 or perhaps memory-model and
synchronization issues.


^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2014-07-08  9:22 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-08-15 19:28 [Bug c/45289] New: gcc lacks a "posix" option for "-std" since POSIX 2008 defines special behavior reddwarf at opensuse dot org
2010-08-15 19:31 ` [Bug c/45289] " pinskia at gcc dot gnu dot org
2010-08-15 19:38 ` redi at gcc dot gnu dot org
2010-08-15 19:47 ` jakub at gcc dot gnu dot org
2010-08-15 20:21 ` reddwarf at opensuse dot org
2010-08-15 21:07 ` matz at gcc dot gnu dot org
2010-08-15 21:34 ` jakub at gcc dot gnu dot org
2010-08-15 21:38 ` rguenth at gcc dot gnu dot org
2010-08-16  0:55 ` matz at gcc dot gnu dot org
2010-08-16 15:05 ` rwild at gcc dot gnu dot org
2010-08-16 15:05 ` rwild at gcc dot gnu dot org
2010-08-27 19:26 ` rguenth at gcc dot gnu dot org
     [not found] <bug-45289-4@http.gcc.gnu.org/bugzilla/>
2014-07-08  0:38 ` bugdal at aerifal dot cx
2014-07-08  9:22 ` rguenth at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).