public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: James R T <jamestiotio@gmail.com>
To: gcc-help@gcc.gnu.org
Subject: Question about declaring an array in the stack on runtime
Date: Sat, 15 Jul 2023 18:43:08 +0800	[thread overview]
Message-ID: <CAA_Li+tJ3DW_65naXfZKHERJpJExbdfwsoJDzQDXYBgiEvanKA@mail.gmail.com> (raw)

Hi folks,

I hope that this is the correct mailing list to ask this question.

I have the following C code snippet:

```c
#include <stdio.h>

int main() {
    unsigned int* arr;
    int some_var = 7;

    if (some_var == 7) {
        arr = (unsigned int[7]){9, 10, 11, 12, 13, 14, 15};
    }

    printf("Value of arr:\n");
    for (unsigned int i = 0; i < 7; i++) {
        printf("%u ", arr[i]);
    }

    return 0;
}
```

I have included the relevant Godbolt link here: https://godbolt.org/z/b4rbn6eGT

I have a few questions related to this code snippet:

1. Is the conditional assignment to `arr` considered undefined
behavior? If it is, which exact clause of the C standard does this
code snippet violate and why? As seen in the Godbolt link, there is
different behavior between GCC and Clang (only GCC `-O1` and above
prints garbage values) which made me suspect that this is UB.

2. Regardless of whether this is UB or not, is it possible for GCC to
also output a warning in `-O0` as in `-O2`? If the behavior changes
across different optimization levels, it seems that it's worth a
warning or two. It can be a different warning instead of
`-Wdangling-pointer` since looking at the produced assembly code, GCC
seems to simply optimize out the whole conditional assignment block in
`-O2`. If it is UB, I understand that it is impossible to catch all
UB, but I am just checking on whether it is possible to catch this
specific one from GCC's perspective. Just FYI, I have also tried using
`-fsanitize=address` and `-fsanitize=undefined` and it seems that
AddressSanitizer would throw a `stack-use-after-scope` error in GCC if
`-fsanitize=address` is specified for both `-O0` and `-O2`, but not in
Clang. `-fsanitize=undefined` does not seem to be able to detect
anything.

If the GCC maintainers consider this an acceptable proposal to add the
warning, I am also willing to post a bug report and develop the
corresponding patch for it, although I would appreciate some guidance
since I am not very familiar with GCC's codebase.

Looking forward to your reply and have a great day ahead!

Best regards,
James Raphael Tiovalen

             reply	other threads:[~2023-07-15 10:43 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-15 10:43 James R T [this message]
2023-07-15 10:49 ` John Scott
2023-07-15 11:19 ` Xi Ruoyao
2023-07-15 12:57   ` James R T
2023-07-17 12:45 ` David Brown

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAA_Li+tJ3DW_65naXfZKHERJpJExbdfwsoJDzQDXYBgiEvanKA@mail.gmail.com \
    --to=jamestiotio@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).