@@ -19,20 +19,20 @@ pub struct CodeAttributeExceptionTable {
1919
2020impl CodeAttributeExceptionTable {
2121 pub fn parse < ' a > ( data : & ' a [ u8 ] , constant_pool : & BTreeMap < u16 , ConstantPoolItem > ) -> IResult < & ' a [ u8 ] , Self > {
22- map ( ( be_u16, be_u16, be_u16, be_u16) , |( start_pc, end_pc, handler_pc, catch_type) | {
22+ map_res ( ( be_u16, be_u16, be_u16, be_u16) , |( start_pc, end_pc, handler_pc, catch_type) | {
2323 let catch_type = if catch_type != 0 {
24- let index = constant_pool. get ( & catch_type) . unwrap ( ) . class_name_index ( ) ;
25- Some ( constant_pool. get ( & index) . unwrap ( ) . utf8 ( ) )
24+ let index = constant_pool. get ( & catch_type) . and_then ( ConstantPoolItem :: class_name_index ) . ok_or ( ( ) ) ? ;
25+ Some ( constant_pool. get ( & index) . and_then ( ConstantPoolItem :: utf8 ) . ok_or ( ( ) ) ? )
2626 } else {
2727 None
2828 } ;
2929
30- Self {
30+ Ok :: < _ , ( ) > ( Self {
3131 start_pc,
3232 end_pc,
3333 handler_pc,
3434 catch_type,
35- }
35+ } )
3636 } )
3737 . parse ( data)
3838 }
@@ -52,7 +52,7 @@ impl AttributeInfoCode {
5252 (
5353 be_u16,
5454 be_u16,
55- map ( flat_map ( be_u32, take) , |x : & [ u8 ] | Self :: parse_code ( x, constant_pool) ) ,
55+ map_res ( flat_map ( be_u32, take) , |x : & [ u8 ] | Self :: parse_code ( x, constant_pool) ) ,
5656 length_count ( be_u16, |x| CodeAttributeExceptionTable :: parse ( x, constant_pool) ) ,
5757 length_count ( be_u16, |x| AttributeInfo :: parse ( x, constant_pool) ) ,
5858 ) ,
@@ -67,22 +67,21 @@ impl AttributeInfoCode {
6767 . parse ( data)
6868 }
6969
70- fn parse_code ( code : & [ u8 ] , constant_pool : & BTreeMap < u16 , ConstantPoolItem > ) -> BTreeMap < u32 , Opcode > {
70+ fn parse_code ( code : & [ u8 ] , constant_pool : & BTreeMap < u16 , ConstantPoolItem > ) -> Result < BTreeMap < u32 , Opcode > , ( ) > {
7171 let mut result = BTreeMap :: new ( ) ;
7272
7373 let mut data = code;
74- loop {
74+ while !data . is_empty ( ) {
7575 let offset = unsafe { data. as_ptr ( ) . offset_from ( code. as_ptr ( ) ) } as usize ;
76- if let Ok ( ( remaining, opcode) ) = Opcode :: parse ( data, offset, constant_pool) {
77- result. insert ( offset as _ , opcode) ;
78-
79- data = remaining;
80- } else {
81- break ;
76+ let ( remaining, opcode) = Opcode :: parse ( data, offset, constant_pool) . map_err ( |_| ( ) ) ?;
77+ if remaining. len ( ) >= data. len ( ) {
78+ return Err ( ( ) ) ;
8279 }
80+ result. insert ( offset as _ , opcode) ;
81+ data = remaining;
8382 }
8483
85- result
84+ Ok ( result)
8685 }
8786}
8887
@@ -114,8 +113,8 @@ impl LocalVariableTableEntry {
114113 (
115114 be_u16,
116115 be_u16,
117- map ( be_u16, |x| constant_pool. get ( & x) . unwrap ( ) . utf8 ( ) ) ,
118- map ( be_u16, |x| constant_pool. get ( & x) . unwrap ( ) . utf8 ( ) ) ,
116+ map_res ( be_u16, |x| constant_pool. get ( & x) . and_then ( ConstantPoolItem :: utf8 ) . ok_or ( ( ) ) ) ,
117+ map_res ( be_u16, |x| constant_pool. get ( & x) . and_then ( ConstantPoolItem :: utf8 ) . ok_or ( ( ) ) ) ,
119118 be_u16,
120119 ) ,
121120 |( start_pc, length, name, descriptor, index) | Self {
@@ -152,7 +151,10 @@ pub enum AttributeInfo {
152151impl AttributeInfo {
153152 pub fn parse < ' a > ( data : & ' a [ u8 ] , constant_pool : & BTreeMap < u16 , ConstantPoolItem > ) -> IResult < & ' a [ u8 ] , Self > {
154153 map_res (
155- ( map ( be_u16, |x| constant_pool. get ( & x) . unwrap ( ) . utf8 ( ) ) , flat_map ( be_u32, take) ) ,
154+ (
155+ map_res ( be_u16, |x| constant_pool. get ( & x) . and_then ( ConstantPoolItem :: utf8) . ok_or ( ( ) ) ) ,
156+ flat_map ( be_u32, take) ,
157+ ) ,
156158 |( name, info) : ( _ , & [ u8 ] ) | {
157159 Ok :: < _ , nom:: Err < _ > > ( match name. as_str ( ) {
158160 "ConstantValue" => AttributeInfo :: ConstantValue ( Self :: parse_constant_value ( info, constant_pool) ?. 1 ) ,
@@ -180,11 +182,11 @@ impl AttributeInfo {
180182 }
181183
182184 fn parse_source_file < ' a > ( data : & ' a [ u8 ] , constant_pool : & BTreeMap < u16 , ConstantPoolItem > ) -> IResult < & ' a [ u8 ] , Arc < String > > {
183- map ( be_u16, |x| constant_pool. get ( & x) . unwrap ( ) . utf8 ( ) ) . parse ( data)
185+ map_res ( be_u16, |x| constant_pool. get ( & x) . and_then ( ConstantPoolItem :: utf8 ) . ok_or ( ( ) ) ) . parse ( data)
184186 }
185187
186188 fn parse_constant_value < ' a > ( data : & ' a [ u8 ] , constant_pool : & BTreeMap < u16 , ConstantPoolItem > ) -> IResult < & ' a [ u8 ] , ConstantPoolReference > {
187- map ( be_u16, |x| ConstantPoolReference :: from_constant_pool ( constant_pool, x as _ ) ) . parse ( data)
189+ map_res ( be_u16, |x| ConstantPoolReference :: from_constant_pool ( constant_pool, x) . ok_or ( ( ) ) ) . parse ( data)
188190 }
189191
190192 fn parse_local_variable_table < ' a > (
0 commit comments