@@ -1162,17 +1162,18 @@ impl Architecture for CoreArchitecture {
11621162 & mut result as * mut _ ,
11631163 & mut count as * mut _ ,
11641164 ) {
1165- let vec = Vec :: < BNInstructionTextToken > :: from_raw_parts ( result, count , count)
1165+ let vec = slice :: from_raw_parts ( result, count)
11661166 . iter ( )
1167- . map ( |x| InstructionTextToken :: from_raw ( x) )
1167+ . map ( |x| InstructionTextToken :: from_raw ( x) . to_owned ( ) )
11681168 . collect ( ) ;
1169+ BNFreeInstructionText ( result, count) ;
11691170 Some ( ( consumed, vec) )
11701171 } else {
11711172 None
11721173 }
11731174 }
11741175 }
1175-
1176+
11761177 fn instruction_llil (
11771178 & self ,
11781179 data : & [ u8 ] ,
@@ -1811,27 +1812,25 @@ where
18111812 let data = unsafe { slice:: from_raw_parts ( data, * len) } ;
18121813 let result = unsafe { & mut * result } ;
18131814
1814- match custom_arch. instruction_text ( data, addr) {
1815- Some ( ( res_size, mut res_tokens) ) => {
1816- unsafe {
1817- // TODO: Can't use into_raw_parts as it's unstable so we do this instead...
1818- let r_ptr = res_tokens. as_mut_ptr ( ) ;
1819- let r_count = res_tokens. len ( ) ;
1820- mem:: forget ( res_tokens) ;
1821-
1822- * result = & mut ( * r_ptr) . 0 ;
1823- * count = r_count;
1824- * len = res_size;
1825- }
1826- true
1827- }
1828- None => false ,
1815+ let Some ( ( res_size, res_tokens) ) = custom_arch. instruction_text ( data, addr) else {
1816+ return false ;
1817+ } ;
1818+
1819+ let res_tokens: Box < [ _ ] > = res_tokens. into_boxed_slice ( ) ;
1820+ unsafe {
1821+ let res_tokens = Box :: leak ( res_tokens) ;
1822+ let r_ptr = res_tokens. as_mut_ptr ( ) ;
1823+ let r_count = res_tokens. len ( ) ;
1824+
1825+ * result = & mut ( * r_ptr) . 0 ;
1826+ * count = r_count;
1827+ * len = res_size;
18291828 }
1829+ true
18301830 }
18311831
18321832 extern "C" fn cb_free_instruction_text ( tokens : * mut BNInstructionTextToken , count : usize ) {
1833- let _tokens =
1834- unsafe { Vec :: from_raw_parts ( tokens as * mut InstructionTextToken , count, count) } ;
1833+ let _tokens = unsafe { Box :: from_raw ( ptr:: slice_from_raw_parts_mut ( tokens, count) ) } ;
18351834 }
18361835
18371836 extern "C" fn cb_instruction_llil < A > (
@@ -1931,15 +1930,7 @@ where
19311930 if len == 0 {
19321931 ptr:: null_mut ( )
19331932 } else {
1934- let mut res = Vec :: with_capacity ( len + 1 ) ;
1935-
1936- res. push ( len as u32 ) ;
1937-
1938- for i in items {
1939- res. push ( i) ;
1940- }
1941-
1942- assert ! ( res. len( ) == len + 1 ) ;
1933+ let mut res: Box < [ _ ] > = [ len as u32 ] . into_iter ( ) . chain ( items) . collect ( ) ;
19431934
19441935 let raw = res. as_mut_ptr ( ) ;
19451936 mem:: forget ( res) ;
@@ -2280,7 +2271,8 @@ where
22802271 unsafe {
22812272 let actual_start = regs. offset ( -1 ) ;
22822273 let len = * actual_start + 1 ;
2283- let _regs = Vec :: from_raw_parts ( actual_start, len as usize , len as usize ) ;
2274+ let regs_ptr = ptr:: slice_from_raw_parts_mut ( actual_start, len. try_into ( ) . unwrap ( ) ) ;
2275+ let _regs = Box :: from_raw ( regs_ptr) ;
22842276 }
22852277 }
22862278
@@ -2420,28 +2412,25 @@ where
24202412 {
24212413 let custom_arch = unsafe { & * ( ctxt as * mut A ) } ;
24222414
2423- if let Some ( intrinsic) = custom_arch. intrinsic_from_id ( intrinsic) {
2424- let inputs = intrinsic. inputs ( ) ;
2425- let mut res = Vec :: with_capacity ( inputs. len ( ) ) ;
2426- for input in inputs {
2427- res. push ( input. into_raw ( ) ) ;
2428- }
2429-
2430- unsafe {
2431- * count = res. len ( ) ;
2432- if res. is_empty ( ) {
2433- ptr:: null_mut ( )
2434- } else {
2435- let raw = res. as_mut_ptr ( ) ;
2436- mem:: forget ( res) ;
2437- raw
2438- }
2439- }
2440- } else {
2415+ let Some ( intrinsic) = custom_arch. intrinsic_from_id ( intrinsic) else {
24412416 unsafe {
24422417 * count = 0 ;
24432418 }
2444- ptr:: null_mut ( )
2419+ return ptr:: null_mut ( ) ;
2420+ } ;
2421+
2422+ let inputs = intrinsic. inputs ( ) ;
2423+ let mut res: Box < [ _ ] > = inputs. into_iter ( ) . map ( |input| input. into_raw ( ) ) . collect ( ) ;
2424+
2425+ unsafe {
2426+ * count = res. len ( ) ;
2427+ if res. is_empty ( ) {
2428+ ptr:: null_mut ( )
2429+ } else {
2430+ let raw = res. as_mut_ptr ( ) ;
2431+ mem:: forget ( res) ;
2432+ raw
2433+ }
24452434 }
24462435 }
24472436
@@ -2453,8 +2442,8 @@ where
24532442
24542443 if !nt. is_null ( ) {
24552444 unsafe {
2456- let list = Vec :: from_raw_parts ( nt, count, count ) ;
2457- for nt in list {
2445+ let name_and_types = Box :: from_raw ( ptr :: slice_from_raw_parts_mut ( nt, count) ) ;
2446+ for nt in name_and_types . into_iter ( ) {
24582447 BnString :: from_raw ( nt. name ) ;
24592448 }
24602449 }
@@ -2473,10 +2462,7 @@ where
24732462
24742463 if let Some ( intrinsic) = custom_arch. intrinsic_from_id ( intrinsic) {
24752464 let inputs = intrinsic. outputs ( ) ;
2476- let mut res = Vec :: with_capacity ( inputs. len ( ) ) ;
2477- for input in inputs {
2478- res. push ( input. into ( ) ) ;
2479- }
2465+ let mut res: Box < [ _ ] > = inputs. iter ( ) . map ( |input| input. as_ref ( ) . into ( ) ) . collect ( ) ;
24802466
24812467 unsafe {
24822468 * count = res. len ( ) ;
@@ -2505,9 +2491,7 @@ where
25052491 {
25062492 let _custom_arch = unsafe { & * ( ctxt as * mut A ) } ;
25072493 if !tl. is_null ( ) {
2508- unsafe {
2509- let _list = Vec :: from_raw_parts ( tl, count, count) ;
2510- }
2494+ let _type_list = unsafe { Box :: from_raw ( ptr:: slice_from_raw_parts_mut ( tl, count) ) } ;
25112495 }
25122496 }
25132497
0 commit comments