Email List: Xaustin-group-lX
[All Lists]

Re: [1003.1(2008)/Issue 7 0000074]: Pointer Types Problem

To: Geoff Clare <gwc@xxxxxxxxxxxxx>
Subject: Re: [1003.1(2008)/Issue 7 0000074]: Pointer Types Problem
From: Wojtek Lerch <wojtek@xxxxxxx>
Date: Thu, 02 Jul 2009 10:20:41 -0400
Cc: austin-group-l@xxxxxxxxxxxxx
References: <e26e5335cda9d0dfe49395e6115066fe@austingroupbugs.net> <1730938080F74546ACC51CB65BA00565@ott.qnx.com> <20090702094404.GA28393@squonk.masqnet>
I don't think it's a good idea to introduce unusual restrictions on what the void* pointer can be converted to. Can I convert it to a "void const*"? What about a "void *const"? Why would it need to be undefined behaviour to convert it to a char*, even though standard C intends to allow converting any valid void* pointer to char*?

Also, a few details are missing from the wording in question:

* Do these conversion work with null pointers? Are they guaranteed to turn null pointers into null pointers? (If not, then one needs to test the result of dlsym() before converting it to a function pointer, rather than after.)

* Is it possible for the converted void* pointer to compare equal to the address to some object? (It can happen on hardware with separate address spaces for code and data -- does POSIX want to support such hardware?)

Here's my attempt at putting this into words:

Any valid pointer to a function type can be converted to a pointer to void [or maybe also to a character type]. The result can be converted back to a function type, producing a value that compares equal to the original value.

If the original pointer is a null pointer, then the converted pointer to void is a null pointer too. If the original pointer points to a function, then the result is not a null pointer, but it is unspecified whether it points to any particular object or not (and therefore the results of dereferencing it are undefined), or whether it is correctly aligned for any type other than a character type. The void* pointer may happen to compare equal to a pointer to some object or to one past the end of some array, but nevertheless is not considered to point to either.


Geoff Clare wrote:
Wojtek Lerch <wojtek@qnx.com> wrote, on 01 Jul 2009:

(0000129) msbrown (manager) - 2009-06-29 02:27
http://austingroupbugs.net/view.php?id=74#c129
--------------------------------------------------------------------
After the sentence ending on line 18890, add the following:

If a pointer to an object or incomplete type is converted to a
pointer to void and then to a pointer to a function type the
behavior is undefined.
I don't think this is right. In C, an assignment or an initialization
is specified to always performs a conversion -- even a conversion from
a type to the same type is still considered a conversion. Doesn't that
mean that the value returned by dlsym() cannot be assigned to anything
without invoking undefined behaviour?

Good point.  I think it needs to say:

    If a pointer to an object or an incomplete type other than void is
    converted ...


If a pointer to a function type is converted to a pointer to
void and then to a pointer to an object or an incomplete type the
behaviour is undefined.

Likewise this should say "... or an incomplete type other than void ..."


Doesn't that imply that it's safe to convert a pointer to function
to a pointer to void, as long as the pointer is not subsequently
converted to some other pointer type? Is it meant to imply that
it's OK for a portable POSIX application to take the address of an
arbitrary function and convert it to void* and back?

That's pretty much exactly what XSH 2.12.3 says, so yes that's the
intention.  Otherwise we would have worded 2.12.3 completely
differently (with explicit reference to dlsym()).






<Prev in Thread] Current Thread [Next in Thread>