diff options
author | Björn Gustavsson <[email protected]> | 2013-01-10 12:15:43 +0100 |
---|---|---|
committer | Björn Gustavsson <[email protected]> | 2013-01-22 19:20:11 +0100 |
commit | 70caf52f1a7e09569c776bb1f99b0bf4737c63c2 (patch) | |
tree | 252d673222be3061d07ecf8148bab3357f792816 /lib/asn1/src | |
parent | 844e2126e3b814f9fa058be367be3af745c7a80b (diff) | |
download | otp-70caf52f1a7e09569c776bb1f99b0bf4737c63c2.tar.gz otp-70caf52f1a7e09569c776bb1f99b0bf4737c63c2.tar.bz2 otp-70caf52f1a7e09569c776bb1f99b0bf4737c63c2.zip |
Correct error handling for the NIF functions
Also make sure that the error handling is contained within the
asn1rt_nif module and does not leak out to generated code.
Diffstat (limited to 'lib/asn1/src')
-rw-r--r-- | lib/asn1/src/asn1rt_nif.erl | 25 | ||||
-rw-r--r-- | lib/asn1/src/asn1rtt_ber.erl | 32 | ||||
-rw-r--r-- | lib/asn1/src/asn1rtt_per.erl | 12 |
3 files changed, 25 insertions, 44 deletions
diff --git a/lib/asn1/src/asn1rt_nif.erl b/lib/asn1/src/asn1rt_nif.erl index de1fb94816..0b2e5a62a5 100644 --- a/lib/asn1/src/asn1rt_nif.erl +++ b/lib/asn1/src/asn1rt_nif.erl @@ -77,10 +77,31 @@ load_nif() -> Status end. -encode_per_complete(_TagValueList) -> +decode_ber_tlv(Binary) -> + case decode_ber_tlv_raw(Binary) of + {error,Reason} -> + exit({error,{asn1,Reason}}); + Other -> + Other + end. + +encode_per_complete(TagValueList) -> + case encode_per_complete_raw(TagValueList) of + {error,Reason} -> handle_error(Reason, TagValueList); + Other when is_binary(Other) -> Other + end. + +handle_error([], _)-> + exit({error,{asn1,enomem}}); +handle_error($1, L) -> % error in complete in driver + exit({error,{asn1,L}}); +handle_error(ErrL, L) -> + exit({error,{asn1,ErrL,L}}). + +encode_per_complete_raw(_TagValueList) -> erlang:nif_error({nif_not_loaded,module,?MODULE,line,?LINE}). -decode_ber_tlv(_Binary) -> +decode_ber_tlv_raw(_Binary) -> erlang:nif_error({nif_not_loaded,module,?MODULE,line,?LINE}). encode_ber_tlv(_TagValueList) -> diff --git a/lib/asn1/src/asn1rtt_ber.erl b/lib/asn1/src/asn1rtt_ber.erl index b374191f37..f3f6875eb3 100644 --- a/lib/asn1/src/asn1rtt_ber.erl +++ b/lib/asn1/src/asn1rtt_ber.erl @@ -123,43 +123,13 @@ ber_encode(Tlv) -> asn1rt_nif:encode_ber_tlv(Tlv). ber_decode_nif(B) -> - case asn1rt_nif:decode_ber_tlv(B) of - {error, Reason} -> handle_error(Reason, B); - Else -> Else - end. + asn1rt_nif:decode_ber_tlv(B). ber_decode_erlang(B) when is_binary(B) -> decode_primitive(B); ber_decode_erlang(Tlv) -> {Tlv,<<>>}. -handle_error([],_)-> - exit({error,{asn1,{"memory allocation problem"}}}); -handle_error({$1,_},L) -> % error in nif - exit({error,{asn1,L}}); -handle_error({$2,T},L) -> % error in nif due to wrong tag - exit({error,{asn1,{"bad tag after byte:",error_pos(T),L}}}); -handle_error({$3,T},L) -> % error in driver due to length error - exit({error,{asn1,{"bad length field after byte:", - error_pos(T),L}}}); -handle_error({$4,T},L) -> % error in driver due to indefinite length error - exit({error,{asn1, - {"indefinite length without end bytes after byte:", - error_pos(T),L}}}); -handle_error({$5,T},L) -> % error in driver due to indefinite length error - exit({error,{asn1,{"bad encoded value after byte:", - error_pos(T),L}}}); -handle_error(ErrL,L) -> - exit({error,{asn1,ErrL,L}}). - -error_pos([]) -> - "unknown position"; -error_pos([B])-> - B; -error_pos([B|Bs]) -> - BS = 8 * length(Bs), - B bsl BS + error_pos(Bs). - decode_primitive(Bin) -> {Form,TagNo,V,Rest} = decode_tag_and_length(Bin), case Form of diff --git a/lib/asn1/src/asn1rtt_per.erl b/lib/asn1/src/asn1rtt_per.erl index d545c8a854..8234121934 100644 --- a/lib/asn1/src/asn1rtt_per.erl +++ b/lib/asn1/src/asn1rtt_per.erl @@ -1311,17 +1311,7 @@ get_constraint(C,Key) -> %% complete(L) -> - case asn1rt_nif:encode_per_complete(L) of - {error, Reason} -> handle_error(Reason, L); - Else when is_binary(Else) -> Else - end. - -handle_error([],_)-> - exit({error,{asn1,{"memory allocation problem in driver"}}}); -handle_error($1,L) -> % error in complete in driver - exit({error,{asn1,L}}); -handle_error(ErrL,L) -> - exit({error,{asn1,ErrL,L}}). + asn1rt_nif:encode_per_complete(L). octets_to_complete(Len,Val) when Len < 256 -> [20,Len,Val]; |