remove undefined behavior

This commit is contained in:
Lev Walkin 2017-09-17 23:24:56 -07:00
parent 9318004694
commit a9e63373e5
2 changed files with 27 additions and 5 deletions

View File

@ -9,6 +9,30 @@
#include <NativeEnumerated.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
NativeEnumerated_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
asn_TYPE_descriptor_t *td,
@ -42,7 +66,7 @@ NativeEnumerated_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
*/
size_t length = *b & 0x7f;
const uint8_t *bend;
long value;
intmax_t value;
if(length < 1 || length > sizeof(*native)) {
ASN__DECODE_FAILED;
@ -52,10 +76,8 @@ NativeEnumerated_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
}
b++;
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) {
const asn_INTEGER_specifics_t *specs =
(const asn_INTEGER_specifics_t *)td->specifics;

View File

@ -182,7 +182,7 @@ main() {
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;
CHECK_ROUNDTRIP(value);
value = -value;