Patrick Williams | 2194f50 | 2022-10-16 14:26:09 -0500 | [diff] [blame] | 1 | From 9234cdca6ee88badfc00297e72f13dac4e540c79 Mon Sep 17 00:00:00 2001 |
| 2 | From: Nick Clifton <nickc@redhat.com> |
| 3 | Date: Fri, 1 Jul 2022 15:58:52 +0100 |
| 4 | Subject: [PATCH] Add a recursion limit to the demangle_const function in the |
| 5 | Rust demangler. |
| 6 | |
| 7 | libiberty/ |
| 8 | PR demangler/105039 |
| 9 | * rust-demangle.c (demangle_const): Add recursion limit. |
| 10 | |
| 11 | Upstream-Status: Backport [https://gcc.gnu.org/git/gitweb.cgi?p=gcc.git;h=9234cdca6ee88badfc00297e72f13dac4e540c79] |
| 12 | --- |
| 13 | libiberty/rust-demangle.c | 29 ++++++++++++++++++++--------- |
| 14 | 1 file changed, 20 insertions(+), 9 deletions(-) |
| 15 | |
| 16 | diff --git a/libiberty/rust-demangle.c b/libiberty/rust-demangle.c |
| 17 | index bb58d900e27..36afcfae278 100644 |
| 18 | --- a/libiberty/rust-demangle.c |
| 19 | +++ b/libiberty/rust-demangle.c |
| 20 | @@ -126,7 +126,7 @@ parse_integer_62 (struct rust_demangler *rdm) |
| 21 | return 0; |
| 22 | |
| 23 | x = 0; |
| 24 | - while (!eat (rdm, '_')) |
| 25 | + while (!eat (rdm, '_') && !rdm->errored) |
| 26 | { |
| 27 | c = next (rdm); |
| 28 | x *= 62; |
| 29 | @@ -1148,6 +1148,15 @@ demangle_const (struct rust_demangler *rdm) |
| 30 | if (rdm->errored) |
| 31 | return; |
| 32 | |
| 33 | + if (rdm->recursion != RUST_NO_RECURSION_LIMIT) |
| 34 | + { |
| 35 | + ++ rdm->recursion; |
| 36 | + if (rdm->recursion > RUST_MAX_RECURSION_COUNT) |
| 37 | + /* FIXME: There ought to be a way to report |
| 38 | + that the recursion limit has been reached. */ |
| 39 | + goto fail_return; |
| 40 | + } |
| 41 | + |
| 42 | if (eat (rdm, 'B')) |
| 43 | { |
| 44 | backref = parse_integer_62 (rdm); |
| 45 | @@ -1158,7 +1167,7 @@ demangle_const (struct rust_demangler *rdm) |
| 46 | demangle_const (rdm); |
| 47 | rdm->next = old_next; |
| 48 | } |
| 49 | - return; |
| 50 | + goto pass_return; |
| 51 | } |
| 52 | |
| 53 | ty_tag = next (rdm); |
| 54 | @@ -1167,7 +1176,7 @@ demangle_const (struct rust_demangler *rdm) |
| 55 | /* Placeholder. */ |
| 56 | case 'p': |
| 57 | PRINT ("_"); |
| 58 | - return; |
| 59 | + goto pass_return; |
| 60 | |
| 61 | /* Unsigned integer types. */ |
| 62 | case 'h': |
| 63 | @@ -1200,18 +1209,20 @@ demangle_const (struct rust_demangler *rdm) |
| 64 | break; |
| 65 | |
| 66 | default: |
| 67 | - rdm->errored = 1; |
| 68 | - return; |
| 69 | + goto fail_return; |
| 70 | } |
| 71 | |
| 72 | - if (rdm->errored) |
| 73 | - return; |
| 74 | - |
| 75 | - if (rdm->verbose) |
| 76 | + if (!rdm->errored && rdm->verbose) |
| 77 | { |
| 78 | PRINT (": "); |
| 79 | PRINT (basic_type (ty_tag)); |
| 80 | } |
| 81 | + |
| 82 | + fail_return: |
| 83 | + rdm->errored = 1; |
| 84 | + pass_return: |
| 85 | + if (rdm->recursion != RUST_NO_RECURSION_LIMIT) |
| 86 | + -- rdm->recursion; |
| 87 | } |
| 88 | |
| 89 | static void |
| 90 | -- |
| 91 | 2.31.1 |
| 92 | |