Skip to content

Commit 35e24fc

Browse files
author
Ariel Ben-Yehuda
committed
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.
1 parent 2cdc89e commit 35e24fc

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

crates/native-c/src/demangle.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,10 +1707,8 @@ NODISCARD static demangle_status rust_demangle_legacy_demangle(const char *s, si
17071707
if (chars_len == 0) {
17081708
return DemangleInvalid;
17091709
}
1710-
char c = *chars++;
1711-
chars_len--;
1712-
1713-
while (c != 'E') {
1710+
char c;
1711+
while ((c = *chars) != 'E') {
17141712
// Decode an identifier element's length
17151713
if (c < '0' || c > '9') {
17161714
return DemangleInvalid;
@@ -1726,25 +1724,25 @@ NODISCARD static demangle_status rust_demangle_legacy_demangle(const char *s, si
17261724
return DemangleInvalid;
17271725
}
17281726
len += d;
1727+
1728+
chars++;
1729+
chars_len--;
17291730
if (chars_len == 0) {
17301731
return DemangleInvalid;
17311732
}
1732-
c = *chars++;
1733-
chars_len--;
1733+
c = *chars;
17341734
}
17351735

17361736
// Advance by the length
1737-
for (size_t i = 0; i < len; i++) {
1738-
if (chars_len == 0) {
1739-
return DemangleInvalid;
1740-
}
1741-
c = *chars++;
1742-
chars_len--;
1737+
if (chars_len <= len) {
1738+
return DemangleInvalid;
17431739
}
1740+
chars += len;
1741+
chars_len -= len;
17441742
elements++;
17451743
}
17461744
*res = (struct demangle_legacy) { inner, inner_len, elements };
1747-
*rest = chars;
1745+
*rest = chars + 1;
17481746
return DemangleOk;
17491747
}
17501748

0 commit comments

Comments
 (0)