http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56908
Bug #: 56908
Summary: Spurious warning when XOR-ing uint8_t values
Classification: Unclassified
Product: gcc
Version: 4.7.2
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: [email protected]
ReportedBy: [email protected]
g++ -Wconversion displays what I believe is a spurious warning in my code:
warning: conversion to 'uint8_t {aka unsigned char}' from 'int' may alter its
value [-Wconversion]
-Wconversion is mandated by the project I'm working on. Since I'm striving for
warningless compilation, I looked into the function. Even after a careful
examination, I cannot find fault with it, nor an obvious way to get rid of the
warning without compromising the code's readability. The minimal example that
warns is:
#include <stdint.h>
#include <stddef.h>
void
xorblock(uint8_t* dest, const uint8_t* src, size_t len)
{
while (len--)
*dest++ ^= *src++;
}
Compiled with g++ -Wconversion a.cc, it prints the warning as:
a.cc: In function 'void xorblock(uint8_t*, const uint8_t*, size_t)':
a.cc:8:20: warning: conversion to 'uint8_t {aka unsigned char}' from 'int' may
alter its value [-Wconversion]
Both operands have the same type, which is appropriate for the calculation.
Casting the right-hand side of the compound assignment to either int, uint8_t,
or unsigned int fails to silence the warning. The warning does not occur with
gcc, only with g++.
The only way I found to remove the warning is to rewrite the assignment as a
non-compound assignment. But this precludes the use of the post-increment
operator and consequently makes the function harder to follow. (In this case
shorter is more readable, as *dest++ = *src++ is one of the most widely
understood C idioms.)
The warning happens with g++ 4.7 and 4.8, but not with g++ 4.6 series.