public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/105775] New: GCC uses an invalid assumption in numeric limits of char
@ 2022-05-30 14:53 dante19031999 at gmail dot com
  2022-06-01 11:42 ` [Bug c/105775] " rguenth at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: dante19031999 at gmail dot com @ 2022-05-30 14:53 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 105775
           Summary: GCC uses an invalid assumption in numeric limits of
                    char
           Product: gcc
           Version: og11 (devel/omp/gcc-11)
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dante19031999 at gmail dot com
  Target Milestone: ---

On my system char is defined as a signed char which means that the limit is
indeed 127.
However in the C standard it is false, therefor there is an error.
This affects the use of the code for cross platform purposes.
Most probably you will find the complementary error in platforms where char is
defined as unsigned char giving the warning on cChar >= 0.
The error can be avoided to a certain extent with #if CHAR_MIN < 0 or
conversion to unsigned char...
The code shown here is a simplified version of the original made for the
purpose of the bug report.

According to cppreference:
signed char - type for signed character representation.
unsigned char - type for unsigned character representation. Also used to
inspect object representations (raw memory). 
char - type for character representation. Equivalent to either signed char or
unsigned char (which one is implementation-defined and may be controlled by a
compiler command line switch), but char is a distinct type, different from both
signed char and unsigned char.

./inc/ascii.h: In function 'is_ascii':
./inc/ascii.h:13:89: error: comparison is always true due to limited range of
data type [-Werror=type-limits]
   13 | __FULL_INLINE inline bool is_ascii( char cChar){return cChar >= 0 &&
cChar <= 127;}
      |                                                                        
   ^~
./inc/ascii.h: In function 'is_ascii_printable':
./inc/ascii.h:17:94: error: comparison is always true due to limited range of
data type [-Werror=type-limits]
   17 | __FULL_INLINE inline bool is_ascii_printable( char cChar){return cChar
>= 32 && cChar <= 127;}
      |                                                                        
              ^~
cc1: some warnings being treated as errors

#define __FULL_INLINE         __attribute__((__const__))
__attribute__((__nothrow__)) __attribute__((__always_inline__))

gcc -x c -std=c17 -Wimplicit-function-declaration -pipe -Werror=format-security
-Wextra -Wall -pedantic -frounding-math -fsignaling-nans -Werror=narrowing
-fPIC -Wunused-variable -Wunused-value -Wunused-but-set-variable -Og -std=gnu17
-I./ascii.h -c ./ascii.c -o ./instdir/ascii.c.o

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap
--enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,lto --prefix=/usr
--mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared
--enable-threads=posix --enable-checking=release --enable-multilib
--with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions
--enable-gnu-unique-object --enable-linker-build-id
--with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin
--enable-initfini-array
--with-isl=/builddir/build/BUILD/gcc-11.3.1-20220421/obj-x86_64-redhat-linux/isl-install
--enable-offload-targets=nvptx-none --without-cuda-driver
--enable-gnu-indirect-function --enable-cet --with-tune=generic
--with-arch_32=i686 --build=x86_64-redhat-linux
--with-build-config=bootstrap-lto --enable-link-serialization=1
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.3.1 20220421 (Red Hat 11.3.1-2) (GCC)

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

* [Bug c/105775] GCC uses an invalid assumption in numeric limits of char
  2022-05-30 14:53 [Bug c/105775] New: GCC uses an invalid assumption in numeric limits of char dante19031999 at gmail dot com
@ 2022-06-01 11:42 ` rguenth at gcc dot gnu.org
  2022-06-21 19:34 ` pinskia at gcc dot gnu.org
  2023-05-17 19:30 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-06-01 11:42 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|og11 (devel/omp/gcc-11)     |11.3.1

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
It's only an "error" if you use -Werror.  If you use system headers then the
diagnostic should also be suppressed automatically.

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

* [Bug c/105775] GCC uses an invalid assumption in numeric limits of char
  2022-05-30 14:53 [Bug c/105775] New: GCC uses an invalid assumption in numeric limits of char dante19031999 at gmail dot com
  2022-06-01 11:42 ` [Bug c/105775] " rguenth at gcc dot gnu.org
@ 2022-06-21 19:34 ` pinskia at gcc dot gnu.org
  2023-05-17 19:30 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-06-21 19:34 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I am trying to understand the issue here.
Are you saying the warning for "comparison is always true due to limited range
of data type" should not happen for the char type as it could either be signed
or unsigned?

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

* [Bug c/105775] GCC uses an invalid assumption in numeric limits of char
  2022-05-30 14:53 [Bug c/105775] New: GCC uses an invalid assumption in numeric limits of char dante19031999 at gmail dot com
  2022-06-01 11:42 ` [Bug c/105775] " rguenth at gcc dot gnu.org
  2022-06-21 19:34 ` pinskia at gcc dot gnu.org
@ 2023-05-17 19:30 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-17 19:30 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
So let's take the 2 functions:
int is_ascii( char cChar){return cChar >= 0 && cChar <= 127;}
int is_ascii_printable( char cChar){return cChar >= 32 && cChar <= 127;}


In C, char can be either signed or unsigned depending on the implementation.
For GCC, it depends on the target ABI (which can be changed at compile time
also with -fsigned-char and -funsigned-char too). (a side note is `char` is a
distinct type from `unsigned char` and `signed char`).

So obvious for is_ascii, GCC will warn when char defaults to signed (<= 127) or
unsigned (>= 0) and for is_ascii_printable, GCC will warn when char defaults to
signed only (<= 127).

Also this is what -Wtype-limits documentation says:
https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Warning-Options.html#index-Wtype-limits
```
Warn if a comparison is always true or always false due to the limited range of
the data type, but do not warn for constant expressions. For example, warn if
an unsigned variable is compared against zero with < or >=.
```


So yes this warning is by design.

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

end of thread, other threads:[~2023-05-17 19:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-30 14:53 [Bug c/105775] New: GCC uses an invalid assumption in numeric limits of char dante19031999 at gmail dot com
2022-06-01 11:42 ` [Bug c/105775] " rguenth at gcc dot gnu.org
2022-06-21 19:34 ` pinskia at gcc dot gnu.org
2023-05-17 19:30 ` pinskia 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).