https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92857

            Bug ID: 92857
           Summary: -Wsign-conversion flag issues false positives for
                    expression using typedef'ed unsigned types
           Product: gcc
           Version: 5.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: joshua.a.saxby at gmail dot com
  Target Milestone: ---

Created attachment 47439
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=47439&action=edit
Full pre-processed source of the minimal code sample required to produce the
erroneous behaviour

When using typedef'ed unsigned types in an arithmetic expression and compiling
with GCC's -Wsign-conversion flag, GCC produces a false positive warning about
sign conversion as a result of the expression.

Here is a minimal code sample (gcc_sign_conversion_warning_bug.c):

#include <stddef.h>
#include <stdint.h>

typedef uint8_t MyUnsigned;

int main(void) {
    MyUnsigned foo = 3U;
    MyUnsigned bar = 2U;
    // conversion to ‘unsigned int’ from ‘int’ may change the sign of the
result
    size_t baz = foo - bar + 1U;
    //                     ^
    return (int)baz;
}

(the full preprocessed source is also attached as a file to this bug report)

Essential information:

- GCC version 5.4.0 20160609

- Running on Ubuntu 16.04

- GCC build-time options: ../src/configure -v --with-pkgversion='Ubuntu
5.4.0-6ubuntu1~16.04.12' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs
--enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr
--program-suffix=-5 --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--with-default-libstdcxx-abi=new --enable-gnu-unique-object
--disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib
--disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo
--with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home
--with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64
--with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64
--with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar
--enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686
--with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib
--with-tune=generic --enable-checking=release --build=x86_64-linux-gnu
--host=x86_64-linux-gnu --target=x86_64-linux-gnu

- Command line that triggers the bug: gcc -v -save-temps -std=gnu99
-Wsign-conversion gcc_sign_conversion_warning_bug.c

- The erroneous warning message produced is: 
gcc_sign_conversion_warning_bug.c:16:28: warning: conversion to ‘unsigned int’
from ‘int’ may change the sign of the result [-Wsign-conversion]
     size_t baz = foo - bar + 1U;
                            ^
Additional information:

I have used the online Compiler Explorer at godbolt.org to verify that this
issue is also present on all GCC versions from 5.4 through 9.2 inclusive. If it
is useful to anyone, there is a link to this online sample here:
https://godbolt.org/z/-2jSYx

I have also tested this on the Clang compiler on the same system used to verify
the GCC sample and Clang does not issue a warning (same warning flag was used).

Reply via email to