Skip to content

Commit 9626fb7

Browse files
committed
Remove dynamic allocation
1 parent 3cd5999 commit 9626fb7

File tree

1 file changed

+21
-6
lines changed
  • sdk/program-entrypoint/src

1 file changed

+21
-6
lines changed

sdk/program-entrypoint/src/lib.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,11 @@ macro_rules! custom_panic_space_efficient {
316316
}
317317

318318
if let Some(loc) = info.location() {
319+
const MAX_LEN: usize = 200;
319320
let filename = loc.file().as_bytes();
320-
// Vector of filename size + 11 (line number + separator)
321+
// MAX_LEN for filename + 3 (ellipsis string) + 11 (line number + separator)
321322
// + 11 (column number + separator) + 13 (panic string) + 15 extra bytes
322-
let mut msg_line = vec![32; filename.len() + 50];
323+
let mut msg_line: [u8; MAX_LEN + 53] = [32; MAX_LEN + 53];
323324

324325
let panic_str = "Panicked at: ".as_bytes(); // 13 bytes
325326
let dst = msg_line.as_mut_ptr();
@@ -328,12 +329,26 @@ macro_rules! custom_panic_space_efficient {
328329
// SAFETY: Destination is larger than the panic string.
329330
std::ptr::copy_nonoverlapping(src, dst, panic_str.len());
330331

331-
let dst = dst.add(panic_str.len());
332-
let src = filename.as_ptr();
332+
let mut dst = dst.add(panic_str.len());
333+
let mut src = filename.as_ptr();
334+
let mut copy_len = filename.len();
335+
if copy_len > MAX_LEN {
336+
let truncated_str = "...".as_bytes(); // 3 bytes
337+
// SAFETY: Destination is larger than the truncated string
338+
std::ptr::copy_nonoverlapping(
339+
truncated_str.as_ptr(),
340+
dst,
341+
truncated_str.len(),
342+
);
343+
dst = dst.add(truncated_str.len());
344+
src = src.add(copy_len - MAX_LEN);
345+
copy_len = MAX_LEN;
346+
}
347+
333348
// SAFETY: The destination is always larger than the filename string.
334-
std::ptr::copy_nonoverlapping(src, dst, filename.len());
349+
std::ptr::copy_nonoverlapping(src, dst, copy_len);
335350

336-
let dst = dst.add(filename.len());
351+
let dst = dst.add(copy_len);
337352
// SAFETY: `write_num` never writes more than 11 bytes
338353
let written_bytes = write_num(loc.line(), dst);
339354
let dst = dst.add(written_bytes);

0 commit comments

Comments
 (0)