Commit b5570727c8 for openssl.org
commit b5570727c8062fe996b0d0afb5a27806a3c23d56
Author: Bob Beck <beck@openssl.org>
Date: Wed Aug 5 13:30:19 2026 -0600
Add OPENSSL_NONSTRING and apply it to ASN1_STRING's data field
OPENSSL_NONSTRING expands to __attribute__((nonstring)) where the compiler
supports it and to nothing otherwise. ASN1_STRING data has an explicit
length and is not necessarily NUL terminated, so marking the field lets GCC
diagnose direct use of it with C string functions such as strlen(), strcpy()
or "%s".
The diagnostic fires only on GCC under an optimised build; other compilers,
including current Clang, accept the attribute but do not warn.
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Andrew Dinh <andrewd@openssl.org>
Reviewed-by: Nikola Pajkovsky <nikolap@openssl.org>
Merge-date: Thu Aug 27 13:52:51 2026
Merged-from: https://github.com/openssl/openssl/pull/32178
diff --git a/include/crypto/asn1.h b/include/crypto/asn1.h
index bc556588d0..546d86a411 100644
--- a/include/crypto/asn1.h
+++ b/include/crypto/asn1.h
@@ -50,7 +50,7 @@
struct asn1_string_st {
int length;
int type;
- unsigned char *data;
+ unsigned char *data OPENSSL_NONSTRING;
/*
* The value of the following field depends on the type being held. It
* is mostly being used for BIT_STRING so if the input data has a
diff --git a/include/openssl/e_os2.h b/include/openssl/e_os2.h
index f3cda73da1..8cec931f55 100644
--- a/include/openssl/e_os2.h
+++ b/include/openssl/e_os2.h
@@ -284,6 +284,22 @@ typedef uint64_t ossl_uintmax_t;
#define ossl_unused
#endif
+/*
+ * OPENSSL_NONSTRING: mark a char/unsigned char buffer object or struct field
+ * whose contents are not necessarily NUL terminated, so that misuse with C
+ * string functions (strlen(), strcpy(), "%s", ...) is diagnosed by compilers
+ * that support the attribute. It has no effect elsewhere.
+ */
+#if defined(__has_attribute)
+#if __has_attribute(nonstring)
+#define OPENSSL_NONSTRING __attribute__((nonstring))
+#else
+#define OPENSSL_NONSTRING
+#endif
+#else
+#define OPENSSL_NONSTRING
+#endif
+
#ifdef __cplusplus
}
#endif