Follow-up to #1313
Clang should silently follow the main
devel branch, but GCC needs to be bumped manually.
Yeah, I guess one solution would be to be verbose and write the array:
0static const unsigned char PREFIX[19] = {'s', 'e', 'c', 'p', '2', '5', '6', 'k', '1', ' ', 't', 'e', 's', 't', ' ', 'i', 'n', 'i', 't'};
an alternative would be to disable the warning, which seems cleaner and any issues that the warning theoretically could uncover should be detectable by a the existing CI sanitizer builds?
0static const unsigned char PREFIX[19] = {'s', 'e', 'c', 'p', '2', '5', '6', 'k', '1', ' ', 't', 'e', 's', 't', ' ', 'i', 'n', 'i', 't'};
This is ultimately a question of taste, but I suggest this because I think it’s the most readable:
0static const unsigned char PREFIX[] = {'s', 'e', 'c', 'p', '2', '5', '6', 'k', '1', ' ', 't', 'e', 's', 't', ' ', 'i', 'n', 'i', 't'};
This makes it unambiguously clear that we consider it an array and not a string (i.e., it’s not null-terminated), the reader doesn’t need to count the chars, and we can use sizeof(PREFIX)
.
We define such constant arrays in multiple code locations, see https://github.com/bitcoin-core/secp256k1/actions/runs/10356727938/job/28667982412?pr=1583 Whenever I review such code, I need to count the chars manually to check whether the terminating null byte is included or not.
The previous code is correct and harmless to initialize an array with a
non-terminated character sequence using a string literal.
However, it requires exactly specifying the array size, which can be
cumbersome.
Also, GCC-15 may issue the -Wunterminated-string-initialization warning.
[1]
Fix both issues by using array initialization. This refactoring commit
does not change behavior.
[1] Example warning:
src/modules/schnorrsig/main_impl.h:48:46: error: initializer-string for array of 'unsigned char' is too long [-Werror=unterminated-string-initialization]
48 | static const unsigned char bip340_algo[13] = "BIP0340/nonce";
| ^~~~~~~~~~~~~~~
Whenever I review such code, I need to count the chars manually to check whether the terminating null byte is included or not.
Good point about the review overhead and ambiguity. I pushed a commit with your suggestion.
ACK fa67b6752d8ba3e4c41f6c36b1c6b94a21770419, I have reviewed the code and it looks OK.
I can see gcc version 15.0.0 20240811 (experimental) (GCC)
in the CI logs.