This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/81117] Improve buffer overflow checking in strncpy
- From: "bernd.edlinger at hotmail dot de" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Sat, 02 Dec 2017 14:59:11 +0000
- Subject: [Bug c/81117] Improve buffer overflow checking in strncpy
- Auto-submitted: auto-generated
- References: <bug-81117-4@http.gcc.gnu.org/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81117
Bernd Edlinger <bernd.edlinger at hotmail dot de> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |bernd.edlinger at hotmail dot de
--- Comment #20 from Bernd Edlinger <bernd.edlinger at hotmail dot de> ---
this breaks glibc-2.26:
../sysdeps/unix/sysv/linux/if_index.c: In function '__if_nametoindex':
../sysdeps/unix/sysv/linux/if_index.c:46:3: error: 'strncpy' specified bound 16
equals destination size [-Werror=stringop-truncation]
strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors
make[2]: *** [/home/ed/gnu/glibc-build/inet/if_index.o] Error 1
but in this case the code is correct:
struct ifreq ifr;
[...]
strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
if (__ioctl (fd, SIOCGIFINDEX, &ifr) < 0)
Because ifr_name does not need zero-termination.
And the following would not be correct, but does not warn:
strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)-1);