public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Is this program well-defined?
@ 2021-11-13  9:20 Haoxin Tu
  2021-11-13  9:47 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: Haoxin Tu @ 2021-11-13  9:20 UTC (permalink / raw)
  To: gcc

Hi all.

I hope this is the right place to ask the question.

I found a test case that makes almost all (GCC-5.1 above) versions
(may) produce the wrong code at -Os.

So, I am confused here whether this test case is well-defined or not.

Here is the program (small.c):
```
#include <stdint.h>
#include <stdio.h>
int16_t a;
uint8_t func_2();
static int32_t func_1() { func_2();}
uint8_t func_2(int p) {
int32_t b[4][9] = {{0}, {0}, {0}, 1L};
for (;; a ++) {
for (;;) {
if (p)
break;
return p;
}
p = b[3][1];
}
return 0;
}
int main() {
func_1();
printf("%d\n", a);
return 0;
}
```
```
$gcc -O1 small.c ; ./a.out
0
$gcc -Os small.c ; ./a.out
1
```

I tested it on today's trunk version of GCC, please also check here for
more results for different versions: https://godbolt.org/z/3nfv9vaY8

My confusion is that whether the called function "func_2()" inside the
"func_1" is defined or not.

Can anyone help me to figure it out?

If it's a well-defined program, I will open a bug report.

Thank you very much and looking forward to hearing from you!


Best regards,
Haoxin

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

* Re: Is this program well-defined?
  2021-11-13  9:20 Is this program well-defined? Haoxin Tu
@ 2021-11-13  9:47 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2021-11-13  9:47 UTC (permalink / raw)
  To: Haoxin Tu; +Cc: gcc

On Sat, 13 Nov 2021, 09:21 Haoxin Tu wrote:
>
> Hi all.
>
> I hope this is the right place to ask the question.


It's not IMHO. Questions like "is this a bug?" generally belong on the
gcc-help list, or just straight to bugzilla.

>
> My confusion is that whether the called function "func_2()" inside the
> "func_1" is defined or not.
>
> Can anyone help me to figure it out?


I think it's undefined, due to C17 6.5.2.2 p6:


If the expression that denotes the called function has a type that
does not include a prototype, the
integer promotions are performed on each argument, and arguments that
have type float are
promoted to double. These are called the default argument promotions.
If the number of arguments
does not equal the number of parameters, the behavior is undefined.

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

end of thread, other threads:[~2021-11-13  9:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-13  9:20 Is this program well-defined? Haoxin Tu
2021-11-13  9: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).