Menu

#3585 unsigned char and signed int in conditional expression

closed-fixed
None
Front-end
5
2023-05-15
2023-05-15
Frank H
No
/*
target: mcs51
command: sdcc --xram-loc 0x3000 bug0.c
version: 4.0.0
*/
#include <stdio.h>

int f(unsigned char u) {
    return u&4 ? u : -1; // returns negative int
    //return u&4 ? (int)u : -1; // works
}

void main(void) {
    printf("%d\n", f('\x9f'));
    for(;;) {}
}

I think this should print 159 (hex 9f) but it prints -97.
As shown in the commented out line, explicitly casting 'u' to int makes it work.
I'm no language lawyer but I think it should automatically convert 'u' to int because the other side (-1) is an int.

Related

Wiki: NGI0-Entrust-SDCC

Discussion

  • Philipp Klaus Krause

    sdcc 4.0.0 was released three years ago. Even since the most recent release, 4.2.0, a lot of type-related bugs have been fixed. Did you check if this bug still exists with a current snapshot or sdcc from trunk in svn?

     
  • Frank H

    Frank H - 2023-05-15

    sorry didn't realize that was so old. however behavior is the same in this snapshot:
    4.2.14 #14052 (Linux)

     
  • Philipp Klaus Krause

    Thanks. I can reproduce the issue on my Debian GNU/Linux testing system on amd64 (I checked for stm8, but since type bugs tend to be front-end issues they typically affect all ports the same).

     
  • Philipp Klaus Krause

    • Category: MCS51 --> Front-end
     
  • Philipp Klaus Krause

    • assigned_to: Philipp Klaus Krause
     
  • Philipp Klaus Krause

    • status: open --> closed-fixed
     
  • Philipp Klaus Krause

    Fixed in [r14055].

     

    Related

    Commit: [r14055]


Log in to post a comment.

MongoDB Logo MongoDB