bug-mes
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-mes] MesCC assigns incorrect types to arithmetic expressions


From: Danny Milosavljevic
Subject: Re: [bug-mes] MesCC assigns incorrect types to arithmetic expressions
Date: Thu, 18 Jul 2019 22:55:27 +0200

#include <assert.h>

int main() {
        unsigned char a = 255U;
        unsigned int b = 1U;
        assert(sizeof(a) == 1U);
        assert(sizeof(a) != sizeof(b)); /* makes sure that this example 
requires promotion, otherwise something else could be wrong */
        assert(a + b == 256U); /* not 0U */
        return 0;
}

(With minor trickery, this test could be independent of the size of "a" - but 
it would make it more difficult to read for no good reason, so let's cross that 
bridge when we come to it)

What happens here:

"a+b" promotes both operands to unsigned int before adding them.  Therefore, 
the result is supposed to be 256.

However, when mescc uses the type of "a" for the result, then the value of 
"a+b" would be 0.
Worse, "==" would then judge that the value of "a+b" has type unsigned char but 
256U has type unsigned int, so it would promote the value of "a+b" again, to 
unsigned char, and then compare.
That is very bad because then it could even compare equal by accident (if 256U 
is cut to 0; unlikely but would be very bad), depending on the actual assembly 
emitted when promoting (as opposed to the judgements at compile time).

Attachment: pgpp0GFCfVqN5.pgp
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]