From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 4EC443858C62; Mon, 27 Mar 2023 21:03:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4EC443858C62 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1679950995; bh=nG5Up+1U4YD470tqJSENVnsljyW7bbm+SKdzqfYkkWE=; h=From:To:Subject:Date:In-Reply-To:References:From; b=e001RwsZTDHNvvSrnhVGFuudgkgXQ+LVisbMtOO/ys/7bDc7WRRdILERLL6NBbqOb tmvhRtUHPh7yGSZqaP+o29REvm1YnJZA4sBg/TjYlL99hHGY9Ntklf41c8I82kLBXn 8tH4MzQN17Q3TbEg3S25i7MDfH15gbZGsIu8rO+Y= From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/109305] Allocator copy in basic_string::operator= Date: Mon, 27 Mar 2023 21:03:14 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 12.1.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: WAITING 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_status everconfirmed cf_reconfirmed_on 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=3D109305 Jonathan Wakely changed: What |Removed |Added ---------------------------------------------------------------------------- Status|UNCONFIRMED |WAITING Ever confirmed|0 |1 Last reconfirmed| |2023-03-27 --- Comment #3 from Jonathan Wakely --- (In reply to Marc-Andr=C3=A9 Laverdi=C3=A8re from comment #0) > Created attachment 54773 [details] > Self-contained preprocessed reproducer >=20 > In basic_string& operator=3D(const basic_string& __str), I noticed this l= ine: >=20 > auto __alloc =3D __str._M_get_allocator(); >=20 > This creates a local copy of the allocator object. I doubt this is > intentional, but I could be wrong. It's completely intentional. The function needs to allocate new storage, so= it needs a non-const allocator that compares equal to __str.get_allocator(). As the comment hints at, the order of the operations is important. We don't want to update the allocator in *this until after we've allocated the stora= ge, because that could throw. So we make a copy of the allocator, use it to allocate memory, then only if that was successful we replace the allocator = in *this. Why do you think this local copy is a problem?=