Disable _ASN_STACK_OVERFLOW_CHECK if building with Asan enabled

Related: OS#6025
Change-Id: I2dda4720f3ea5a023d340863db177e6334beeaa3
diff --git a/include/asn1c/asn_internal.h b/include/asn1c/asn_internal.h
index 580c2e7..0af6bd9 100644
--- a/include/asn1c/asn_internal.h
+++ b/include/asn1c/asn_internal.h
@@ -108,9 +108,34 @@
 /*
  * Check stack against overflow, if limit is set.
  */
+
+/* Since GCC 13, AddressSanitizer started defaulting to
+* ASAN_OPTIONS="detect_stack_use_after_return=1", which makes this check
+* fail due to apparently jumping stack pointers.
+* Hence, disable this check if building with ASan, as documented in:
+* GCC: https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html
+* Clang: https://clang.llvm.org/docs/AddressSanitizer.html#conditional-compilation-with-has-feature-address-sanitizer
+*/
+#if defined(__SANITIZE_ADDRESS__)
+	#define _ASN_SANITIZE_ENABLED 1
+#elif defined(__has_feature)
+#if __has_feature(address_sanitizer)
+	#define _ASN_SANITIZE_ENABLED 1
+#endif
+#endif
+
 #define	_ASN_DEFAULT_STACK_MAX	(30000)
+
+#if defined(_ASN_SANITIZE_ENABLED)
 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 */
@@ -126,6 +151,7 @@
 	}
 	return 0;
 }
+#endif
 
 #ifdef	__cplusplus
 }