On Sun, Mar 29, 2015 at 3:26 PM, Andreas Schwab <sch...@linux-m68k.org> wrote:
> Godmar Back <god...@gmail.com> writes:
>
>> ps: I would like to see the warning, of course, since casting a bool
>> returning function to an int returning function is undefined behavior.
>
> The cast itself is ok, the undefined behavior only occurs when calling
> it without casting back.
>

Thanks. I'm a bit confused then what constitutes defined & undefined behavior.

The actual situation I encountered is best described by this example:

--
/* Tested with gcc 4.4.7 and 4.8.2 -m32 */
#include <stdio.h>
#include <stdbool.h>

bool boolFunctionThatReturnsFalse() {
    // I'm faking this here, but a real 'bool' function could
    // place 0x100 in $eax when meaning to return false.
    register bool r asm("%eax");
    asm("mov $0x100, %eax");
    return r;
}

typedef int (*intFunction)(void);

int
main()
{
    if ( boolFunctionThatReturnsFalse() )
        printf("true\n");
    else
        printf("false\n");

    intFunction f = (intFunction) boolFunctionThatReturnsFalse;
    if ( f() )
        printf("true\n");
    else
        printf("false\n");
}
--

(I'm faking the return value of boolFunctionThatReturnsFalse here, but
I have observed this behavior in actual code.)

Is this defined or undefined behavior that my problem invokes?

 - Godmar

Reply via email to