On Wed, 17 Feb 2021, 21:12 Thanos Makatos via Gcc, <gcc@gcc.gnu.org> wrote:

> I run into a problem that I'm not sure whether it's a bug in my program
> (most likely) or something wrong with GCC (highly unlikely, I know, hence
> why I haven't sent this to gcc-bugs).



This is the wrong list, please use gcc-help for questions like this.

The problem is using a function that returns a bool, defined in another
> source file without a declaration, and the program compiled with -O. In my
> test, function foo1 returns true while it should be returning false.
>

As Martin explained, the compiler assumes that an implicitly-declared
function returns an int. Your function initializes a single byte with its
return value and the caller reads 4 bytes, including 3 bytes of
uninitialized garbage. If that garbage happens to contains any non-zero
bits, you get a non-zero int and converting that to bool yields true.

Declare your functions. Don't ignore warnings.

Reply via email to