From 463b9fa0ae5ae056caefb5f6359b145c42b1d3f5 Mon Sep 17 00:00:00 2001 From: Michael Zillgith Date: Wed, 12 Mar 2025 18:01:17 +0000 Subject: [PATCH] - disable asn1c code stack overflow check when ASAN enabled (#539) --- src/mms/iso_mms/asn1c/asn_internal.h | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/mms/iso_mms/asn1c/asn_internal.h b/src/mms/iso_mms/asn1c/asn_internal.h index 5d6910d8..485631bb 100644 --- a/src/mms/iso_mms/asn1c/asn_internal.h +++ b/src/mms/iso_mms/asn1c/asn_internal.h @@ -100,20 +100,39 @@ static inline void ASN_DEBUG(const char *fmt, ...) { (void)fmt; } if(cb(" ", 4, app_key) < 0) return -1; \ } while(0) +#if defined(__SANITIZE_ADDRESS__) + #define ASN_DISABLE_STACK_OVERFLOW_CHECK 1 +#elif defined(__has_feature) +#if __has_feature(address_sanitizer) + #define ASN_DISABLE_STACK_OVERFLOW_CHECK 1 +#endif +#endif + /* * Check stack against overflow, if limit is set. */ #define _ASN_DEFAULT_STACK_MAX (30000) -static inline int -_ASN_STACK_OVERFLOW_CHECK(asn_codec_ctx_t *ctx) { - if(ctx && ctx->max_stack_size) { +#if defined(ASN_DISABLE_STACK_OVERFLOW_CHECK) +static inline int +_ASN_STACK_OVERFLOW_CHECK(asn_codec_ctx_t *ctx) +{ + (void)ctx; + return 0; +} +#else +static inline int +_ASN_STACK_OVERFLOW_CHECK(asn_codec_ctx_t *ctx) +{ + if(ctx && ctx->max_stack_size) + { /* ctx MUST be allocated on the stack */ ptrdiff_t usedstack = ((char *)ctx - (char *)&ctx); if(usedstack > 0) usedstack = -usedstack; /* grows up! */ /* double negative required to avoid int wrap-around */ - if(usedstack < -(ptrdiff_t)ctx->max_stack_size) { + if(usedstack < -(ptrdiff_t)ctx->max_stack_size) + { ASN_DEBUG("Stack limit %ld reached", (long)ctx->max_stack_size); return -1; @@ -121,6 +140,7 @@ _ASN_STACK_OVERFLOW_CHECK(asn_codec_ctx_t *ctx) { } return 0; } +#endif /* defined(ASN_DISABLE_STACK_OVERFLOW_CHECK) */ #ifdef __cplusplus }