public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* using undeclared function returning bool results in wrong return value
@ 2021-02-17 21:05 Thanos Makatos
  2021-02-17 22:05 ` Martin Sebor
  2021-02-17 22:25 ` Jonathan Wakely
  0 siblings, 2 replies; 16+ messages in thread
From: Thanos Makatos @ 2021-02-17 21:05 UTC (permalink / raw)
  To: gcc

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). 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.

Here are the files and repro:

/* main.c */
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>

bool foo2(int);

void main(void) {
        assert(!foo2(0));
        assert(!foo1(0));
}

/* foo.c */
#include <stdbool.h>

bool foo1(int n) {
        return n == 1 || n == 2 || n == 4;
}

bool foo2(int n) {
        return foo1(n);
}

# gcc --version
gcc (Debian 8.3.0-6) 8.3.0
# gcc main.c foo.c -O
In file included from main.c:3:
main.c: In function 'main':
main.c:9:10: warning: implicit declaration of function 'foo1'; did you mean 'foo2'? [-Wimplicit-function-declaration]
  assert(!foo1(0));
          ^~~~
# ./a.out
a.out: main.c:9: main: Assertion `!foo1(0)' failed.
Aborted

I get the same behavior if I use char instead of bool.

The problem goes away if:
1. I declare foo1 in main.c (just like I do for foo2), or
2. compile w/o -O, or
3. use n == 3 instead of n == 4 in the return statement, or
4. use short or int instead of bool/char.

Am I causing undefined behavior because I'm not declaring foo1?

Please CC me in your response, I'm not subscribed to this list.

^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2021-03-10 13:52 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-17 21:05 using undeclared function returning bool results in wrong return value Thanos Makatos
2021-02-17 22:05 ` Martin Sebor
2021-02-17 22:25 ` Jonathan Wakely
2021-02-18 12:31   ` Florian Weimer
2021-02-18 15:57     ` David Brown
2021-02-19  8:45       ` Florian Weimer
2021-02-19  9:28         ` David Brown
2021-02-19 11:18           ` Jonathan Wakely
2021-02-19 11:20             ` Jonathan Wakely
2021-02-20 14:25             ` David Brown
2021-02-20 15:46               ` David Malcolm
2021-02-20 16:49                 ` David Brown
2021-02-23  2:26                   ` [PATCH] docs: add interactive vs batch distinction to UX guidelines David Malcolm
2021-03-10 13:52                     ` David Malcolm
2021-02-23  1:02                 ` using undeclared function returning bool results in wrong return value Martin Sebor
2021-02-23  7:47                   ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).