strlcpy
Posted Jun 12, 2003 4:50 UTC (Thu) by
ncm (subscriber, #165)
Parent article:
strlcpy
Worse, each instance of strlcpy in a program indicates a
potential bug in its own right. strlcpy has been rejected
by Posix, I am told, at least twice, for that reason. While
less likely to participate in a buffer overflow, it is likely to
introduce more subtle bugs.
First, since it performs a partial copy, it's easy for code
(even code that checks and acts correctly on its return
value) to end up using the partial, incorrect copy.
Second, if told it has a zero-length buffer, it doesn't even
null-terminate it, leaving whatever was already present in the
buffer in place.
A much more useful definition, if there must be something like
strlcpy, would be to call abort() if the argument string doesn't
fit in the buffer. (If called from within the Linux kernel, an Oops
would not be out of place.) The only circumstance I can
think of where a partial copy wouldn't always be worse than entirely
failing is when copying an error message into a fixed-size display
field. That case seems to me rare enough not to merit such a
complicated and dangerous function.
If strlcpy is part of the kernel's library, I can only hope that
any patch that relies on it is rejected as too poorly coded. A set
of "canary" utility functions used to detect bad code would not
be a bad addition to the kernel, but their purpose would be hard
to keep secret.
(
Log in to post comments)