From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id AD0BC3858D28; Tue, 11 Apr 2023 07:43:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AD0BC3858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1681199030; bh=ls0NyjXP6+bgLhn5rSe+RYsGdYf5p+wHz/SlDleyfKE=; h=From:To:Subject:Date:From; b=CrEYp7929OQawrVGJizJgSIm1M8E/EfWAQPkgCi6L9ZILMuTCV23MOj073l1mQZx+ Xb/tXX6A3ziOV6IqMjJMZeiJP7jyGKwHtCF5RVvA3FcqgnMBcbmMVnQ/uoXMmwV2BX y3pCDZdhqEYxvmYAxf5fzvVMS8oev+JfDyjgWU8Y= From: "johannes.kellner at wandelbots dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/109470] New: unexpected const & behavior Date: Tue, 11 Apr 2023 07:43:50 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: johannes.kellner at wandelbots dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D109470 Bug ID: 109470 Summary: unexpected const & behavior Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: johannes.kellner at wandelbots dot com Target Milestone: --- Hello, I am filing this ticket, as I had source code, where this funny Min template created a seg fault when compiling release (optimized code). I changed the this to an example - to check the behavior and the first to s= how is this UNEXPECTED/INVALID? behavior: #include #include template const TYPE& Min(const TYPE& lhs, const TYPE& rhs) { if (lhs < rhs) return lhs; return rhs; } int main() { size_t lenght =3D 16; const int MAX =3D 32; const int& dst =3D Min(MAX, (int)lenght); assert(dst <=3D MAX); return dst; } Expected behavior: return 16 If compiled with gcc 12.2 and -O0: returns 16 If compiled with gcc 12.2 and -O1: returns 0 UNEXPECTED !!! I assume, that something with the extension of lifetime,=20 for the result of `(int)lenght` fails in the optimized code. I found this unexpected behavior, because this Min function was used to calculate the size for a memcpy. And if compiled with optimization, dst bec= ame invalid (very large), just after the call to Min. We tested with gcc10 but have the same problem with gcc12. Please feel free to contact me for details.=