• Rong Tao's avatar
    selftests/bpf: cgroup_helpers.c: Fix strncpy() fortify warning · b3c09fdc
    Rong Tao authored
    Copy libbpf_strlcpy() from libbpf_internal.h to bpf_util.h, and rename it
    to bpf_strlcpy(), then replace selftests strncpy()/libbpf_strlcpy() with
    bpf_strlcpy(), fix compile warning.
    
    The libbpf_internal.h header cannot be used directly here, because
    references to cgroup_helpers.c in samples/bpf will generate compilation
    errors. We also can't add libbpf_strlcpy() directly to bpf_util.h,
    because the definition of libbpf_strlcpy() in libbpf_internal.h is
    duplicated. In order not to modify the libbpf code, add a new function
    bpf_strlcpy() to selftests bpf_util.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: default avatarRong Tao <rongtao@cestc.cn>
    Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/tencent_469D8AF32BD56816A29981BED06E96D22506@qq.com
    b3c09fdc
bpf_util.h 1.59 KB