incorrect signed/unsigned conversions for short and char

Bug #237134 reported by eben
2
Affects Status Importance Assigned to Milestone
gcc-4.2 (Ubuntu)
New
Undecided
Unassigned

Bug Description

Binary package hint: gcc-4.2

Release:

Description: Ubuntu 8.04
Release: 8.04

Version information:

gcc-4.2:
  Installed: 4.2.3-2ubuntu7
  Candidate: 4.2.3-2ubuntu7
  Version table:
 *** 4.2.3-2ubuntu7 0
        500 http://us.archive.ubuntu.com hardy/main Packages
        100 /var/lib/dpkg/status

Description:

Signed/unsigned comparisons yield unexpected results in some cases. C code that exercises this is appears below. The expected result (tested on previous versions of gcc) is that it will print the gcc version and nothing else.

#include <stdio.h>

int main() {

    long l = 0L;
    unsigned long ul = 0x80000000L;

    int i = 0;
    unsigned int ui = 0x80000000;

    short s = 0;
    unsigned short us = 0x8000;

    char c = 0;
    unsigned char uc = 0x80;

    printf("Using gcc version %s\n\n", __VERSION__);

    if (l > ul)
        printf("long 0x%08lx > unsigned long 0x%08lx evaluates to true\n", l, ul);

    if (i > ui)
        printf("int 0x%08x > unsigned int 0x%08x evaluates to true\n", i, ui);

    if (s > us)
        printf("short 0x%04x > unsigned short 0x%04x evaluates to true\n", s, us);

    if (c > uc)
        printf("char 0x%02x > unsigned char 0x%02x evaluates to true\n", c, uc);

    if (((long) 0L) > ((unsigned long) 0x80000000L))
        printf("((long) 0L) > ((unsigned long) 0x80000000L) evaluates to true\n");

    if (((int) 0) > ((unsigned char) 0x8000000))
        printf("((int) 0) > ((unsigned int) 0x8000000) evaluates to true\n");

    if (((char) 0) > ((unsigned short) 0x8000))
        printf("((short) 0) > ((unsigned short) 0x8000) evaluates to true\n");

    if (((char) 0) > ((unsigned char) 0x80))
        printf("((char) 0) > ((unsigned char) 0x80) evaluates to true\n");

    return 0;
}

$ gcc -Wall test.c -o test
$ ./test

Using gcc version 4.2.3 (Ubuntu 4.2.3-2ubuntu7)

short 0x0000 > unsigned short 0x8000 evaluates to true
char 0x00 > unsigned char 0x80 evaluates to true

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.