public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/45034]  New: "safe" conversion from unsigned to signed char gives broken code
@ 2010-07-22 21:13 mikpe at it dot uu dot se
  2010-07-22 21:14 ` [Bug c/45034] " mikpe at it dot uu dot se
                   ` (18 more replies)
  0 siblings, 19 replies; 22+ messages in thread
From: mikpe at it dot uu dot se @ 2010-07-22 21:13 UTC (permalink / raw)
  To: gcc-bugs

The following program (which I'll also attach later) gives unexpected results,
where signed char values are passed as non properly sign-extended ints:

> cat char-neg.c
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

static void fixnum_neg(signed char x, signed char *py, int *pv)
{
    unsigned char ux, uy;

    ux = (unsigned char)x;
    uy = -ux;
    *py = (uy <= 127) ? (signed char)uy : (-(signed char)(255 - uy) - 1);
    *pv = (x == -128) ? 1 : 0;
}

void __attribute__((noinline)) foo(int x, int y, int v)
{
    printf("test_neg: -(%d) => (%d, %d)\n", x, y, v);
    if (y < -128 || y > 127)
        abort();
}

int test_neg(void)
{
    signed char x, y;
    int v, err;

    err = 0;
    x = -128;
    for (;;) {
        fixnum_neg(x, &y, &v);
        foo((int)x, (int)y, v);
        if ((v && x != -128) || (!v && x == -128))
            ++err;
        if (x == 127)
            break;
        ++x;
    }
    return err;
}

int main(void)
{
    if (CHAR_BIT != 8
        || SCHAR_MIN != -128 || SCHAR_MAX != 127
        || UCHAR_MAX != 255)
        abort();
    if (test_neg() != 0)
        abort();
    return 0;
}
> gcc -O2 -Wall -Wextra char-neg.c ; ./a.out
test_neg: -(-128) => (-128, 1)
test_neg: -(-127) => (-129, 0)
Abort

The abort shows that the `signed char' variable y is incorrectly extended to
int when passed to foo().

Passing -fwrapv eliminates the failure.  Maybe I've been staring at this for
too long, but I can't see any signed overflow in this code.

The problem occurs with gcc 4.6/4.5/4.4/4.3/4.2, but not with 4.1 or older.

The program comes from some code which attempts to emulate machine-level
integer arithmetic and condition code settings.  To validate the condition code
logic I used exhaustive testing on a smaller integer type (signed char),  but
that broke as shown above.  The assignment to *py in fixnum_neg() is one of
several attempts to cast from unsigned to signed char without (apparently)
triggering undefined behaviour due to signed overflow; other failed attempts
have included plain casts, assignment via a union, and memcpy() via a local
signed char temporary.

gcc was configured --with-gmp=... --with-mpfr=... --with-mpc=...
--disable-plugin --disable-lto --disable-nls --enable-threads=posix
--enable-checking=release --disable-libmudflap --enable-languages=c


-- 
           Summary: "safe" conversion from unsigned to signed char gives
                    broken code
           Product: gcc
           Version: 4.6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: mikpe at it dot uu dot se
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45034


^ permalink raw reply	[flat|nested] 22+ messages in thread
[parent not found: <bug-45034-4@http.gcc.gnu.org/bugzilla/>]

end of thread, other threads:[~2011-06-27 13:19 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-07-22 21:13 [Bug c/45034] New: "safe" conversion from unsigned to signed char gives broken code mikpe at it dot uu dot se
2010-07-22 21:14 ` [Bug c/45034] " mikpe at it dot uu dot se
2010-07-22 21:26 ` [Bug tree-optimization/45034] " pinskia at gcc dot gnu dot org
2010-07-23  4:14 ` hjl dot tools at gmail dot com
2010-07-23  8:52 ` [Bug tree-optimization/45034] [4.3/4.4/4.5/4.6 Regression] " rguenth at gcc dot gnu dot org
2010-07-24 18:47 ` mikpe at it dot uu dot se
2010-07-25 10:56 ` mikpe at it dot uu dot se
2010-07-26 14:47 ` rakdver at gcc dot gnu dot org
2010-07-27 22:18 ` mikpe at it dot uu dot se
2010-07-27 22:43 ` joseph at codesourcery dot com
2010-07-27 23:09 ` rakdver at kam dot mff dot cuni dot cz
2010-07-28  9:32 ` rguenth at gcc dot gnu dot org
2010-07-28 12:24 ` rguenth at gcc dot gnu dot org
2010-07-28 14:14 ` mikpe at it dot uu dot se
2010-07-28 15:38 ` mikpe at it dot uu dot se
2010-07-28 23:32 ` mikpe at it dot uu dot se
2010-07-29 11:00 ` [Bug tree-optimization/45034] [4.3/4.4/4.5 " rguenth at gcc dot gnu dot org
2010-07-29 11:00 ` [Bug tree-optimization/45034] [4.3/4.4/4.5/4.6 " rguenth at gcc dot gnu dot org
2010-08-08 15:50 ` [Bug tree-optimization/45034] [4.3/4.4/4.5 " rguenth at gcc dot gnu dot org
2010-08-08 15:51 ` [Bug tree-optimization/45034] [4.3/4.4 " rguenth at gcc dot gnu dot org
     [not found] <bug-45034-4@http.gcc.gnu.org/bugzilla/>
2011-06-27 10:37 ` rguenth at gcc dot gnu.org
2011-06-27 13:19 ` rguenth at gcc dot gnu.org

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).