Skip to content
This repository was archived by the owner on Jun 16, 2020. It is now read-only.

Commit 7e8c792

Browse files
committed
Removes use of unsafe transmute in read_f32 and read_f64
https://github.com/yurydelendik/wasmparser.rs/issues/14
1 parent 262e829 commit 7e8c792

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "wasmparser"
3-
version = "0.5.0"
3+
version = "0.5.1"
44
authors = ["Yury Delendik <[email protected]>"]
55
license = "Apache-2.0"
66
repository = "https://github.com/yurydelendik/wasmparser.rs"

src/parser.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
*/
1515
// See https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md
1616

17-
use std::mem::transmute;
1817
use std::result;
1918

2019
type Result<T> = result::Result<T, &'static str>;
@@ -251,6 +250,20 @@ pub struct RelocEntry {
251250
addend: Option<u32>,
252251
}
253252

253+
/// An IEEE binary32 immediate floating point value, represented as a u32
254+
/// containing the bitpattern.
255+
///
256+
/// All bit patterns are allowed.
257+
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
258+
pub struct Ieee32(u32);
259+
260+
/// An IEEE binary64 immediate floating point value, represented as a u64
261+
/// containing the bitpattern.
262+
///
263+
/// All bit patterns are allowed.
264+
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
265+
pub struct Ieee64(u64);
266+
254267
/// Instructions as defined at https://webassembly.github.io/spec/binary/instructions.html
255268
#[derive(Debug)]
256269
pub enum Operator {
@@ -301,8 +314,8 @@ pub enum Operator {
301314
GrowMemory { reserved: u32 },
302315
I32Const { value: i32 },
303316
I64Const { value: i64 },
304-
F32Const { value: f32 },
305-
F64Const { value: f64 },
317+
F32Const { value: Ieee32 },
318+
F64Const { value: Ieee64 },
306319
I32Eqz,
307320
I32Eq,
308321
I32Ne,
@@ -616,14 +629,14 @@ impl<'a> BinaryReader<'a> {
616629
Ok((result << ashift) >> ashift)
617630
}
618631

619-
pub fn read_f32(&mut self) -> Result<f32> {
632+
pub fn read_f32(&mut self) -> Result<Ieee32> {
620633
let value = self.read_u32()?;
621-
Ok(unsafe { transmute::<u32, f32>(value) })
634+
Ok(Ieee32(value))
622635
}
623636

624-
pub fn read_f64(&mut self) -> Result<f64> {
637+
pub fn read_f64(&mut self) -> Result<Ieee64> {
625638
let value = self.read_u64()?;
626-
Ok(unsafe { transmute::<u64, f64>(value) })
639+
Ok(Ieee64(value))
627640
}
628641

629642
pub fn read_string(&mut self) -> Result<&'a [u8]> {

0 commit comments

Comments
 (0)