From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 921173858434; Tue, 17 May 2022 20:44:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 921173858434 From: "ronghua at iu dot edu" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/105115] `demangle_const` causes infinite recursion and stack overflow Date: Tue, 17 May 2022 20:44:54 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed 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: ronghua at iu dot edu 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: 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, 17 May 2022 20:44:54 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D105115 --- Comment #2 from ronghua at iu dot edu --- The simplified PoC: $ echo _RYAbB2_ | ./binutils/cxxfilt Segmentation fault Root Cause Analysis: 1. In `rust_demangle_callback`, "_R" at beginning causes `rdm.version =3D=3D 0`[1][2], so `demangle_path` in the else-branch is called[3] with `rdm.sym = =3D=3D "YAbB2_"`, with `rdm.next` field initialized to 0. 2. Next char 'Y' causes `demangle_type` to be called[4] in a switch-case branch, with `rdm.next =3D=3D 1`. 3. Next char 'A' causes `demangle_type` to be called first[5] to consume ch= ar 'b', then `demangle_const` is called[6] with `rdm.next =3D=3D 3`. 4. Next char 'B' is consumed[7], and "2_" is parsed as `integer_62`[8] to be value 3, and this vlaue is assigned to `rdm.next`, then `demangle_const` is called recursively[9]. However, currently the situation is `rdm.next =3D=3D= 3` again, which is exactly same as last call to `demangle_const`, and this cau= ses infinite recursion. [1] https://github.com/bminor/binutils-gdb/blob/626d0e40e55c35a4f143b70def49873= 4e8ed3c2a/libiberty/rust-demangle.c#L1356 [2] https://github.com/bminor/binutils-gdb/blob/626d0e40e55c35a4f143b70def49873= 4e8ed3c2a/libiberty/rust-demangle.c#L1362 [3] https://github.com/bminor/binutils-gdb/blob/626d0e40e55c35a4f143b70def49873= 4e8ed3c2a/libiberty/rust-demangle.c#L1453 [4] https://github.com/bminor/binutils-gdb/blob/626d0e40e55c35a4f143b70def49873= 4e8ed3c2a/libiberty/rust-demangle.c#L759 [5] https://github.com/bminor/binutils-gdb/blob/626d0e40e55c35a4f143b70def49873= 4e8ed3c2a/libiberty/rust-demangle.c#L934 [6] https://github.com/bminor/binutils-gdb/blob/626d0e40e55c35a4f143b70def49873= 4e8ed3c2a/libiberty/rust-demangle.c#L938 [7] https://github.com/bminor/binutils-gdb/blob/626d0e40e55c35a4f143b70def49873= 4e8ed3c2a/libiberty/rust-demangle.c#L1151 [8] https://github.com/bminor/binutils-gdb/blob/626d0e40e55c35a4f143b70def49873= 4e8ed3c2a/libiberty/rust-demangle.c#L1153 [9] https://github.com/bminor/binutils-gdb/blob/626d0e40e55c35a4f143b70def49873= 4e8ed3c2a/libiberty/rust-demangle.c#L1158 Possible Fix: The infinite recursion check added in this [commit](https://github.com/bminor/binutils-gdb/commit/f10f8617a302f45dae72= 1eae0cd659911f03d864) should also be applied to `demangle_const`=