On Tue, Oct 30, 2018 at 11:01:15AM +0100, Didier Kryn wrote: > Le 29/10/2018 à 22:42, Adam Borowski a écrit : > > Well, it is possible to use strcpy() right. On the other hand, _every_ use > > of strncpy() for a C string is a bug. > > Let's assume "the programmer knows what she is doing" (this is how the C > language assume its insecurity). Then she knows the length of the > destination is smaller than the length of the source. It is an error, but, > in some cases (which we are dealing with here), it is acceptable to just > truncate the string. So what can she do? > > Certainly strcpy() is unusable because it would overflow. The fastest > way to do the job with the standard C library is to invoke strncpy() with > sizeof(dest)-1 and append a null.
In that case, memcpy() is strictly better: * faster: + no need to compare every byte + can copy longer strings a word at a time * safer: you don't get the false impression it'd null-terminate the string for you > The two other methods are to use snprintf() or to copy the bytes one by > one in a loop. Or copy word by word -- or even via a mem-to-mem DMA transfer some folks want to add to new machines. Or... Meow! -- ⢀⣴⠾⠻⢶⣦⠀ Have you heard of the Amber Road? For thousands of years, the ⣾⠁⢰⠒⠀⣿⡁ Romans and co valued amber, hauled through the Europe over the ⢿⡄⠘⠷⠚⠋⠀ mountains and along the Vistula, from Gdańsk. To where it came ⠈⠳⣄⠀⠀⠀⠀ together with silk (judging by today's amber stalls). _______________________________________________ Dng mailing list Dng@lists.dyne.org https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng