Compiler warning about strncpy

Environment

Fedora 29, gcc 8.3.1, running Kickstart downloaded 2021-07-23

Problem/Question

While compiling, the compiler emitted these warnings:

  GEN      netdata
In function 'update_disk_table',
    inlined from 'read_local_disks' at collectors/ebpf.plugin/ebpf_disk.c:347:13:
collectors/ebpf.plugin/ebpf_disk.c:292:9: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-overflow=]
         strncpy(disk_list->family, name, length);
         ^
collectors/ebpf.plugin/ebpf_disk.c: In function 'read_local_disks':
collectors/ebpf.plugin/ebpf_disk.c:288:18: note: length computed here
         length = strlen(name);
                  ^
In function 'update_disk_table',
    inlined from 'read_local_disks' at collectors/ebpf.plugin/ebpf_disk.c:347:13:
collectors/ebpf.plugin/ebpf_disk.c:280:9: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-overflow=]
         strncpy(w->family, name, length);
         ^
collectors/ebpf.plugin/ebpf_disk.c: In function 'read_local_disks':
collectors/ebpf.plugin/ebpf_disk.c:276:18: note: length computed here
         length = strlen(name);
                  ^

What I expected to happen

No warnings. :slight_smile:

Hey, @cho :wave: Thanks for reporting!

@Thiago_Marques_0 please, take a look.

Hello @cho ,

This looks like a problem only with this gcc version, because I am not having it with latest gcc. I will install the same fedora version to fix this for old versions. Thank you to report!

Best regards,

Thiago

Hello @cho ,

Firstly sorry for the delay, but I was on vacation when I saw your message.

I observed that this warning given by gcc 8.x does not make much sense, because we are testing the source size before to do the copy, as you can see in this code.

After to install the same Fedora 29 and get the same gcc version, I could replicate the problem, but when I am compiling with gcc 10.3.0, I do not have more warnings, so I think GNU fixed the bug with their compile. On Monday I will take a look again and try to remove this warning for old GCCs.

Best Regards!

PR Remove warning when GCC 8.x is used by thiagoftsm · Pull Request #11389 · netdata/netdata · GitHub was made to fix this issue.
Thank you @cho !