Skip to content

Commit 659631f

Browse files
authored
[3.13] gh-133256: Add _Py_NONSTRING macro (#133257) (#135135)
gh-133256: Add _Py_NONSTRING macro (#133257) Fix GCC 15 compiler warnings such as: In file included from Python/pylifecycle.c:26: Include/internal/pycore_runtime.h:47:26: warning: initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute (9 chars into 8 available) [-Wunterminated-string-initialization] 47 | #define _Py_Debug_Cookie "xdebugpy" | ^~~~~~~~~~ (cherry picked from commit e26bafd)
1 parent ae2e795 commit 659631f

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

Include/internal/pycore_runtime.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ typedef struct _Py_AuditHookEntry {
6060
} _Py_AuditHookEntry;
6161

6262
typedef struct _Py_DebugOffsets {
63-
char cookie[8];
63+
char cookie[8] _Py_NONSTRING;
6464
uint64_t version;
6565
uint64_t free_threaded;
6666
// Runtime state offset;

Include/pyport.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,14 @@ extern "C" {
541541
# define _Py__has_builtin(x) 0
542542
#endif
543543

544+
// Preprocessor check for a compiler __attribute__. Always return 0
545+
// if __has_attribute() macro is not defined.
546+
#ifdef __has_attribute
547+
# define _Py__has_attribute(x) __has_attribute(x)
548+
#else
549+
# define _Py__has_attribute(x) 0
550+
#endif
551+
544552
// _Py_TYPEOF(expr) gets the type of an expression.
545553
//
546554
// Example: _Py_TYPEOF(x) x_copy = (x);
@@ -606,4 +614,20 @@ extern "C" {
606614
# define _SGI_MP_SOURCE
607615
#endif
608616

617+
618+
// _Py_NONSTRING: The nonstring variable attribute specifies that an object or
619+
// member declaration with type array of char, signed char, or unsigned char,
620+
// or pointer to such a type is intended to store character arrays that do not
621+
// necessarily contain a terminating NUL.
622+
//
623+
// Usage:
624+
//
625+
// char name [8] _Py_NONSTRING;
626+
#if _Py__has_attribute(nonstring)
627+
# define _Py_NONSTRING __attribute__((nonstring))
628+
#else
629+
# define _Py_NONSTRING
630+
#endif
631+
632+
609633
#endif /* Py_PYPORT_H */

0 commit comments

Comments
 (0)