[Ksummit-discuss] [MAINTAINERS SUMMIT] API replacement/deprecation

Theodore Y. Ts'o tytso at mit.edu
Fri Sep 7 20:56:07 UTC 2018


On Fri, Sep 07, 2018 at 10:30:04PM +0200, Arnd Bergmann wrote:
> 
> I suspect that a lot of the cases that want NUL-padding also don't
> want NUL-termination: when you store a string on disk in a fixed-length
> record or transfer it over the network, you don't want to leak stack
> data to the medium, but you also don't need the terminating character
> because you know the maximum length already.
> 
> strncpy() does exactly the right thing for that case, it's just that
> this pattern is now a corner case, and gcc tends to flag such
> usage with a warning about missing termination (unless you
> use __nonstring) but doesn't flag the more common usage when
> it looks correct.

Yeah, the case I have is when I'm copying from a NUL-terminated string
into a fixed char array.  So if we had a function called
"copy_string_to_char_array" (we'll figure out a better name later)
which takes a source, destination, and size parameter, and which does
the functional equivalent of:

	memset(dest, 0, size);
	strncpy(src, dest, size);

... we could do something that's more efficient than the above, and
does exactly what I'm looking for in this case.

Of course, there could be other corner cases where strncpy() is
justified; this is just the use case I care about.  :-)

     	     	       	       	    - Ted


More information about the Ksummit-discuss mailing list