[PATCH] <semaphore.h>: Add nonnull attributes

Yann Droneaud ydroneaud@opteya.com
Wed Mar 20 15:44:00 GMT 2019


Hi,

Le mardi 19 mars 2019 à 19:40 -0300, Adhemerval Zanella a écrit :
> 
> On 18/03/2019 09:37, Yann Droneaud wrote:
> > Except the following functions, NPTL implementation assume sem_t
> > argument (or other arguments) are not NULL, so they would benefit
> > from having the nonnull attribute.
> > 
> > - sem_close(): can cope with a NULL sem_t and return -1 with error EINVAL;
> > - sem_destroy(): does nothing at all
> > 
> > 	* sysdeps/pthread/semaphore.h (sem_init): Add __nonnull attribute.
> > 	(sem_destroy, sem_open, sem_close, sem_unlink): Likewise.
> > 	(sem_wait, sem_timedwait, sem_trywait, sem_post): Likewise.
> > 	(sem_getvalue): Likewise.
> 
> LGTM, although reading some gcc bug reports I am not sure how effective 
> this attribute is in realword cases. For instance, gcc BZ#17308 [1] indicates
> that gcc stills missing the warning for deduced values (and the bug was
> closed with the premise of gcc fixing at least for some builtins like
> print).
> 
> However it seems that for arbitrary function definition the attribute
> still does not help much:
> 
> ---
> $ cat test.c
> #include <sys/cdefs.h>
> 
> /* Arbitrary type just as an example.  */
> typedef union
> {
>   char __size[16];
>   long int __align;
> } sem_t;
> 
> extern sem_t *sem_open (const char *__name, int __oflag, ...)
>   __THROW __nonnull ((1));
> 
> sem_t * bar (void)
> {
>   char *name = 0;
>   return sem_open (name, 0);
> }
> $ gcc -v
> [...]
> gcc version 9.0.1 20190315 (experimental) (GCC) 
> $ gcc -Wall -Wnonnull test.c -c
> $
> 

Thanks for the review !

For the -Wnonnull warning to be effective you have to enable some
optimization. I don't know the exact one. At least with the ones
enabled by -O1:

$ gcc -Wnonnull -O test.c -c
test.c: In function 'bar':
test.c:16:10: warning: argument 1 null where non-null expected [-Wnonnull]
   16 |   return sem_open (name, 0);
      |          ^~~~~~~~~~~~~~~~~~
test.c:10:15: note: in a call to function 'sem_open' declared here
   10 | extern sem_t *sem_open (const char *__name, int __oflag, ...)
      |               ^~~~~~~~

$ gcc -v
[...]
gcc version 9.0.1 20190312 (Red Hat 9.0.1-0.10) (GCC) 

> ---
> 
> [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=17308
> 

Regards.

-- 
Yann Droneaud
OPTEYA





More information about the Libc-alpha mailing list