From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 27ADD3858D28; Mon, 24 Apr 2023 19:50:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 27ADD3858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682365805; bh=JqwMlku7y2b4ZC/teR9CA341PJVl8Bno5ELDHS3UjQ0=; h=From:To:Subject:Date:In-Reply-To:References:From; b=ISZLUXgeuqXLL40Atu7y9wsmKbDegjD4RRCYikktneNXVnYnKu581/hBW9SPaMi18 XVVRpOa+7F6obRQr+FshIJWCK2eBKT9hQT2a1xu+FOJ4E8OVAWPtLTfWLZtNndhgxl OwNb1lXvaDbZlXimEoOw8iLYOkjaiDJSuiw+af0Q= From: "pinskia at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/109609] [12/13/14 Regression] Invalid strncpy/strncat optimization in GCC 12 Date: Mon, 24 Apr 2023 19:50:04 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: middle-end X-Bugzilla-Version: 12.2.0 X-Bugzilla-Keywords: wrong-code X-Bugzilla-Severity: normal X-Bugzilla-Who: pinskia at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P2 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: 12.3 X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: 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=3D109609 --- Comment #4 from Andrew Pinski --- Oh the problem is related to tail calls: strncpy (&dst, ptr_30, 23); [tail call] That should not be marked as a tail call as buf is still alive during the c= all of strncpy. Simple workaround, add asm("":::"memory"); Right before the end of invert. Or use -fno-optimize-sibling-calls Self contained testcase: ``` #define N 23 #define MAX_LEN 13 char dst[N + 1]; void invert(const char *id) { char buf[MAX_LEN]; char *ptr =3D buf + sizeof(buf); // start from the end of buf *(--ptr) =3D '\0'; // terminate string while (*id && ptr > buf) { *(--ptr) =3D *(id++); // copy id backwards } __builtin_strncpy(dst, ptr, N); // copy ptr/buf to dst // asm("":::"memory"); // This "fixes" the issue. } int main() { invert("abcde"); if (strcmp(dst, "edcba")) __builtin_abort(); } ```=