* Questions on -Wtype-limits
@ 2021-01-03 18:53 William Roberts
2021-01-05 3:47 ` Kewen.Lin
0 siblings, 1 reply; 2+ messages in thread
From: William Roberts @ 2021-01-03 18:53 UTC (permalink / raw)
To: gcc-help
When I compile the below program, I expect the -Wtype-limits warning
to be displayed for all 4 if conditions, but I only get uint8_t and
uint16_t conditions, why?
#include <stdint.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
(void)argv;
uint8_t i8 = (uint8_t) argc;
uint16_t i16 = (uint16_t) argc;
uint32_t i32 = (uint32_t) argc;
uint64_t i64 = (uint64_t) argc;
if (i8 > UINT8_MAX) {
return 8;
}
if (i16 > UINT16_MAX) {
return 16;
}
if (i32 > UINT32_MAX) {
return 32;
}
if (i64 > UINT64_MAX) {
return 64;
}
return 0;
}
gcc -Wall -Wextra -o go a.c
a.c: In function ‘main’:
a.c:13:9: warning: comparison is always false due to limited range of
data type [-Wtype-limits]
13 | if (i8 > UINT8_MAX) {
| ^
a.c:17:10: warning: comparison is always false due to limited range of
data type [-Wtype-limits]
17 | if (i16 > UINT16_MAX) {
gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Thanks,
Bill
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Questions on -Wtype-limits
2021-01-03 18:53 Questions on -Wtype-limits William Roberts
@ 2021-01-05 3:47 ` Kewen.Lin
0 siblings, 0 replies; 2+ messages in thread
From: Kewen.Lin @ 2021-01-05 3:47 UTC (permalink / raw)
To: William Roberts; +Cc: gcc-help
Hi,
on 2021/1/4 上午2:53, William Roberts via Gcc-help wrote:
> When I compile the below program, I expect the -Wtype-limits warning
> to be displayed for all 4 if conditions, but I only get uint8_t and
> uint16_t conditions, why?
This is a known issue, see below PR.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89126
BR,
Kewen
>
> #include <stdint.h>
> #include <stdlib.h>
>
> int main(int argc, char *argv[]) {
> (void)argv;
>
> uint8_t i8 = (uint8_t) argc;
> uint16_t i16 = (uint16_t) argc;
> uint32_t i32 = (uint32_t) argc;
> uint64_t i64 = (uint64_t) argc;
>
> if (i8 > UINT8_MAX) {
> return 8;
> }
> if (i16 > UINT16_MAX) {
> return 16;
> }
> if (i32 > UINT32_MAX) {
> return 32;
> }
> if (i64 > UINT64_MAX) {
> return 64;
> }
>
> return 0;
> }
>
> gcc -Wall -Wextra -o go a.c
> a.c: In function ‘main’:
> a.c:13:9: warning: comparison is always false due to limited range of
> data type [-Wtype-limits]
> 13 | if (i8 > UINT8_MAX) {
> | ^
> a.c:17:10: warning: comparison is always false due to limited range of
> data type [-Wtype-limits]
> 17 | if (i16 > UINT16_MAX) {
>
> gcc --version
> gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
> Copyright (C) 2019 Free Software Foundation, Inc.
> This is free software; see the source for copying conditions. There is NO
> warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
>
> Thanks,
> Bill
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2021-01-05 3:47 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-03 18:53 Questions on -Wtype-limits William Roberts
2021-01-05 3:47 ` Kewen.Lin
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).