]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Fix a new GCC warning about strings.
authorNick Mathewson <nickm@torproject.org>
Mon, 19 May 2025 01:11:00 +0000 (21:11 -0400)
committerNick Mathewson <nickm@torproject.org>
Mon, 19 May 2025 01:32:20 +0000 (21:32 -0400)
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.

configure.ac
src/lib/cc/compat_compiler.h
src/test/test.c
src/test/test_bridges.c
src/test/test_cell_formats.c
src/test/test_circuitlist.c
src/test/test_crypto_ope.c
src/test/test_dir.c
src/test/test_extorport.c
src/test/test_relaycrypt.c
src/test/test_util_format.c

index 2ec9ecb4e0a47b89bd6882984c7c19cc3565c7cb..bc8ce18729168ef4b6212d00bf959be038fc55eb 100644 (file)
@@ -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]),
index 991b33d9e7f77ddbac4d38052a687b54341646cd..8ab86e88326aee4e2e7b25995f01bc1aceb98695 100644 (file)
 #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__)
index 2030a8336ec3daab1691e514004bc9a5eb5247de..5d36c43a1c3043e3faddb1949c55fa4623e20ba2 100644 (file)
@@ -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;
 
index f778710e1be1bf51c66315fe7fe684baa585925c..fea1cff24445c7ff9ccfe8955026196820d40e45 100644 (file)
@@ -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,
index b7b149cd668ae52d509b6df69b30fc4bbc2a7923..6ed69d1f8b649160370fc28e7add5964e0362ef5 100644 (file)
@@ -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;
index 4bcff57fc38383c9e5bc2976ec40f0e4dfd4ee39..004235beda79312e5dfb900d5e8d4f979646469e 100644 (file)
@@ -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);
index a17af181db444b4d64a2ec0514564d2a1dad9350..1df6678211579ad2a5093f5bb41f1636e697159f 100644 (file)
@@ -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));
index d09afcb9e683497f7ba08f0b69ab1b71e9c9fc52..d0e17d4122eb69af4255ab00eaad54f5df79eb85 100644 (file)
@@ -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. */
index 201a702d1932a15d8b09930de860d8a0b26f1a03..559ecd63972c3749cdb9d0c3224e51aca4ad7aac 100644 (file)
@@ -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"
index 3a615c53a35872950c42cfa853fc6d0fa7e74e89..ec0891ef4426f72d035fde8bb6df3f3f59913cba 100644 (file)
@@ -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
 };
-
index f0ee58a445368996faa75a1483bf1cf35ce0b253..027d6a0be9ab0c4886cf4e08c95e758de5a4ddcd 100644 (file)
@@ -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)));