From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x12d.google.com (mail-lf1-x12d.google.com [IPv6:2a00:1450:4864:20::12d]) by sourceware.org (Postfix) with ESMTPS id EF0423858D3C for ; Sat, 15 Jul 2023 10:43:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EF0423858D3C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-lf1-x12d.google.com with SMTP id 2adb3069b0e04-4fb96e2b573so4643830e87.3 for ; Sat, 15 Jul 2023 03:43:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689417827; x=1692009827; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=mhkJpkdfPmbC/ZhE4r+AIlg0+of4u76sb3xIK/RCUAg=; b=g0XyAfGhGFSgwmJ8jRfix9h8EiK2zjv6k9punrpTqurHPcICVpJ08dabiSatc5Wqov w1VZkAjgnqHPLZZaY7ixgrnJ47rjwuTIY/gnW05BuyoGqXIKtn/YNZCpApxMXz4Zbyrv UJceOdBe9+auEHzPK4dhOuwbz18n7/O5heXbS8UMlKSSJLZRpNZeCGjnjV1qaXEThTu1 Tqk6uHECB773ymWzOzHeQ6T5Ct+3j+nbAxM4yhuhUCzDnwSgTQNi+DCiJwiYC0QeLs1v plr57xflK8tkcxHcuxcZxHyPVO1pQhK6DisdCv8SUvJHUpnrZxFINHKspST8V8G7xwoA x7Gg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689417827; x=1692009827; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=mhkJpkdfPmbC/ZhE4r+AIlg0+of4u76sb3xIK/RCUAg=; b=brmCrprIpo+y4qdjkw0gAeg3et41YTH34EfnlHtEnslNxKgdi/v2cQIQ1OaYMReIQi yzNMGQkifnxU+a+X5f1pKhAb3exIC4P74tTlsu4LV7GMlnT76IF58xvQyeVwzP74x3vQ LhqvdkPZBK1Ee8/hVLhyxnytRWUBcNZQ8JCi3c+TmGi2g+M2OUSJfjC0VIZG4Mz1fA5e JSfbe/xg3Sl1NRL9VP6mHeHrcu0NQtj4nJCuxaL0mmVcVcDQK5rNW+F/U65ckU5dEIrq 3v1+Tb82ZxVfPDgh3+jLmq+xYRXXKL7kQz4q0iB+tE96MD8QANttHslB/BciTfKzDiq9 RL7Q== X-Gm-Message-State: ABy/qLa7ud69t5+xFTBXr33ZMlS6COZAeYLNs32ib3uYwHXSCmsGTfGT BDWR/8hQA1wBhV1tWyN5TLg7VdbDPXjPekLIrIQ0JGqrxR0= X-Google-Smtp-Source: APBJJlGg5krXu9CQqOqN6ypxwn9ClLl4Qulv84WnwSTox8o33uTp99PU5CX/aDh1nLh9POIPYDFet0M9tflUzUcXd7k= X-Received: by 2002:a2e:9c56:0:b0:2b8:36d4:7b0a with SMTP id t22-20020a2e9c56000000b002b836d47b0amr4394445ljj.29.1689417827140; Sat, 15 Jul 2023 03:43:47 -0700 (PDT) MIME-Version: 1.0 From: James R T Date: Sat, 15 Jul 2023 18:43:08 +0800 Message-ID: Subject: Question about declaring an array in the stack on runtime To: gcc-help@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=0.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Hi folks, I hope that this is the correct mailing list to ask this question. I have the following C code snippet: ```c #include 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