From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ciao.gmane.io (ciao.gmane.io [116.202.254.214]) by sourceware.org (Postfix) with ESMTPS id B3A673858D28 for ; Mon, 17 Jul 2023 12:45:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B3A673858D28 Authentication-Results: sourceware.org; dmarc=fail (p=quarantine dis=none) header.from=westcontrol.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=m.gmane-mx.org Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1qLNbH-0004lM-T4 for gcc-help@gcc.gnu.org; Mon, 17 Jul 2023 14:45:16 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: gcc-help@gcc.gnu.org From: David Brown Subject: Re: Question about declaring an array in the stack on runtime Date: Mon, 17 Jul 2023 14:45:11 +0200 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.0 Content-Language: en-GB In-Reply-To: X-Spam-Status: No, score=-0.4 required=5.0 tests=BAYES_00,FORGED_MUA_MOZILLA,HEADER_FROM_DIFFERENT_DOMAINS,KAM_DMARC_STATUS,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 15/07/2023 12:43, James R T via Gcc-help wrote: > Hi folks, > > 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. > I think you've had solid answers to your other points already, so you can see why it is UB. When you use the -O flags, you are not just enabling a selection of optimisation passes - you are also changing the code analysis passes. With -O0, relatively little analysis is done. This means that many warnings are not active at -O0. So it is quite normal for warnings like this to require -O1 or above to work, and some may need -O2 to work fully (I don't have details off-hand - I'm a long-term gcc user, not a gcc developer). Personally, I've never had any use for -O0 - I use -O2 usually and -O1 at a minimum, even for early checking of code, precisely because it allows far better static warnings.