Skip to content

Commit fa67b67

Browse files
author
MarcoFalke
committed
refactor: Use array initialization for unterminated strings
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"; | ^~~~~~~~~~~~~~~
1 parent e34b476 commit fa67b67

File tree

5 files changed

+12
-12
lines changed

5 files changed

+12
-12
lines changed

examples/schnorr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
#include "examples_util.h"
1919

2020
int main(void) {
21-
unsigned char msg[12] = "Hello World!";
21+
unsigned char msg[] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!'};
2222
unsigned char msg_hash[32];
23-
unsigned char tag[17] = "my_fancy_protocol";
23+
unsigned char tag[] = {'m', 'y', '_', 'f', 'a', 'n', 'c', 'y', '_', 'p', 'r', 'o', 't', 'o', 'c', 'o', 'l'};
2424
unsigned char seckey[32];
2525
unsigned char randomize[32];
2626
unsigned char auxiliary_rand[32];

src/modules/ellswift/tests_impl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,9 @@ void run_ellswift_tests(void) {
406406
/* Test hash initializers. */
407407
{
408408
secp256k1_sha256 sha, sha_optimized;
409-
static const unsigned char encode_tag[25] = "secp256k1_ellswift_encode";
410-
static const unsigned char create_tag[25] = "secp256k1_ellswift_create";
411-
static const unsigned char bip324_tag[26] = "bip324_ellswift_xonly_ecdh";
409+
static const unsigned char encode_tag[] = {'s', 'e', 'c', 'p', '2', '5', '6', 'k', '1', '_', 'e', 'l', 'l', 's', 'w', 'i', 'f', 't', '_', 'e', 'n', 'c', 'o', 'd', 'e'};
410+
static const unsigned char create_tag[] = {'s', 'e', 'c', 'p', '2', '5', '6', 'k', '1', '_', 'e', 'l', 'l', 's', 'w', 'i', 'f', 't', '_', 'c', 'r', 'e', 'a', 't', 'e'};
411+
static const unsigned char bip324_tag[] = {'b', 'i', 'p', '3', '2', '4', '_', 'e', 'l', 'l', 's', 'w', 'i', 'f', 't', '_', 'x', 'o', 'n', 'l', 'y', '_', 'e', 'c', 'd', 'h'};
412412

413413
/* Check that hash initialized by
414414
* secp256k1_ellswift_sha256_init_encode has the expected

src/modules/schnorrsig/main_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static void secp256k1_nonce_function_bip340_sha256_tagged_aux(secp256k1_sha256 *
4545

4646
/* algo argument for nonce_function_bip340 to derive the nonce exactly as stated in BIP-340
4747
* by using the correct tagged hash function. */
48-
static const unsigned char bip340_algo[13] = "BIP0340/nonce";
48+
static const unsigned char bip340_algo[] = {'B', 'I', 'P', '0', '3', '4', '0', '/', 'n', 'o', 'n', 'c', 'e'};
4949

5050
static const unsigned char schnorrsig_extraparams_magic[4] = SECP256K1_SCHNORRSIG_EXTRAPARAMS_MAGIC;
5151

src/modules/schnorrsig/tests_impl.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ static void nonce_function_bip340_bitflip(unsigned char **args, size_t n_flip, s
2121
}
2222

2323
static void run_nonce_function_bip340_tests(void) {
24-
unsigned char tag[13] = "BIP0340/nonce";
25-
unsigned char aux_tag[11] = "BIP0340/aux";
26-
unsigned char algo[13] = "BIP0340/nonce";
24+
unsigned char tag[] = {'B', 'I', 'P', '0', '3', '4', '0', '/', 'n', 'o', 'n', 'c', 'e'};
25+
unsigned char aux_tag[] = {'B', 'I', 'P', '0', '3', '4', '0', '/', 'a', 'u', 'x'};
26+
unsigned char algo[] = {'B', 'I', 'P', '0', '3', '4', '0', '/', 'n', 'o', 'n', 'c', 'e'};
2727
size_t algolen = sizeof(algo);
2828
secp256k1_sha256 sha;
2929
secp256k1_sha256 sha_optimized;
@@ -158,7 +158,7 @@ static void test_schnorrsig_api(void) {
158158
/* Checks that hash initialized by secp256k1_schnorrsig_sha256_tagged has the
159159
* expected state. */
160160
static void test_schnorrsig_sha256_tagged(void) {
161-
unsigned char tag[17] = "BIP0340/challenge";
161+
unsigned char tag[] = {'B', 'I', 'P', '0', '3', '4', '0', '/', 'c', 'h', 'a', 'l', 'l', 'e', 'n', 'g', 'e'};
162162
secp256k1_sha256 sha;
163163
secp256k1_sha256 sha_optimized;
164164

@@ -806,7 +806,7 @@ static void test_schnorrsig_sign(void) {
806806
unsigned char sk[32];
807807
secp256k1_xonly_pubkey pk;
808808
secp256k1_keypair keypair;
809-
const unsigned char msg[32] = "this is a msg for a schnorrsig..";
809+
const unsigned char msg[] = {'t', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 'm', 's', 'g', ' ', 'f', 'o', 'r', ' ', 'a', ' ', 's', 'c', 'h', 'n', 'o', 'r', 'r', 's', 'i', 'g', '.', '.'};
810810
unsigned char sig[64];
811811
unsigned char sig2[64];
812812
unsigned char zeros64[64] = { 0 };

src/testrand_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
static uint64_t secp256k1_test_state[4];
1919

2020
SECP256K1_INLINE static void testrand_seed(const unsigned char *seed16) {
21-
static const unsigned char PREFIX[19] = "secp256k1 test init";
21+
static const unsigned char PREFIX[] = {'s', 'e', 'c', 'p', '2', '5', '6', 'k', '1', ' ', 't', 'e', 's', 't', ' ', 'i', 'n', 'i', 't'};
2222
unsigned char out32[32];
2323
secp256k1_sha256 hash;
2424
int i;

0 commit comments

Comments
 (0)