From eab295cc73a876eeaa1424b3ed7a76bcf46a5fec Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 18 May 2023 11:29:19 +0200 Subject: [PATCH] Fix mir CString lowering not respecting the extra 0 byte for length calc --- crates/hir-ty/src/mir/lower.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/hir-ty/src/mir/lower.rs b/crates/hir-ty/src/mir/lower.rs index 051ae228e22..28305bbe81c 100644 --- a/crates/hir-ty/src/mir/lower.rs +++ b/crates/hir-ty/src/mir/lower.rs @@ -1121,11 +1121,13 @@ fn lower_literal_to_operand(&mut self, ty: Ty, l: &Literal) -> Result { } hir_def::hir::Literal::CString(b) => { let b = b.as_bytes(); + let bytes = b.iter().copied().chain(iter::once(0)).collect::>(); + let mut data = Vec::with_capacity(mem::size_of::() * 2); 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(); - mm.insert(0, b.iter().copied().chain(iter::once(0)).collect::>()); + mm.insert(0, bytes); return Ok(Operand::from_concrete_const(data, mm, ty)); } hir_def::hir::Literal::ByteString(b) => {