[bpf-next] selftests/bpf: cgroup_helpers.c: Fix strncpy() fortify warning
Commit Message
From: Rong Tao <rongtao@cestc.cn>
Move libbpf_strlcpy() to libbpf_common.h, and replace strncpy() with
libbpf_strlcpy(), fix compile warning.
We can't use libbpf_internal.h directly, because it introduces a lot of
header dependency issues. So move libbpf_strlcpy() into libbpf_common.h,
and if you need to use the libbpf_strlcpy() function, you need to include
the header file libbpf.h
How to reproduce this compilation warning:
$ make -C samples/bpf
cgroup_helpers.c: In function ‘__enable_controllers’:
cgroup_helpers.c:80:17: warning: ‘strncpy’ specified bound 4097 equals destination size [-Wstringop-truncation]
80 | strncpy(enable, controllers, sizeof(enable));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: Rong Tao <rongtao@cestc.cn>
---
tools/lib/bpf/libbpf_common.h | 19 +++++++++++++++++++
tools/lib/bpf/libbpf_internal.h | 19 -------------------
tools/testing/selftests/bpf/cgroup_helpers.c | 3 ++-
tools/testing/selftests/bpf/xsk.c | 19 -------------------
4 files changed, 21 insertions(+), 39 deletions(-)
Comments
On Wed, Nov 2, 2022 at 5:59 AM Rong Tao <rtoax@foxmail.com> wrote:
>
> From: Rong Tao <rongtao@cestc.cn>
>
> Move libbpf_strlcpy() to libbpf_common.h, and replace strncpy() with
> libbpf_strlcpy(), fix compile warning.
>
> We can't use libbpf_internal.h directly, because it introduces a lot of
> header dependency issues. So move libbpf_strlcpy() into libbpf_common.h,
> and if you need to use the libbpf_strlcpy() function, you need to include
> the header file libbpf.h
well, no, we should make libbpf_strlcpy as a public API, it's internal
helper. libbpf_common.h is part of libbpf's UAPI.
So don't touch libbpf, please. Name the function as _strlcpy or
something like that, put it into bpf_util.h and use that from
selftests.
>
> How to reproduce this compilation warning:
>
> $ make -C samples/bpf
> cgroup_helpers.c: In function ‘__enable_controllers’:
> cgroup_helpers.c:80:17: warning: ‘strncpy’ specified bound 4097 equals destination size [-Wstringop-truncation]
> 80 | strncpy(enable, controllers, sizeof(enable));
> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> Signed-off-by: Rong Tao <rongtao@cestc.cn>
> ---
> tools/lib/bpf/libbpf_common.h | 19 +++++++++++++++++++
> tools/lib/bpf/libbpf_internal.h | 19 -------------------
> tools/testing/selftests/bpf/cgroup_helpers.c | 3 ++-
> tools/testing/selftests/bpf/xsk.c | 19 -------------------
> 4 files changed, 21 insertions(+), 39 deletions(-)
>
[...]
@@ -70,4 +70,23 @@
}; \
})
+/* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst
+ * is zero-terminated string no matter what (unless sz == 0, in which case
+ * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs
+ * in what is returned. Given this is internal helper, it's trivial to extend
+ * this, when necessary. Use this instead of strncpy inside libbpf source code.
+ */
+static inline void libbpf_strlcpy(char *dst, const char *src, size_t sz)
+{
+ size_t i;
+
+ if (sz == 0)
+ return;
+
+ sz--;
+ for (i = 0; i < sz && src[i]; i++)
+ dst[i] = src[i];
+ dst[i] = '\0';
+}
+
#endif /* __LIBBPF_LIBBPF_COMMON_H */
@@ -191,25 +191,6 @@ static inline void *libbpf_reallocarray(void *ptr, size_t nmemb, size_t size)
return realloc(ptr, total);
}
-/* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst
- * is zero-terminated string no matter what (unless sz == 0, in which case
- * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs
- * in what is returned. Given this is internal helper, it's trivial to extend
- * this, when necessary. Use this instead of strncpy inside libbpf source code.
- */
-static inline void libbpf_strlcpy(char *dst, const char *src, size_t sz)
-{
- size_t i;
-
- if (sz == 0)
- return;
-
- sz--;
- for (i = 0; i < sz && src[i]; i++)
- dst[i] = src[i];
- dst[i] = '\0';
-}
-
__u32 get_kernel_version(void);
struct btf;
@@ -11,6 +11,7 @@
#include <fcntl.h>
#include <unistd.h>
#include <ftw.h>
+#include <bpf/libbpf.h>
#include "cgroup_helpers.h"
@@ -77,7 +78,7 @@ static int __enable_controllers(const char *cgroup_path, const char *controllers
enable[len] = 0;
close(fd);
} else {
- strncpy(enable, controllers, sizeof(enable));
+ libbpf_strlcpy(enable, controllers, sizeof(enable));
}
snprintf(path, sizeof(path), "%s/cgroup.subtree_control", cgroup_path);
@@ -521,25 +521,6 @@ static int xsk_create_bpf_link(struct xsk_socket *xsk)
return 0;
}
-/* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst
- * is zero-terminated string no matter what (unless sz == 0, in which case
- * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs
- * in what is returned. Given this is internal helper, it's trivial to extend
- * this, when necessary. Use this instead of strncpy inside libbpf source code.
- */
-static inline void libbpf_strlcpy(char *dst, const char *src, size_t sz)
-{
- size_t i;
-
- if (sz == 0)
- return;
-
- sz--;
- for (i = 0; i < sz && src[i]; i++)
- dst[i] = src[i];
- dst[i] = '\0';
-}
-
static int xsk_get_max_queues(struct xsk_socket *xsk)
{
struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };