]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
kernel-netlink: Fix compiler warnings with strncpy()
authorTobias Brunner <tobias@strongswan.org>
Thu, 15 Sep 2022 08:08:10 +0000 (10:08 +0200)
committerTobias Brunner <tobias@strongswan.org>
Thu, 15 Sep 2022 10:16:12 +0000 (12:16 +0200)
Normally, GCC sees that we terminate the destination with a zero byte.
However, when using `-fsanitize=address`, there seems to be additional
instrumentation code after strncpy() so GCC produces warnings like
these:

‘__builtin_strncpy’ specified bound 16 equals destination size [-Wstringop-truncation]

src/libcharon/plugins/kernel_netlink/kernel_netlink_ipsec.c
src/libcharon/plugins/kernel_netlink/kernel_netlink_net.c

index 6fac8c873c8a1f44dfcd8b9ceac754b1579125c8..ca6ce175ba646918a5da2fddf75358894ccbf519 100644 (file)
@@ -1411,7 +1411,7 @@ static void netlink_find_offload_feature(const char *ifname)
                .cmd = ETHTOOL_GSSET_INFO,
                .sset_mask = 1ULL << ETH_SS_FEATURES,
        );
-       strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
+       strncpy(ifr.ifr_name, ifname, IFNAMSIZ-1);
        ifr.ifr_name[IFNAMSIZ-1] = '\0';
        ifr.ifr_data = (void*)sset_info;
 
@@ -1427,7 +1427,7 @@ static void netlink_find_offload_feature(const char *ifname)
                .cmd = ETHTOOL_GSTRINGS,
                .string_set = ETH_SS_FEATURES,
        );
-       strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
+       strncpy(ifr.ifr_name, ifname, IFNAMSIZ-1);
        ifr.ifr_name[IFNAMSIZ-1] = '\0';
        ifr.ifr_data = (void*)cmd;
 
@@ -1486,7 +1486,7 @@ static bool netlink_detect_offload(const char *ifname)
                .cmd = ETHTOOL_GFEATURES,
                .size = netlink_hw_offload.total_blocks,
        );
-       strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
+       strncpy(ifr.ifr_name, ifname, IFNAMSIZ-1);
        ifr.ifr_name[IFNAMSIZ-1] = '\0';
        ifr.ifr_data = (void*)cmd;
 
@@ -1778,8 +1778,8 @@ METHOD(kernel_ipsec_t, add_sa, status_t,
                        }
                        algo->alg_key_len = data->enc_key.len * 8;
                        algo->alg_icv_len = icv_size;
-                       strncpy(algo->alg_name, alg_name, sizeof(algo->alg_name));
-                       algo->alg_name[sizeof(algo->alg_name) - 1] = '\0';
+                       strncpy(algo->alg_name, alg_name, sizeof(algo->alg_name)-1);
+                       algo->alg_name[sizeof(algo->alg_name)-1] = '\0';
                        memcpy(algo->alg_key, data->enc_key.ptr, data->enc_key.len);
                        break;
                }
@@ -1805,8 +1805,8 @@ METHOD(kernel_ipsec_t, add_sa, status_t,
                                goto failed;
                        }
                        algo->alg_key_len = data->enc_key.len * 8;
-                       strncpy(algo->alg_name, alg_name, sizeof(algo->alg_name));
-                       algo->alg_name[sizeof(algo->alg_name) - 1] = '\0';
+                       strncpy(algo->alg_name, alg_name, sizeof(algo->alg_name)-1);
+                       algo->alg_name[sizeof(algo->alg_name)-1] = '\0';
                        memcpy(algo->alg_key, data->enc_key.ptr, data->enc_key.len);
                }
        }
@@ -1862,8 +1862,8 @@ METHOD(kernel_ipsec_t, add_sa, status_t,
                        }
                        algo->alg_key_len = data->int_key.len * 8;
                        algo->alg_trunc_len = trunc_len;
-                       strncpy(algo->alg_name, alg_name, sizeof(algo->alg_name));
-                       algo->alg_name[sizeof(algo->alg_name) - 1] = '\0';
+                       strncpy(algo->alg_name, alg_name, sizeof(algo->alg_name)-1);
+                       algo->alg_name[sizeof(algo->alg_name)-1] = '\0';
                        memcpy(algo->alg_key, data->int_key.ptr, data->int_key.len);
                }
                else
@@ -1877,8 +1877,8 @@ METHOD(kernel_ipsec_t, add_sa, status_t,
                                goto failed;
                        }
                        algo->alg_key_len = data->int_key.len * 8;
-                       strncpy(algo->alg_name, alg_name, sizeof(algo->alg_name));
-                       algo->alg_name[sizeof(algo->alg_name) - 1] = '\0';
+                       strncpy(algo->alg_name, alg_name, sizeof(algo->alg_name)-1);
+                       algo->alg_name[sizeof(algo->alg_name)-1] = '\0';
                        memcpy(algo->alg_key, data->int_key.ptr, data->int_key.len);
                }
        }
@@ -1904,8 +1904,8 @@ METHOD(kernel_ipsec_t, add_sa, status_t,
                        goto failed;
                }
                algo->alg_key_len = 0;
-               strncpy(algo->alg_name, alg_name, sizeof(algo->alg_name));
-               algo->alg_name[sizeof(algo->alg_name) - 1] = '\0';
+               strncpy(algo->alg_name, alg_name, sizeof(algo->alg_name)-1);
+               algo->alg_name[sizeof(algo->alg_name)-1] = '\0';
        }
 
        if (data->encap)
index 97309a6d259e85c3926251da5645fbc5684c49fb..7dcb15676afc45a124eaed2714a5ebc547436cf0 100644 (file)
@@ -1126,7 +1126,7 @@ static void process_link(private_kernel_netlink_net_t *this,
                                );
                                this->ifaces->insert_last(this->ifaces, entry);
                        }
-                       strncpy(entry->ifname, name, IFNAMSIZ);
+                       strncpy(entry->ifname, name, IFNAMSIZ-1);
                        entry->ifname[IFNAMSIZ-1] = '\0';
                        entry->usable = charon->kernel->is_interface_usable(charon->kernel,
                                                                                                                                name);