Menu

MCPP bug: conditional operator expression

beamer145
2010-07-05
2019-11-05
  • beamer145

    beamer145 - 2010-07-05

    There is a bug in the evaluation of the conditional operator when it is used as the right hand side argument of an == or != test in a #if.
    I can't see any logic in its behaviour, see code examples….

    Code examples:
    #if  ( ( 1 ? 1 : 0 ) == 10 )
    #pragma message_test1NOK
    #else
    #pragma message_test1OK
    #endif

    #if  (10 == ( 1 ? 1 : 0 ) )
    #pragma message_test2NOK
    #else
    #pragma message_test2OK
    #endif

    #if  (10 == ( 1 ? 10 : 0 ) )
    #pragma message_test3OK
    #else
    #pragma message_test3NOK
    #endif

    Output:
    pragma 'message_test1OK'
    pragma 'message_test2NOK'
    pragma 'message_test3NOK'

     
  • Eric Smith

    Eric Smith - 2019-11-05

    Just in case anyone else finds this via a search engine, the bug is a typo in the eval_signed function in mcpp_eval.c. The line:

       v1 = (--*valpp)->val ? v1 : v2;
    

    should read

       valp--; v1 = (valp->val) ? v1 : v2;
    

    because later in the code the value of valpp is updated from valp. The eval_unsigned function has this right already.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.