public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* Calling functions through a pointer of incorrect type
@ 2016-03-22  9:20 Florian Weimer
  2016-03-22 19:14 ` Joseph Myers
  0 siblings, 1 reply; 5+ messages in thread
From: Florian Weimer @ 2016-03-22  9:20 UTC (permalink / raw)
  To: GNU C Library

Just a sanity check: It is no longer acceptable glibc coding style to
call functions through mismatching pointer types.  Correct?

Typical mismatches are: The function has an argument of type struct foo
**, but the function pointer has type void ** for the same argument.  Or
the function pointer has additional trailing arguments not declared by
the function.

These calls result in undefined behavior in standard C, and I don't
think it's a support GCC extension.

Florian

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

* Re: Calling functions through a pointer of incorrect type
  2016-03-22  9:20 Calling functions through a pointer of incorrect type Florian Weimer
@ 2016-03-22 19:14 ` Joseph Myers
  2016-03-22 20:25   ` Florian Weimer
  0 siblings, 1 reply; 5+ messages in thread
From: Joseph Myers @ 2016-03-22 19:14 UTC (permalink / raw)
  To: Florian Weimer; +Cc: GNU C Library

On Tue, 22 Mar 2016, Florian Weimer wrote:

> Just a sanity check: It is no longer acceptable glibc coding style to
> call functions through mismatching pointer types.  Correct?

I think more specific examples would help so we can see if there are 
cleaner approaches that generate the same code.  (For example, it's 
deliberate that in some places functions with incompatible C types but 
compatible ABIs are aliased to each other.)

-- 
Joseph S. Myers
joseph@codesourcery.com

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

* Re: Calling functions through a pointer of incorrect type
  2016-03-22 19:14 ` Joseph Myers
@ 2016-03-22 20:25   ` Florian Weimer
  2016-03-22 21:33     ` Joseph Myers
  0 siblings, 1 reply; 5+ messages in thread
From: Florian Weimer @ 2016-03-22 20:25 UTC (permalink / raw)
  To: Joseph Myers; +Cc: GNU C Library

On 03/22/2016 08:14 PM, Joseph Myers wrote:
> On Tue, 22 Mar 2016, Florian Weimer wrote:
> 
>> Just a sanity check: It is no longer acceptable glibc coding style to
>> call functions through mismatching pointer types.  Correct?
> 
> I think more specific examples would help so we can see if there are 
> cleaner approaches that generate the same code.  (For example, it's 
> deliberate that in some places functions with incompatible C types but 
> compatible ABIs are aliased to each other.)

I saw this in getaddrinfo:

  if (fct == NULL)
    /* We are cheating here.  The gethostbyname2_r
       function does not have the same interface as
       gethostbyname3_r but the extra arguments the
       latter takes are added at the end.  So the
       gethostbyname2_r code will just ignore them.  */
    fct = __nss_lookup_function (nip, "gethostbyname2_r");

The void ** vs concrete pointer example is __nss_getent.

For __nss_getent, it wouldn't be too hard to split the buffer management
from the call, and move the call to the caller of __nss_getent, where
the correct type is known.

getaddrinfo may be harder and involve splitting the code around the caller.

Florian

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

* Re: Calling functions through a pointer of incorrect type
  2016-03-22 20:25   ` Florian Weimer
@ 2016-03-22 21:33     ` Joseph Myers
  2016-03-29 21:27       ` Richard Henderson
  0 siblings, 1 reply; 5+ messages in thread
From: Joseph Myers @ 2016-03-22 21:33 UTC (permalink / raw)
  To: Florian Weimer; +Cc: GNU C Library

On Tue, 22 Mar 2016, Florian Weimer wrote:

> On 03/22/2016 08:14 PM, Joseph Myers wrote:
> > On Tue, 22 Mar 2016, Florian Weimer wrote:
> > 
> >> Just a sanity check: It is no longer acceptable glibc coding style to
> >> call functions through mismatching pointer types.  Correct?
> > 
> > I think more specific examples would help so we can see if there are 
> > cleaner approaches that generate the same code.  (For example, it's 
> > deliberate that in some places functions with incompatible C types but 
> > compatible ABIs are aliased to each other.)
> 
> I saw this in getaddrinfo:
> 
>   if (fct == NULL)
>     /* We are cheating here.  The gethostbyname2_r
>        function does not have the same interface as
>        gethostbyname3_r but the extra arguments the
>        latter takes are added at the end.  So the
>        gethostbyname2_r code will just ignore them.  */
>     fct = __nss_lookup_function (nip, "gethostbyname2_r");

Well, glibc is written with knowledge of ABI properties - that is, objects 
can assume that other objects follow the ABI and so interact in ways that 
are not valid whole-program C but are valid given that each object is an 
ABI-conforming compilation of its own sources on their own.  So while 
cleaning up cases like this would seem desirable unless particularly 
performance-critical, and I wouldn't like to see new code like this 
without strong justification, they aren't inherently bugs if all supported 
ABIs have the required property.

-- 
Joseph S. Myers
joseph@codesourcery.com

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

* Re: Calling functions through a pointer of incorrect type
  2016-03-22 21:33     ` Joseph Myers
@ 2016-03-29 21:27       ` Richard Henderson
  0 siblings, 0 replies; 5+ messages in thread
From: Richard Henderson @ 2016-03-29 21:27 UTC (permalink / raw)
  To: Joseph Myers, Florian Weimer; +Cc: GNU C Library

On 03/22/2016 02:33 PM, Joseph Myers wrote:
> On Tue, 22 Mar 2016, Florian Weimer wrote:
>>   if (fct == NULL)
>>     /* We are cheating here.  The gethostbyname2_r
>>        function does not have the same interface as
>>        gethostbyname3_r but the extra arguments the
>>        latter takes are added at the end.  So the
>>        gethostbyname2_r code will just ignore them.  */
>>     fct = __nss_lookup_function (nip, "gethostbyname2_r");
> 
> ... they aren't inherently bugs if all supported
> ABIs have the required property.

The only ABI I can think of that doesn't have this property is Windows
__stdcall.  Which obviously isn't used in any glibc configuration.


r~

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

end of thread, other threads:[~2016-03-29 21:27 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-03-22  9:20 Calling functions through a pointer of incorrect type Florian Weimer
2016-03-22 19:14 ` Joseph Myers
2016-03-22 20:25   ` Florian Weimer
2016-03-22 21:33     ` Joseph Myers
2016-03-29 21:27       ` Richard Henderson

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).