This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/80745] inconsistent warning: large integer implicitly truncated to unsigned type
- From: "egallager at gcc dot gnu.org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Tue, 01 Aug 2017 15:42:43 +0000
- Subject: [Bug c/80745] inconsistent warning: large integer implicitly truncated to unsigned type
- Auto-submitted: auto-generated
- References: <bug-80745-4@http.gcc.gnu.org/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80745
Eric Gallager <egallager at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |NEW
Last reconfirmed| |2017-08-01
CC| |egallager at gcc dot gnu.org
Ever confirmed|0 |1
--- Comment #2 from Eric Gallager <egallager at gcc dot gnu.org> ---
(In reply to Martin Sebor from comment #0)
> In four declarations below, the initializer expression is truncated when
> assigned to unsigned char. Yet only the first two initializers are
> diagnosed (the warning message could be more helpful but that's the subject
> of bug 80731). The same problem affects other unsigned integers besides
> unsigned char.
>
> All four initializers should be diagnosed.
>
> $ cat t.c && gcc -S -Wall -Wextra -Wpedantic -Woverflow t.c
> #include <limits.h>
>
> unsigned char uc1 = UCHAR_MAX + 1U;
> unsigned char uc2 = USHRT_MAX + 1U;
> unsigned char uc3 = UINT_MAX + 1U;
> unsigned char uc4 = ULONG_MAX + 1LU;
>
> t.c:3:21: warning: large integer implicitly truncated to unsigned type
> [-Woverflow]
> unsigned char uc1 = UCHAR_MAX + 1U;
> ^~~~~~~~~
> t.c:4:21: warning: large integer implicitly truncated to unsigned type
> [-Woverflow]
> unsigned char uc2 = USHRT_MAX + 1U;
> ^~~~~~~~~
Confirmed, although since you've already improved -Woverflow a little since
then, the warning now reads:
$ /usr/local/bin/gcc -c -S -Wall -Wextra -Wpedantic -Woverflow 80745.c
80745.c:3:21: warning: conversion from ‘unsigned int’ to ‘unsigned char’
changes value from ‘256’ to ‘0’ [-Woverflow]
unsigned char uc1 = UCHAR_MAX + 1U;
^~~~~~~~~
80745.c:4:21: warning: conversion from ‘unsigned int’ to ‘unsigned char’
changes value from ‘65536’ to ‘0’ [-Woverflow]
unsigned char uc2 = USHRT_MAX + 1U;
^~~~~~~~~
$
(In reply to Martin Sebor from comment #1)
> The reason for the missing warning is that in the latter two cases the
> initializer expression itself wraps around to zero, which isn't diagnosed or
> detected, and the initialization then isn't diagnosed.
>
> It seems that unsigned integer wrapping should be diagnosed independently of
> signed integer overflow (e.g., under -Wtruncation or something like that),
> and consistently for any kind of unsigned truncation or wrapping.
>
I dunno, the fact that unsigned integers wrap is pretty commonly (ab)used on
purpose in lots of code I've seen. I'd be wary about a warning about unsigned
integer wrapping triggering lots of false positives. Still, at least in this
specific testcase in this bug, warning in the additional cases you recommend
seems reasonable.