Menu

#335 Not completely impl.warnings with neg.vals in unsigned exprs

open-invalid
nobody
None
5
2011-09-16
2011-09-13
No

/* Subj: Not completely implemented warnings with negative values in unsigned expressions.

Please look to this code: */

unsigned char Sub_6900 (void)
{
return -1; /* Though this is wrong assignment too, here is no warnings */
/* Compiler uses 255 for this assignment */
}

int main (void)
{
unsigned char UChar8;

UChar8 = -1; /* Though this is wrong assignment too, here is no warnings */
/* Compiler uses 255 for this assignment */

if (Sub_6900() == (-1)) {} /* warning 94: comparison is always false.
/* COMPILER DOES NOT USE -1 as 255 for this comparison */
/* WHY compiler not warnings in other cases? */
/* If change this as: */
if (Sub_6900() == 255) {} /* Here is really no problems */

/* Why -1 in some cases perceived as 255, and in other cases it is wrong value? */
/* Need to extend warnings to any other negative values,
at least with known in compile-time values.
Or to use -1 as 255 in any unsigned expressions without any warnings.
Personally I propose first variant. It looks more safe */
return 0;
}

To compile:
sdcc -mz80 --code-loc 26000 --data-loc 0xF800 --no-std-crt0 --opt-code-size --max-allocs-per-node 1 --funsigned-char --disable-warning 85 -L z80 Basic.c

SDCC : mcs51/gbz80/z80/ds390/pic16/pic14/TININative/ds400/hc08 3.0.4 #6849 (Sep 13 2011) (MINGW32)

P.S. My e-mail is allot [(at)] bk.ru

Discussion

  • Raphael Neider

    Raphael Neider - 2011-09-13

    I did not consult the standard, but I cannot see a bug here: The assignments of -1 truncate the literal to the target type -- just as assigning an int variable to a char variable should work without warning (Java warns you here, C does not).
    The comparison is a different story, as here both operands are generally promoted to int -- which yields a left-hand side of anywhere between 0 and 255 and a right hand side of -1, which is correctly warned about as never being identical.
    You can use (Sub_6900() == (unsigned char)-1) to get this right.

     
  • Raphael Neider

    Raphael Neider - 2011-09-13
    • status: open --> pending-invalid
     
  • Oleg N. Cher

    Oleg N. Cher - 2011-09-14
    • status: pending-invalid --> open-invalid
     
  • Oleg N. Cher

    Oleg N. Cher - 2011-09-14

    I comply with your argument. The standard ANSI C probably does not specify the corresponding to message in an effort assign the negative number to unsigned variable.
    However in practice this obstructs programming, allowing be included spare bugs.

    So see here.
    (I long could not understand where is bug):

    typedef char BOOLEAN; /* It is signed char */
    #define TRUE (-1)
    #define FALSE 0

    ...
    if (Sub_6900() == TRUE) {} // Here we have received warning 94. Needs:
    if (Sub_6900() == (unsigned)TRUE) {} // but

    unsigned char u = (unsigned)(-1) // here (unsigned) not without fall
    ...

    And this is possibility to have code:

    unsigned char u;

    u = -1; if( u == -1 ) ... This is not very well, eh?

    So I offer to do warning more strictly, than it is ordered in standard. For more safety and speedups in finding the bugs. SDCC is designed for dev for microcontrollers, isn't it?
    And that is a medical equipment, for instance, warmhearted facilitators, exact measuring instruments, possible, space research. Here necessary accuracy and safety.

    (You see as big and complex SDCC, and as much bugs is present. This occurs somewhat because of chosen instrument - a dangerous language C).
    But let this will stay, as you wish.

     
  • Maarten Brock

    Maarten Brock - 2011-09-16
    • labels: 101552 -->
     
  • Maarten Brock

    Maarten Brock - 2011-09-16

    This is no bug. But you seem to request more warnings, so I'll file it under feature requests.

     

Log in to post a comment.