From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id E173E398B43C; Tue, 9 Feb 2021 11:33:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E173E398B43C From: "cvs-commit at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/98465] [11 Regression] Bogus -Wstringop-overread with -std=gnu++20 -O2 and std::string::insert Date: Tue, 09 Feb 2021 11:33:34 +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: 11.0 X-Bugzilla-Keywords: alias, diagnostic, missed-optimization, patch X-Bugzilla-Severity: normal X-Bugzilla-Who: cvs-commit at gcc dot gnu.org X-Bugzilla-Status: ASSIGNED X-Bugzilla-Resolution: X-Bugzilla-Priority: P2 X-Bugzilla-Assigned-To: msebor at gcc dot gnu.org X-Bugzilla-Target-Milestone: 11.0 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 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 09 Feb 2021 11:33:35 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D98465 --- Comment #30 from CVS Commits --- The master branch has been updated by Jakub Jelinek : https://gcc.gnu.org/g:e14ea108faa6eba6a60a45ff0ca3099ce6ae45c2 commit r11-7146-ge14ea108faa6eba6a60a45ff0ca3099ce6ae45c2 Author: Jakub Jelinek Date: Tue Feb 9 12:32:43 2021 +0100 string: Add a workaround for -Wstringop-overread false positives [PR984= 65] In the PR there are several possibilities how to improve _M_disjunct at least in certain cases so that the compiler can figure out at least in = some cases where __s is provably disjunct from _M_data() ... _M_data() + this->size() but it is probably GCC 12 material. The false positive warning is on this particular copy, which is done for non-disjunct pointers when __len2 > __len1 and the __s >=3D __p + __len= 1, i.e. __s used to point to the characters moved through _S_move a few li= nes earlier by __len2 - __len1 characters up to make space. That is why the _S_copy source is __s + __len2 - __len1. Unfortunately, when the compi= ler can't prove objects are disjunct, that copying from __s + __len2 - __le= n1 of __len2 characters can very well mean accessing characters the source object (if it is not disjunct) provably can't have. The following patch works around that by making the _S_copy be a __p ba= sed pointer instead of __s based pointer. __s + __len2 - __len1 and __p + (__s - __p) + (__len2 - __len1) have the same value and the latter may seem to be uselessly longer, but it seems at least currently in GIMPLE we keep it that way and so th= at is what the warning code during expansion will see, and only actually optimize it to __s + __len2 - __len1 during RTL when we lose information on what is a pointer and what is a mere offset with the same mode. So, in the end we emit exactly the same assembly, just without the false positive warning. 2021-02-09 Jakub Jelinek PR middle-end/98465 * include/bits/basic_string.tcc (basic_string::_M_replace): When __s points to the characters moved by earlier _S_move, compute the source address using expression based on the __p pointer rather than _= _s pointer. * g++.dg/warn/Wstringop-overread-1.C: New test.=