From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 9C67D3858D28 for ; Sun, 30 Jan 2022 10:41:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9C67D3858D28 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-625-jus2Jn-7NWCvnL91Esn0vg-1; Sun, 30 Jan 2022 05:41:50 -0500 X-MC-Unique: jus2Jn-7NWCvnL91Esn0vg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D79B21006AA0; Sun, 30 Jan 2022 10:41:49 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.125]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 716F01090055; Sun, 30 Jan 2022 10:41:49 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.16.1/8.16.1) with ESMTPS id 20UAfk5c3412218 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Sun, 30 Jan 2022 11:41:46 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 20UAfjc33412217; Sun, 30 Jan 2022 11:41:45 +0100 Date: Sun, 30 Jan 2022 11:41:45 +0100 From: Jakub Jelinek To: Theodore Papadopoulo Cc: gcc@gcc.gnu.org Subject: Re: Enquiry Message-ID: <20220130104145.GC2646553@tucnak> Reply-To: Jakub Jelinek References: MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-5.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 30 Jan 2022 10:41:54 -0000 On Sun, Jan 30, 2022 at 10:47:41AM +0100, Theodore Papadopoulo wrote: > Before creating a bug report, I want to check with the GCC community (all > the more that checking that the problem has not yet been reported is > complicated at leat for me). > > The following (admitedly buggy) program generates a segmentation violation > on fedora 35 (this is with g++ 11.2.1 20211203 (Red Hat 11.2.1-7) (GCC)) > when compiled with -O3 (other versions replacing unisgned by std::string may > trigger the exception instead of the segv) > > bool assert_sthg(const unsigned s) { >     if (s==123) >         throw 1; > } > > int main() { >     assert_sthg(0); >     return 0; > } > > When compiling, we indeed get a warning: > > test.C:4:1: warning: control reaches end of non-void function > [-Wreturn-type] > > I can well understand that the program being buggy that the optimizer is > allowed to do anything including the observed segmentation violation. > Yet the result is quite surprising.... Undefined behavior can have any kind of surprising behavior. > The question is, in that case, wouldn't it be better to turn the warning > into an error at -O3 ? No, it can't be an error by default, it is undefined behavior only at runtime, if you never call the function or always call it with assert_sthg(123), then the program can be valid. Jakub