From: Nick Mathewson Date: Mon, 19 May 2025 01:11:00 +0000 (-0400) Subject: Fix a new GCC warning about strings. X-Git-Tag: tor-0.4.8.17~8^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=097aab5b60e8ec23beebde80a6c3fde755e093b4;p=thirdparty%2Ftor.git Fix a new GCC warning about strings. When we say something like ``` const char foo[3] = "foo"; ``` GCC now complains, because there is no space for the terminating NUL. But we use this construction in a lot of places in our tests to initialize test digests, keys, and so on. So to resolve the issue, we have to mark these strings with a new attribute. --- diff --git a/configure.ac b/configure.ac index 2ec9ecb4e0..bc8ce18729 100644 --- a/configure.ac +++ b/configure.ac @@ -570,6 +570,21 @@ if test "$tor_cv_c_attr_fallthrough" = "yes"; then AC_DEFINE(HAVE_ATTR_FALLTHROUGH, [1], [defined if we have the fallthrough attribute.]) fi +saved_CFLAGS="$CFLAGS" +CFLAGS="$CFLAGS -Werror" +AC_CACHE_CHECK([for __attribute__((nonstring))], + tor_cv_c_attr_nonstring, + [AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM([], + [[ __attribute__((nonstring)) const char foo[5] = "abcde"; ]])], + [tor_cv_c_attr_nonstring=yes], + [tor_cv_c_attr_nonstring=no] )]) +CFLAGS="$saved_CFLAGS" + +if test "$tor_cv_c_attr_nonstring" = "yes"; then + AC_DEFINE(HAVE_ATTR_NONSTRING, [1], [defined if we have the nonstring attribute.]) +fi + TORUSER=_tor AC_ARG_WITH(tor-user, AS_HELP_STRING(--with-tor-user=NAME, [specify username for tor daemon]), diff --git a/src/lib/cc/compat_compiler.h b/src/lib/cc/compat_compiler.h index 991b33d9e7..8ab86e8832 100644 --- a/src/lib/cc/compat_compiler.h +++ b/src/lib/cc/compat_compiler.h @@ -79,6 +79,12 @@ #define FALLTHROUGH #endif +#if defined(HAVE_ATTR_NONSTRING) +#define NONSTRING __attribute__((nonstring)) +#else +#define NONSTRING +#endif + /* What GCC do we have? */ #ifdef __GNUC__ #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) diff --git a/src/test/test.c b/src/test/test.c index 2030a8336e..5d36c43a1c 100644 --- a/src/test/test.c +++ b/src/test/test.c @@ -221,7 +221,8 @@ test_ntor_handshake(void *arg) /* shared */ const curve25519_public_key_t *server_pubkey; - uint8_t node_id[20] = "abcdefghijklmnopqrst"; + + NONSTRING uint8_t node_id[20] = "abcdefghijklmnopqrst"; (void) arg; diff --git a/src/test/test_bridges.c b/src/test/test_bridges.c index f778710e1b..fea1cff244 100644 --- a/src/test/test_bridges.c +++ b/src/test/test_bridges.c @@ -253,7 +253,7 @@ test_bridges_get_configured_bridge_by_addr_port_digest_digest_only(void *arg) { char digest[DIGEST_LEN]; bridge_info_t *bridge; - const char fingerprint[HEX_DIGEST_LEN] = + NONSTRING const char fingerprint[HEX_DIGEST_LEN] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; tor_addr_t *addr = tor_malloc(sizeof(tor_addr_t)); char ret_addr[16]; @@ -321,7 +321,7 @@ test_bridges_get_configured_bridge_by_exact_addr_port_digest_donly(void *arg) { char digest[DIGEST_LEN]; bridge_info_t *bridge; - const char fingerprint[HEX_DIGEST_LEN] = + NONSTRING const char fingerprint[HEX_DIGEST_LEN] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; tor_addr_t *addr = tor_malloc(sizeof(tor_addr_t)); uint16_t port = 11111; @@ -353,7 +353,7 @@ test_bridges_get_configured_bridge_by_exact_addr_port_digest_both(void *arg) { char digest[DIGEST_LEN]; bridge_info_t *bridge; - const char fingerprint[HEX_DIGEST_LEN] = + NONSTRING const char fingerprint[HEX_DIGEST_LEN] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; tor_addr_t *addr = tor_malloc(sizeof(tor_addr_t)); uint16_t port = 4444; @@ -419,7 +419,7 @@ test_bridges_find_bridge_by_digest_known(void *arg) { char digest1[DIGEST_LEN]; bridge_info_t *bridge; - const char fingerprint[HEX_DIGEST_LEN] = + NONSTRING const char fingerprint[HEX_DIGEST_LEN] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; helper_add_bridges_to_bridgelist(arg); @@ -616,10 +616,10 @@ test_bridges_node_is_a_configured_bridge(void *arg) node_t node_with_digest; memset(&node_with_digest, 0, sizeof(node_with_digest)); - const char fingerprint[HEX_DIGEST_LEN] = + NONSTRING const char fingerprint[HEX_DIGEST_LEN] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; - const char fingerprint2[HEX_DIGEST_LEN] = + NONSTRING const char fingerprint2[HEX_DIGEST_LEN] = "ffffffffffffffffffffffffffffffffffffffff"; base16_decode(node_with_digest.identity, DIGEST_LEN, diff --git a/src/test/test_cell_formats.c b/src/test/test_cell_formats.c index b7b149cd66..6ed69d1f8b 100644 --- a/src/test/test_cell_formats.c +++ b/src/test/test_cell_formats.c @@ -32,7 +32,7 @@ static void test_cfmt_relay_header(void *arg) { relay_header_t rh; - const uint8_t hdr_1[RELAY_HEADER_SIZE] = + NONSTRING const uint8_t hdr_1[RELAY_HEADER_SIZE] = "\x03" "\x00\x00" "\x21\x22" "ABCD" "\x01\x03"; uint8_t hdr_out[RELAY_HEADER_SIZE]; (void)arg; diff --git a/src/test/test_circuitlist.c b/src/test/test_circuitlist.c index 4bcff57fc3..004235beda 100644 --- a/src/test/test_circuitlist.c +++ b/src/test/test_circuitlist.c @@ -187,9 +187,9 @@ test_rend_token_maps(void *arg) { or_circuit_t *c1, *c2, *c3, *c4; origin_circuit_t *c5; - const uint8_t tok1[REND_TOKEN_LEN] = "The cat can't tell y"; - const uint8_t tok2[REND_TOKEN_LEN] = "ou its name, and it "; - const uint8_t tok3[REND_TOKEN_LEN] = "doesn't really care."; + NONSTRING const uint8_t tok1[REND_TOKEN_LEN] = "The cat can't tell y"; + NONSTRING const uint8_t tok2[REND_TOKEN_LEN] = "ou its name, and it "; + NONSTRING const uint8_t tok3[REND_TOKEN_LEN] = "doesn't really care."; /* -- Adapted from a quote by Fredrik Lundh. */ (void)arg; @@ -420,7 +420,7 @@ test_hs_circuitmap_isolation(void *arg) ed25519_public_key_t intro_pk2 = { {2} }; /* Junk, not important. */ { - const uint8_t tok1[REND_TOKEN_LEN] = "bet i got some of th"; + NONSTRING const uint8_t tok1[REND_TOKEN_LEN] = "bet i got some of th"; circ1 = or_circuit_new(0, NULL); tt_assert(circ1); @@ -442,7 +442,7 @@ test_hs_circuitmap_isolation(void *arg) } { - const uint8_t tok2[REND_TOKEN_LEN] = "you dont know anythi"; + NONSTRING const uint8_t tok2[REND_TOKEN_LEN] = "you dont know anythi"; circ2 = origin_circuit_new(); tt_assert(circ2); diff --git a/src/test/test_crypto_ope.c b/src/test/test_crypto_ope.c index a17af181db..1df6678211 100644 --- a/src/test/test_crypto_ope.c +++ b/src/test/test_crypto_ope.c @@ -28,7 +28,7 @@ test_crypto_ope_consistency(void *arg) const int TEST_VALS[] = { 5, 500, 1023, 1024, 1025, 2046, 2047, 2048, 2049, 10000, OPE_INPUT_MAX }; unsigned i; - const uint8_t key[32] = "A fixed key, chosen arbitrarily."; + NONSTRING const uint8_t key[32] = "A fixed key, chosen arbitrarily."; ope = crypto_ope_new(key); tt_assert(ope); @@ -56,7 +56,7 @@ test_crypto_ope_oob(void *arg) (void)arg; crypto_ope_t *ope = NULL; - const uint8_t key[32] = "A fixed key, chosen arbitrarily."; + NONSTRING const uint8_t key[32] = "A fixed key, chosen arbitrarily."; ope = crypto_ope_new(key); tt_u64_op(UINT64_MAX, OP_EQ, crypto_ope_encrypt(ope,INT_MIN)); diff --git a/src/test/test_dir.c b/src/test/test_dir.c index d09afcb9e6..d0e17d4122 100644 --- a/src/test/test_dir.c +++ b/src/test/test_dir.c @@ -2544,7 +2544,7 @@ test_dir_dirserv_read_measured_bandwidths(void *arg) smartlist_len(bw_file_headers)); /* force bw_file_headers to be bigger than * MAX_BW_FILE_HEADER_COUNT_IN_VOTE */ - char line[8] = "foo=bar\0"; + NONSTRING char line[8] = "foo=bar\0"; smartlist_add_strdup(bw_file_headers, line); tt_int_op(MAX_BW_FILE_HEADER_COUNT_IN_VOTE, OP_LT, smartlist_len(bw_file_headers)); @@ -4450,14 +4450,17 @@ test_dir_bwauth_bw_file_digest256(void *arg) char *fname = tor_strdup(get_fname("V3BandwidthsFile")); /* Initialize to a wrong digest. */ - uint8_t digest[DIGEST256_LEN] = "01234567890123456789abcdefghijkl"; + NONSTRING + uint8_t digest[DIGEST256_LEN] = "01234567890123456789abcdefghijkl"; /* Digest of an empty string. Initialize to a wrong digest. */ - char digest_empty_str[DIGEST256_LEN] = "01234567890123456789abcdefghijkl"; + NONSTRING + char digest_empty_str[DIGEST256_LEN] = "01234567890123456789abcdefghijkl"; crypto_digest256(digest_empty_str, "", 0, DIGEST_SHA256); /* Digest of the content. Initialize to a wrong digest. */ - char digest_expected[DIGEST256_LEN] = "01234567890123456789abcdefghijkl"; + NONSTRING + char digest_expected[DIGEST256_LEN] = "01234567890123456789abcdefghijkl"; crypto_digest256(digest_expected, content, strlen(content), DIGEST_SHA256); /* When the bandwidth file can not be found. */ diff --git a/src/test/test_extorport.c b/src/test/test_extorport.c index 201a702d19..559ecd6397 100644 --- a/src/test/test_extorport.c +++ b/src/test/test_extorport.c @@ -166,7 +166,7 @@ test_ext_or_cookie_auth(void *arg) size_t reply_len=0; char hmac1[32], hmac2[32]; - const char client_nonce[32] = + NONSTRING const char client_nonce[32] = "Who is the third who walks alway"; char server_hash_input[] = "ExtORPort authentication server-to-client hash" diff --git a/src/test/test_relaycrypt.c b/src/test/test_relaycrypt.c index 3a615c53a3..ec0891ef44 100644 --- a/src/test/test_relaycrypt.c +++ b/src/test/test_relaycrypt.c @@ -19,7 +19,7 @@ #include "test/test.h" -static const char KEY_MATERIAL[3][CPATH_KEY_MATERIAL_LEN] = { +NONSTRING static const char KEY_MATERIAL[3][CPATH_KEY_MATERIAL_LEN] = { " 'My public key is in this signed x509 object', said Tom assertively.", "'Let's chart the pedal phlanges in the tomb', said Tom cryptographically", " 'Segmentation fault bugs don't _just happen_', said Tom seethingly.", @@ -189,4 +189,3 @@ struct testcase_t relaycrypt_tests[] = { TEST(inbound), END_OF_TESTCASES }; - diff --git a/src/test/test_util_format.c b/src/test/test_util_format.c index f0ee58a445..027d6a0be9 100644 --- a/src/test/test_util_format.c +++ b/src/test/test_util_format.c @@ -13,7 +13,7 @@ static void test_util_format_unaligned_accessors(void *ignored) { (void)ignored; - char buf[9] = "onionsoup"; // 6f6e696f6e736f7570 + NONSTRING char buf[9] = "onionsoup"; // 6f6e696f6e736f7570 tt_u64_op(get_uint64(buf+1), OP_EQ, tor_htonll(UINT64_C(0x6e696f6e736f7570)));