Skip to content

Commit e26bafd

Browse files
authored
gh-133256: Add _Py_NONSTRING macro (#133257)
Fix GCC 15 compiler warnings such as: Modules/fcntlmodule.c:27:36: warning: initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute (9 chars into 8 available) [-Wunterminated-string-initialization] static const char guard[GUARDSZ] = "\x00\xfa\x69\xc4\x67\xa3\x6c\x58";
1 parent ad2f088 commit e26bafd

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

Include/internal/pycore_debug_offsets.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ extern "C" {
5959

6060

6161
typedef struct _Py_DebugOffsets {
62-
char cookie[8];
62+
char cookie[8] _Py_NONSTRING;
6363
uint64_t version;
6464
uint64_t free_threaded;
6565
// Runtime state offset;

Include/pyport.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,4 +685,19 @@ extern "C" {
685685
#endif
686686

687687

688+
// _Py_NONSTRING: The nonstring variable attribute specifies that an object or
689+
// member declaration with type array of char, signed char, or unsigned char,
690+
// or pointer to such a type is intended to store character arrays that do not
691+
// necessarily contain a terminating NUL.
692+
//
693+
// Usage:
694+
//
695+
// char name [8] _Py_NONSTRING;
696+
#if _Py__has_attribute(nonstring)
697+
# define _Py_NONSTRING __attribute__((nonstring))
698+
#else
699+
# define _Py_NONSTRING
700+
#endif
701+
702+
688703
#endif /* Py_PYPORT_H */

Modules/fcntlmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
#define GUARDSZ 8
2626
// NUL followed by random bytes.
27-
static const char guard[GUARDSZ] = "\x00\xfa\x69\xc4\x67\xa3\x6c\x58";
27+
static const char guard[GUARDSZ] _Py_NONSTRING = "\x00\xfa\x69\xc4\x67\xa3\x6c\x58";
2828

2929
/*[clinic input]
3030
module fcntl

0 commit comments

Comments
 (0)