Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a separate method that reads a string of unlimited length for use in the custom name section #1650

Merged
merged 5 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions crates/wasmparser/src/binary_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,14 @@ impl<'a> BinaryReader<'a> {
Ok(Ieee64(value))
}

/// (internal) Reads a fixed-size WebAssembly string from the module.
fn internal_read_string(&mut self, len: usize) -> Result<&'a str> {
let bytes = self.read_bytes(len)?;
str::from_utf8(bytes).map_err(|_| {
BinaryReaderError::new("malformed UTF-8 encoding", self.original_position() - 1)
})
}

/// Reads a WebAssembly string from the module.
/// # Errors
/// If `BinaryReader` has less than up to four bytes remaining, the string's
Expand All @@ -701,10 +709,13 @@ impl<'a> BinaryReader<'a> {
self.original_position() - 1,
));
}
let bytes = self.read_bytes(len)?;
str::from_utf8(bytes).map_err(|_| {
BinaryReaderError::new("malformed UTF-8 encoding", self.original_position() - 1)
})
return self.internal_read_string(len);
}

/// Reads a unlimited WebAssembly string from the module.
pub fn read_unlimited_string(&mut self) -> Result<&'a str> {
let len = self.read_var_u32()? as usize;
return self.internal_read_string(len);
}

#[cold]
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmparser/src/readers/core/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct Naming<'a> {
impl<'a> FromReader<'a> for Naming<'a> {
fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
let index = reader.read_var_u32()?;
let name = reader.read_string()?;
let name = reader.read_unlimited_string()?;
Ok(Naming { index, name })
}
}
Expand Down