Skip to content

Commit c64f1fa

Browse files
committed
[sw] Add nonstring attribute
Adds the `nonstring` attribute through a new `OT_NONSTRING` macro. This is used to prevent warnings when compiling with Clang 21+ and an array is initialized with a string literal but is sized not to include the terminating null. Signed-off-by: Luís Marques <[email protected]>
1 parent cf3e351 commit c64f1fa

File tree

10 files changed

+29
-17
lines changed

10 files changed

+29
-17
lines changed

sw/device/lib/base/macros.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,18 @@ extern "C++" {
624624
*/
625625
#define OT_USED __attribute__((used))
626626

627+
/**
628+
* An attribute used to indicate that a character array variable is not intended
629+
* to be treated as a null-terminated string.
630+
*/
631+
#if defined(__clang__) && __clang_major__ >= 21
632+
#define OT_NONSTRING __attribute__((nonstring))
633+
#elif defined(__GNUC__) && !defined(__clang__)
634+
#define OT_NONSTRING __attribute__((nonstring))
635+
#else
636+
#define OT_NONSTRING
637+
#endif
638+
627639
/**
628640
* OT_BUILD_FOR_STATIC_ANALYZER indicates whether we are compiling for the
629641
* purpose of static analysis. Currently, this macro only detects

sw/device/lib/runtime/print.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ enum {
4747

4848
// NOTE: all of the lengths of the strings below are given so that the NUL
4949
// terminator is left off; that way, `sizeof(kConst)` does not include it.
50-
static const char kDigitsLow[16] = "0123456789abcdef";
51-
static const char kDigitsHigh[16] = "0123456789ABCDEF";
50+
OT_NONSTRING static const char kDigitsLow[16] = "0123456789abcdef";
51+
OT_NONSTRING static const char kDigitsHigh[16] = "0123456789ABCDEF";
5252

53-
static const char kErrorNul[17] = "%<unexpected nul>";
54-
static const char kUnknownSpec[15] = "%<unknown spec>";
55-
static const char kErrorTooWide[12] = "%<bad width>";
53+
OT_NONSTRING static const char kErrorNul[17] = "%<unexpected nul>";
54+
OT_NONSTRING static const char kUnknownSpec[15] = "%<unknown spec>";
55+
OT_NONSTRING static const char kErrorTooWide[12] = "%<bad width>";
5656

5757
static size_t base_dev_null(void *data, const char *buf, size_t len) {
5858
return len;
@@ -766,7 +766,7 @@ size_t base_vfprintf(buffer_sink_t out, const char *format, va_list args) {
766766
return bytes_written;
767767
}
768768

769-
const char kBaseHexdumpDefaultFmtAlphabet[256] =
769+
OT_NONSTRING const char kBaseHexdumpDefaultFmtAlphabet[256] =
770770
// clang-format off
771771
// First 32 characters are not printable.
772772
"................................"
@@ -829,7 +829,7 @@ size_t base_fhexdump_with(buffer_sink_t out, base_hexdump_fmt_t fmt,
829829
size_t line_bytes_written = 0;
830830
for (size_t word = 0; word < bytes_per_line; word += fmt.bytes_per_word) {
831831
if (len < line + word) {
832-
char spaces[16] = " ";
832+
OT_NONSTRING char spaces[16] = " ";
833833
while (line_bytes_written < chars_per_line) {
834834
size_t to_print = chars_per_line - line_bytes_written;
835835
if (to_print > sizeof(spaces)) {

sw/device/lib/testing/hmac_testutils.c

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

1313
#define MODULE_ID MAKE_MODULE_ID('h', 'm', 't')
1414

15-
const char kHmacRefData[34] = "Sample message for keylen=blocklen";
15+
OT_NONSTRING const char kHmacRefData[34] = "Sample message for keylen=blocklen";
1616

1717
const uint8_t kHmacRefLongKey[100] = {
1818
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,

sw/device/silicon_creator/lib/dbg_print.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "sw/device/silicon_creator/lib/drivers/uart.h"
1515
#include "sw/device/silicon_creator/lib/epmp_defs.h"
1616

17-
static const char kHexTable[16] = "0123456789abcdef";
17+
OT_NONSTRING static const char kHexTable[16] = "0123456789abcdef";
1818

1919
static void print_integer(unsigned value, bool is_signed) {
2020
char buf[12];

sw/device/silicon_creator/lib/drivers/uart.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ void uart_write(const void *data, size_t len) {
109109

110110
void uart_write_hex(uint32_t val, size_t len, uint32_t after) {
111111
HARDENED_CHECK_LE(len, sizeof(uint32_t));
112-
static const uint8_t kHexTable[16] = "0123456789abcdef";
112+
OT_NONSTRING static const uint8_t kHexTable[16] = "0123456789abcdef";
113113
size_t i = len * 8;
114114
do {
115115
i -= 4;

sw/device/silicon_creator/lib/shutdown.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ static void shutdown_print(shutdown_log_prefix_t prefix, uint32_t val) {
370370
abs_mmio_write32(kUartBase + UART_WDATA_REG_OFFSET, prefix >> 24);
371371

372372
// Print the hex representation of `val`.
373-
const char kHexTable[16] = "0123456789abcdef";
373+
OT_NONSTRING const char kHexTable[16] = "0123456789abcdef";
374374
// `kHexStrLen` is laundered so that it is loaded to a register at every
375375
// iteration.
376376
for (size_t i = 0; i < launder32(kHexStrLen); ++i) {

sw/device/tests/hmac_endianness_test.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ static const dif_hmac_transaction_t kHmacconfig_bigd_bigm = {
3939
.message_endianness = kDifHmacEndiannessBig,
4040
};
4141

42-
static const char kData[142] =
42+
OT_NONSTRING static const char kData[142] =
4343
"Every one suspects himself of at least one of "
4444
"the cardinal virtues, and this is mine: I am "
4545
"one of the few honest people that I have ever "
4646
"known";
4747

48-
static const char kData2[8] = "Help Us ";
49-
static const char kData2_endian[8] = "pleH sU ";
48+
OT_NONSTRING static const char kData2[8] = "Help Us ";
49+
OT_NONSTRING static const char kData2_endian[8] = "pleH sU ";
5050

5151
static const dif_hmac_digest_t kExpectedShaDigest = {
5252
.digest =

sw/device/tests/hmac_error_conditions_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static const dif_hmac_transaction_t kHmacTransactionConfig = {
6060
.message_endianness = kDifHmacEndiannessLittle,
6161
};
6262

63-
static const char kData[142] =
63+
OT_NONSTRING static const char kData[142] =
6464
"Every one suspects himself of at least one of "
6565
"the cardinal virtues, and this is mine: I am "
6666
"one of the few honest people that I have ever "

sw/device/tests/hmac_secure_wipe_test.c

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

1414
OTTF_DEFINE_TEST_CONFIG();
1515

16-
static const char kData[142] =
16+
OT_NONSTRING static const char kData[142] =
1717
"Every one suspects himself of at least one of "
1818
"the cardinal virtues, and this is mine: I am "
1919
"one of the few honest people that I have ever "

sw/device/tests/hmac_smoketest.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static const dif_hmac_transaction_t kHmacTransactionConfig = {
2323
.message_endianness = kDifHmacEndiannessLittle,
2424
};
2525

26-
static const char kData[142] =
26+
OT_NONSTRING static const char kData[142] =
2727
"Every one suspects himself of at least one of "
2828
"the cardinal virtues, and this is mine: I am "
2929
"one of the few honest people that I have ever "

0 commit comments

Comments
 (0)