This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Q.: inconsistent (?) warnings about functions called through non-compatible types


On Sun, Mar 29, 2015 at 3:26 PM, Andreas Schwab <schwab@linux-m68k.org> wrote:
> Godmar Back <godmar@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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]