From 35e24fc52fb93d3870fc2ca02020c235f50b6faa Mon Sep 17 00:00:00 2001 From: Ariel Ben-Yehuda Date: Thu, 5 Dec 2024 09:00:29 +0000 Subject: [PATCH 1/2] change legacy_demangle to be more idiomatic C the code used to be translated line-to-line from Rust iterators, change it to be more idiomatic C and guarantee there is no weird for loop. Fuzzing passes. --- crates/native-c/src/demangle.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/crates/native-c/src/demangle.c b/crates/native-c/src/demangle.c index bb36fe4..b28baf6 100644 --- a/crates/native-c/src/demangle.c +++ b/crates/native-c/src/demangle.c @@ -1707,10 +1707,8 @@ NODISCARD static demangle_status rust_demangle_legacy_demangle(const char *s, si if (chars_len == 0) { return DemangleInvalid; } - char c = *chars++; - chars_len--; - - while (c != 'E') { + char c; + while ((c = *chars) != 'E') { // Decode an identifier element's length if (c < '0' || c > '9') { return DemangleInvalid; @@ -1726,25 +1724,25 @@ NODISCARD static demangle_status rust_demangle_legacy_demangle(const char *s, si return DemangleInvalid; } len += d; + + chars++; + chars_len--; if (chars_len == 0) { return DemangleInvalid; } - c = *chars++; - chars_len--; + c = *chars; } // Advance by the length - for (size_t i = 0; i < len; i++) { - if (chars_len == 0) { - return DemangleInvalid; - } - c = *chars++; - chars_len--; + if (chars_len <= len) { + return DemangleInvalid; } + chars += len; + chars_len -= len; elements++; } *res = (struct demangle_legacy) { inner, inner_len, elements }; - *rest = chars; + *rest = chars + 1; return DemangleOk; } From 80e40f57d99fa0a63f793c02ec7b0b9e0155ad80 Mon Sep 17 00:00:00 2001 From: Ariel Ben-Yehuda Date: Thu, 5 Dec 2024 09:02:31 +0000 Subject: [PATCH 2/2] add comment about finding latest version of code --- crates/native-c/src/demangle.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/native-c/src/demangle.c b/crates/native-c/src/demangle.c index b28baf6..e4eb399 100644 --- a/crates/native-c/src/demangle.c +++ b/crates/native-c/src/demangle.c @@ -1,6 +1,8 @@ // Code for demangling Rust symbols. This code is mostly // a line-by-line translation of the Rust code in `rustc-demangle`. +// you can find the latest version of this code in https://github.com/rust-lang/rustc-demangle + #include #include #include