On Sat, May 30, 2020 at 12:20:19AM +0200, Harald Anlauf wrote: > > Gesendet: Freitag, 29. Mai 2020 um 23:57 Uhr > > Von: "H.J. Lu" <hjl.to...@gmail.com> > > > This breaks bootstrap: > > > > https://gcc.gnu.org/pipermail/gcc-regression/2020-May/072642.html > > > > ../../src-master/gcc/fortran/class.c:487:13: error: ‘char* > > strncpy(char*, const char*, size_t)’ specified bound 67 equals > > destination size [-Werror=stringop-truncation] > > 487 | strncpy (dt_name, gfc_dt_upper_string (derived->name), > > sizeof (dt_name)); > > what is the right way to deal with that? > > I haven't seen any use of strlcpy in the gcc sources. This would do > the right thing and would fit here. > > So should one use the clumsy way: > > strncpy(buf, str, buflen - 1); > if (buflen > 0) > buf[buflen - 1]= '\0'; > > Or is there something more convenient that keeps gcc happy?
Depends on what exactly you want to achieve, but strncpy is pretty much always the wrong answer. Do you really want to clear the rest of buf, when gfc_dt_upper_string is much shorter? That is one thing that is only seldom useful from strncpy behavior. The other is that it doesn't zero terminate on truncation. I'd suggest e.g. remember gfc_dt_upper_string result in a temporary, compute len = strnlen (res, sizeof (dt_name) - 1); and then you can just memcpy and zero terminate buf[len] = '\0'; Or do you ever want to truncate? Jakub