Commit a9e63373 authored by Lev Walkin's avatar Lev Walkin

remove undefined behavior

parent 93180046
...@@ -9,6 +9,30 @@ ...@@ -9,6 +9,30 @@
#include <NativeEnumerated.h> #include <NativeEnumerated.h>
#include <errno.h> #include <errno.h>
/*
* This function is only to get rid of Undefined Behavior Sanitizer warning.
*/
static intmax_t CLANG_NO_SANITIZE("shift-base")
asn__safe_nativeenumerated_convert_helper(const uint8_t *b,
const uint8_t *end) {
intmax_t value;
/* Perform the sign initialization */
/* Actually value = -(*b >> 7); gains nothing, yet unreadable! */
if((*b >> 7)) {
value = -1;
} else {
value = 0;
}
/* Conversion engine */
for(; b < end; b++) {
value = (value << 8) | *b;
}
return value;
}
asn_dec_rval_t asn_dec_rval_t
NativeEnumerated_decode_oer(const asn_codec_ctx_t *opt_codec_ctx, NativeEnumerated_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
asn_TYPE_descriptor_t *td, asn_TYPE_descriptor_t *td,
...@@ -42,7 +66,7 @@ NativeEnumerated_decode_oer(const asn_codec_ctx_t *opt_codec_ctx, ...@@ -42,7 +66,7 @@ NativeEnumerated_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
*/ */
size_t length = *b & 0x7f; size_t length = *b & 0x7f;
const uint8_t *bend; const uint8_t *bend;
long value; intmax_t value;
if(length < 1 || length > sizeof(*native)) { if(length < 1 || length > sizeof(*native)) {
ASN__DECODE_FAILED; ASN__DECODE_FAILED;
...@@ -52,10 +76,8 @@ NativeEnumerated_decode_oer(const asn_codec_ctx_t *opt_codec_ctx, ...@@ -52,10 +76,8 @@ NativeEnumerated_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
} }
b++; b++;
bend = b + length; bend = b + length;
value = (*b & 0x80) ? -1 : 0; /* Determine sign */
for(; b < bend; b++)
value = (value << 8) | *b;
value = asn__safe_nativeenumerated_convert_helper(b, bend);
if(value < 0) { if(value < 0) {
const asn_INTEGER_specifics_t *specs = const asn_INTEGER_specifics_t *specs =
(const asn_INTEGER_specifics_t *)td->specifics; (const asn_INTEGER_specifics_t *)td->specifics;
......
...@@ -182,7 +182,7 @@ main() { ...@@ -182,7 +182,7 @@ main() {
CHECK_ROUNDTRIP(value); CHECK_ROUNDTRIP(value);
} }
for(size_t i = 0; i < 8 * sizeof(intmax_t) ; i++) { for(size_t i = 0; i < 8 * sizeof(intmax_t) - 1; i++) {
intmax_t value = (intmax_t)1 << i; intmax_t value = (intmax_t)1 << i;
CHECK_ROUNDTRIP(value); CHECK_ROUNDTRIP(value);
value = -value; value = -value;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment