Fix mir CString lowering not respecting the extra 0 byte for length calc

This commit is contained in:
Lukas Wirth 2023-05-18 11:29:19 +02:00
parent 4b577e2bc8
commit eab295cc73

View File

@ -1121,11 +1121,13 @@ fn lower_literal_to_operand(&mut self, ty: Ty, l: &Literal) -> Result<Operand> {
} }
hir_def::hir::Literal::CString(b) => { hir_def::hir::Literal::CString(b) => {
let b = b.as_bytes(); let b = b.as_bytes();
let bytes = b.iter().copied().chain(iter::once(0)).collect::<Vec<_>>();
let mut data = Vec::with_capacity(mem::size_of::<usize>() * 2); let mut data = Vec::with_capacity(mem::size_of::<usize>() * 2);
data.extend(0usize.to_le_bytes()); data.extend(0usize.to_le_bytes());
data.extend(b.len().to_le_bytes()); data.extend(bytes.len().to_le_bytes());
let mut mm = MemoryMap::default(); let mut mm = MemoryMap::default();
mm.insert(0, b.iter().copied().chain(iter::once(0)).collect::<Vec<_>>()); mm.insert(0, bytes);
return Ok(Operand::from_concrete_const(data, mm, ty)); return Ok(Operand::from_concrete_const(data, mm, ty));
} }
hir_def::hir::Literal::ByteString(b) => { hir_def::hir::Literal::ByteString(b) => {