beanit / asn1bean

ASN1bean (formerly known as jASN1) is a Java ASN.1 BER and DER encoding/decoding library
https://www.beanit.com/asn1/
Apache License 2.0
110 stars 45 forks source link

Add constraint checking for `SIZE` and `INTEGER(l..h)` #41

Open odiferousmint opened 3 years ago

odiferousmint commented 3 years ago

Hello.

For example:

IA5String SIZE(0..10) would check if the length of string is between 0 and 10. IA5String SIZE(10) would check if the length of string is exactly 10. INTEGER(0..10) would check if the value is between 0 and 10. INTEGER(10) would check if the value is exactly 10.

Now, my issue is what would happen if it fails? We will most likely know it as runtime, right? Would it be possible to catch this constraint error as an user of the library? If not, then I really have no idea how one would handle the bounds check errors besides just simply crashing. In any case, it should definitely should be possible to do this, just throw an error in the generated Java code in the decode method, so when the user of the library attempts to decode data that fits the specification but say, a value is higher than N, then it should throw a range error or something.

Easy-to-digest information about it here: https://stackoverflow.com/a/24238356/15088101


While we are at it, type checking would be pretty cool, too. :)