public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "hniksic at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/56908] New: Spurious warning when XOR-ing uint8_t values
Date: Wed, 10 Apr 2013 15:48:00 -0000	[thread overview]
Message-ID: <bug-56908-4@http.gcc.gnu.org/bugzilla/> (raw)


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: unassigned@gcc.gnu.org
        ReportedBy: hniksic@gmail.com


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.


             reply	other threads:[~2013-04-10 15:48 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-10 15:48 hniksic at gmail dot com [this message]
2014-07-09 22:43 ` [Bug c++/56908] " manu at gcc dot gnu.org
2024-04-05 23:10 ` pinskia at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-56908-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).