Skip to content

Commit 114e587

Browse files
committed
Replace mem:transmute in wrap_args with safer options
Replace `mem:transmute` in `wrap_args` with safer options as without it: $ frawk -Bcranelift 'BEGIN { printf("%d\t%f\t%s\n", integer, float, string); }' would crash with: failure in runtime invalid type code passed to printf_impl_file: 2. Halting execution or with `invalid opcode` after cranelift update. Fixes: #121
1 parent 35de188 commit 114e587

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

src/codegen/intrinsics.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,10 +1144,16 @@ unsafe fn wrap_args<'a>(
11441144
ty_code
11451145
)
11461146
};
1147+
11471148
let typed_arg: FormatArg = match ty {
1148-
Ty::Int => mem::transmute::<usize, Int>(arg).into(),
1149+
Ty::Int => (arg.cast_signed() as i64).into(),
11491150
Ty::Float => Float::from_bits(arg as u64).into(),
1150-
Ty::Str => mem::transmute::<usize, &Str>(arg).clone().into(),
1151+
Ty::Str => {
1152+
unsafe {
1153+
let str_ref = std::ptr::with_exposed_provenance::<Str>(arg).as_ref().unwrap();
1154+
str_ref.clone().into()
1155+
}
1156+
},
11511157
Ty::Null => FormatArg::Null,
11521158
_ => fail!(
11531159
_rt,

src/compile.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use std::sync::Arc;
2525
pub(crate) const UNUSED: u32 = u32::max_value();
2626
pub(crate) const NULL_REG: u32 = UNUSED - 1;
2727

28+
#[repr(u8)]
2829
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash, Default)]
2930
pub(crate) enum Ty {
3031
Int = 0,

0 commit comments

Comments
 (0)