Commit 642b92f3 authored by Lev Walkin's avatar Lev Walkin

get rid of undefined behavior sanitizer warning

parent e0236da7
...@@ -783,11 +783,34 @@ INTEGER_encode_uper(asn_TYPE_descriptor_t *td, ...@@ -783,11 +783,34 @@ INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
#endif /* ASN_DISABLE_PER_SUPPORT */ #endif /* ASN_DISABLE_PER_SUPPORT */
/*
* This function is only to get rid of Undefined Behavior Sanitizer warning.
*/
static intmax_t CLANG_NO_SANITIZE("shift-base")
asn__safe_integer_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;
}
int int
asn_INTEGER2imax(const INTEGER_t *iptr, intmax_t *lptr) { asn_INTEGER2imax(const INTEGER_t *iptr, intmax_t *lptr) {
uint8_t *b, *end; uint8_t *b, *end;
size_t size; size_t size;
intmax_t value;
/* Sanity checking */ /* Sanity checking */
if(!iptr || !iptr->buf || !lptr) { if(!iptr || !iptr->buf || !lptr) {
...@@ -800,11 +823,11 @@ asn_INTEGER2imax(const INTEGER_t *iptr, intmax_t *lptr) { ...@@ -800,11 +823,11 @@ asn_INTEGER2imax(const INTEGER_t *iptr, intmax_t *lptr) {
size = iptr->size; size = iptr->size;
end = b + size; /* Where to stop */ end = b + size; /* Where to stop */
if(size > sizeof(value)) { if(size > sizeof(intmax_t)) {
uint8_t *end1 = end - 1; uint8_t *end1 = end - 1;
/* /*
* Slightly more advanced processing, * Slightly more advanced processing,
* able to process INTEGERs with >sizeof(value) bytes * able to process INTEGERs with >sizeof(intmax_t) bytes
* when the actual value is small, e.g. for intmax_t == int32_t * when the actual value is small, e.g. for intmax_t == int32_t
* (0x0000000000abcdef INTEGER would yield a fine 0x00abcdef int32_t) * (0x0000000000abcdef INTEGER would yield a fine 0x00abcdef int32_t)
*/ */
...@@ -818,8 +841,8 @@ asn_INTEGER2imax(const INTEGER_t *iptr, intmax_t *lptr) { ...@@ -818,8 +841,8 @@ asn_INTEGER2imax(const INTEGER_t *iptr, intmax_t *lptr) {
} }
size = end - b; size = end - b;
if(size > sizeof(value)) { if(size > sizeof(intmax_t)) {
/* Still cannot fit the sizeof(value) */ /* Still cannot fit the sizeof(intmax_t) */
errno = ERANGE; errno = ERANGE;
return -1; return -1;
} }
...@@ -831,16 +854,7 @@ asn_INTEGER2imax(const INTEGER_t *iptr, intmax_t *lptr) { ...@@ -831,16 +854,7 @@ asn_INTEGER2imax(const INTEGER_t *iptr, intmax_t *lptr) {
return 0; return 0;
} }
/* Perform the sign initialization */ *lptr = asn__safe_integer_convert_helper(b, end);
/* 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;
}
*lptr = value;
return 0; return 0;
} }
......
...@@ -104,7 +104,7 @@ typedef unsigned int uint32_t; ...@@ -104,7 +104,7 @@ typedef unsigned int uint32_t;
#endif /* _WIN32 */ #endif /* _WIN32 */
#if __GNUC__ >= 3 #if __GNUC__ >= 3 || defined(__clang__)
#ifndef GCC_PRINTFLIKE #ifndef GCC_PRINTFLIKE
#define GCC_PRINTFLIKE(fmt,var) __attribute__((format(printf,fmt,var))) #define GCC_PRINTFLIKE(fmt,var) __attribute__((format(printf,fmt,var)))
#endif #endif
...@@ -120,6 +120,12 @@ typedef unsigned int uint32_t; ...@@ -120,6 +120,12 @@ typedef unsigned int uint32_t;
#endif #endif
#endif #endif
#if defined(__clang__)
#define CLANG_NO_SANITIZE(what) __attribute__((no_sanitize(what)))
#else
#define CLANG_NO_SANITIZE(what)
#endif
/* Figure out if thread safety is requested */ /* Figure out if thread safety is requested */
#if !defined(ASN_THREAD_SAFE) && (defined(THREAD_SAFE) || defined(_REENTRANT)) #if !defined(ASN_THREAD_SAFE) && (defined(THREAD_SAFE) || defined(_REENTRANT))
#define ASN_THREAD_SAFE #define ASN_THREAD_SAFE
......
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