https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89807
Bug ID: 89807
Summary: Incorrect -Wconversion warning when shifting uint32_t
with 24
Product: gcc
Version: 8.3.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: nbkolchin at gmail dot com
Target Milestone: ---
https://godbolt.org/z/x2vI6l
Sample code:
#include <stdint.h>
int test(uint32_t v)
{
uint8_t a = (v >> 24) & 0xFF; // ERROR: produces warning
uint8_t a2 = v >> 24; // ERROR: produces warning
uint8_t b = (v >> 16) & 0xFF; // OK
uint8_t c = (v >> 8) & 0xFF; // OK
uint8_t d = v & 0xFF; // OK
return a + a2 + b + c + d;
}
Compile with -Werror=conversion:
<source>: In function 'test':
<source>:5:17: error: conversion from 'uint32_t' {aka 'unsigned int'} to
'uint8_t' {aka 'unsigned char'} may change value [-Werror=conversion]
5 | uint8_t a = (v >> 24) & 0xFF;
| ^
<source>:6:18: error: conversion from 'uint32_t' {aka 'unsigned int'} to
'uint8_t' {aka 'unsigned char'} may change value [-Werror=conversion]
6 | uint8_t a2 = v >> 24;
| ^
cc1: some warnings being treated as errors
Compiler returned: 1
P.S. Clang compiles this code without warnings.