This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/81117] Improve buffer overflow checking in strncpy


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);

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]