@@ -296,77 +296,21 @@ macro_rules! custom_panic_space_efficient {
296296 #[ cfg( all( not( feature = "custom-panic" ) , target_os = "solana" ) ) ]
297297 #[ no_mangle]
298298 fn custom_panic( info: & core:: panic:: PanicInfo <' _>) {
299- #[ inline( never) ]
300- unsafe fn write_num( mut num: u32 , dst: * mut u8 ) -> usize {
301- let mut buf: [ u8 ; 10 ] = [ 0 ; 10 ] ;
302- let init_ptr = buf. as_mut_ptr( ) . add( 11 ) ;
303- let mut write_ptr = init_ptr;
304- while num > 0 {
305- write_ptr = write_ptr. sub( 1 ) ;
306- // SAFETY: The pointer will always be within *buf and *buf+10
307- * write_ptr = ( num % 10 ) as u8 + 48 ;
308- num /= 10 ;
309- }
310- * dst = 58 ;
311- let dst = dst. add( 1 ) ;
312- let len = init_ptr. offset_from( write_ptr) as usize ;
313- // SAFETY: The copy length will never be greater than 10.
314- std:: ptr:: copy_nonoverlapping( write_ptr, dst, len) ;
315- len + 1
316- }
317-
318- if let Some ( loc) = info. location( ) {
319- const MAX_LEN : usize = 200 ;
320- let filename = loc. file( ) . as_bytes( ) ;
321- // MAX_LEN for filename + 3 (ellipsis string) + 11 (line number + separator)
322- // + 11 (column number + separator) + 13 (panic string) + 15 extra bytes
323- let mut msg_line: [ u8 ; MAX_LEN + 53 ] = [ 32 ; MAX_LEN + 53 ] ;
324-
325- let panic_str = "Panicked at: " . as_bytes( ) ; // 13 bytes
326- let dst = msg_line. as_mut_ptr( ) ;
299+ if let Some ( Some ( mm) ) = info. message( ) . map( |mes| mes. as_str( ) ) {
300+ let mes = mm. as_bytes( ) ;
327301 unsafe {
328- let src = panic_str. as_ptr( ) ;
329- // SAFETY: Destination is larger than the panic string.
330- std:: ptr:: copy_nonoverlapping( src, dst, panic_str. len( ) ) ;
331-
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-
348- // SAFETY: The destination is always larger than the filename string.
349- std:: ptr:: copy_nonoverlapping( src, dst, copy_len) ;
350-
351- let dst = dst. add( copy_len) ;
352- // SAFETY: `write_num` never writes more than 11 bytes
353- let written_bytes = write_num( loc. line( ) , dst) ;
354- let dst = dst. add( written_bytes) ;
355- // SAFETY: `write_num` never writes more than 11 bytes
356- let written_bytes = write_num( loc. column( ) , dst) ;
357-
358- let line_len = dst. add( written_bytes) . offset_from( msg_line. as_ptr( ) ) as usize ;
359- // SAFETY: The line length is properly offseted from number of
360- // written bytes
361- solana_program:: syscalls:: sol_log_( msg_line. as_ptr( ) , line_len as u64 ) ;
302+ solana_program:: syscalls:: sol_log_( mes. as_ptr( ) , mes. len( ) as u64 ) ;
362303 }
363304 }
364305
365- if let Some ( Some ( mm) ) = info. message( ) . map( |mes| mes. as_str( ) ) {
366- let mes = mm. as_bytes( ) ;
367- // SAFETY: We assumed the panic message is well formed.
306+ if let Some ( loc) = info. location( ) {
368307 unsafe {
369- solana_program:: syscalls:: sol_log_( mes. as_ptr( ) , mes. len( ) as u64 ) ;
308+ solana_program:: syscalls:: sol_panic_(
309+ loc. file( ) . as_ptr( ) ,
310+ loc. file( ) . len( ) as u64 ,
311+ loc. line( ) as u64 ,
312+ loc. column( ) as u64 ,
313+ ) ;
370314 }
371315 }
372316 }
0 commit comments