public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* No type checking in ctype builtins
@ 2022-04-27 15:29 Andrea Monaco
  2022-04-27 15:54 ` Andreas Schwab
  2022-04-27 16:19 ` Jonathan Wakely
  0 siblings, 2 replies; 6+ messages in thread
From: Andrea Monaco @ 2022-04-27 15:29 UTC (permalink / raw)
  To: gcc


This program

  #include <ctype.h>

  int main ()
  {
    char *s;
    isspace (s);
  }

compiles with no warning in gcc 8.3.0, even though there's a type
mistake; the correct call would be isspace (*s).

The ctype functions are implemented as macros in glibc, so you can't
have type checking.  But they are also provided as builtins by gcc, so I
wonder why type checking is not performed in that case, either.



Thanks,

Andrea Monaco

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

* Re: No type checking in ctype builtins
  2022-04-27 15:29 No type checking in ctype builtins Andrea Monaco
@ 2022-04-27 15:54 ` Andreas Schwab
  2022-04-27 18:07   ` Andrea Monaco
  2022-04-28 11:13   ` Florian Weimer
  2022-04-27 16:19 ` Jonathan Wakely
  1 sibling, 2 replies; 6+ messages in thread
From: Andreas Schwab @ 2022-04-27 15:54 UTC (permalink / raw)
  To: Andrea Monaco via Gcc; +Cc: Andrea Monaco

On Apr 27 2022, Andrea Monaco via Gcc wrote:

> This program
>
>   #include <ctype.h>
>
>   int main ()
>   {
>     char *s;
>     isspace (s);
>   }
>
> compiles with no warning in gcc 8.3.0, even though there's a type
> mistake; the correct call would be isspace (*s).

Try -Wsystem-headers.

> The ctype functions are implemented as macros in glibc, so you can't
> have type checking.  But they are also provided as builtins by gcc, so I
> wonder why type checking is not performed in that case, either.

You need to suppress the macro to get the builtin.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."

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

* Re: No type checking in ctype builtins
  2022-04-27 15:29 No type checking in ctype builtins Andrea Monaco
  2022-04-27 15:54 ` Andreas Schwab
@ 2022-04-27 16:19 ` Jonathan Wakely
  2022-04-28 12:02   ` Eric Gallager
  1 sibling, 1 reply; 6+ messages in thread
From: Jonathan Wakely @ 2022-04-27 16:19 UTC (permalink / raw)
  To: Andrea Monaco; +Cc: gcc

On Wed, 27 Apr 2022 at 16:29, Andrea Monaco via Gcc <gcc@gcc.gnu.org> wrote:
>
>
> This program
>
>   #include <ctype.h>
>
>   int main ()
>   {
>     char *s;
>     isspace (s);
>   }
>
> compiles with no warning in gcc 8.3.0, even though there's a type
> mistake; the correct call would be isspace (*s).

N.B. The correct call would be isspace((unsigned char)*s) because
isspace has undefined behaviour if you pass it a char with a negative
value.

It would be nice if GCC warned about *that*.

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

* Re: No type checking in ctype builtins
  2022-04-27 15:54 ` Andreas Schwab
@ 2022-04-27 18:07   ` Andrea Monaco
  2022-04-28 11:13   ` Florian Weimer
  1 sibling, 0 replies; 6+ messages in thread
From: Andrea Monaco @ 2022-04-27 18:07 UTC (permalink / raw)
  To: schwab, jwakely.gcc; +Cc: gcc


  > Try -Wsystem-headers.

You're right.  That showed a warning.


  > You need to suppress the macro to get the builtin.

That means a macro expansion takes precedence over a builtin function
substitution, if I understand correctly.  It makes sense, because
preprocessing happens before compilation.

Maybe we could mention that in the manual, for example in "6.59 Other
Built-in Functions Provided by GCC"
(https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html).



Andrea Monaco


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

* Re: No type checking in ctype builtins
  2022-04-27 15:54 ` Andreas Schwab
  2022-04-27 18:07   ` Andrea Monaco
@ 2022-04-28 11:13   ` Florian Weimer
  1 sibling, 0 replies; 6+ messages in thread
From: Florian Weimer @ 2022-04-28 11:13 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: Andrea Monaco via Gcc, Andrea Monaco

* Andreas Schwab:

> On Apr 27 2022, Andrea Monaco via Gcc wrote:
>
>> This program
>>
>>   #include <ctype.h>
>>
>>   int main ()
>>   {
>>     char *s;
>>     isspace (s);
>>   }
>>
>> compiles with no warning in gcc 8.3.0, even though there's a type
>> mistake; the correct call would be isspace (*s).
>
> Try -Wsystem-headers.
>
>> The ctype functions are implemented as macros in glibc, so you can't
>> have type checking.  But they are also provided as builtins by gcc, so I
>> wonder why type checking is not performed in that case, either.
>
> You need to suppress the macro to get the builtin.

Shouldn't that happen automatically with -O2?

I think it's a header bug, we don't use inline functions in C mode, only
in C++ mode:

#ifndef __cplusplus
# define __isctype(c, type) \
  ((*__ctype_b_loc ())[(int) (c)] & (unsigned short int) type)
#elif defined __USE_EXTERN_INLINES
# define __isctype_f(type) \
  __extern_inline int							      \
  is##type (int __c) __THROW						      \
  {									      \
    return (*__ctype_b_loc ())[(int) (__c)] & (unsigned short int) _IS##type; \
  }
#endif

Thanks,
Florian


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

* Re: No type checking in ctype builtins
  2022-04-27 16:19 ` Jonathan Wakely
@ 2022-04-28 12:02   ` Eric Gallager
  0 siblings, 0 replies; 6+ messages in thread
From: Eric Gallager @ 2022-04-28 12:02 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: Andrea Monaco, gcc

On Wed, Apr 27, 2022 at 12:20 PM Jonathan Wakely via Gcc
<gcc@gcc.gnu.org> wrote:
>
> On Wed, 27 Apr 2022 at 16:29, Andrea Monaco via Gcc <gcc@gcc.gnu.org> wrote:
> >
> >
> > This program
> >
> >   #include <ctype.h>
> >
> >   int main ()
> >   {
> >     char *s;
> >     isspace (s);
> >   }
> >
> > compiles with no warning in gcc 8.3.0, even though there's a type
> > mistake; the correct call would be isspace (*s).
>
> N.B. The correct call would be isspace((unsigned char)*s) because
> isspace has undefined behaviour if you pass it a char with a negative
> value.
>
> It would be nice if GCC warned about *that*.

Related bugs:
78155: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78155
99950: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99950

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

end of thread, other threads:[~2022-04-28 12:02 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-27 15:29 No type checking in ctype builtins Andrea Monaco
2022-04-27 15:54 ` Andreas Schwab
2022-04-27 18:07   ` Andrea Monaco
2022-04-28 11:13   ` Florian Weimer
2022-04-27 16:19 ` Jonathan Wakely
2022-04-28 12:02   ` Eric Gallager

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