Re: [Patch] Zoran strncpy() cleanup

From: Horst Schirmeier
Date: Mon Jun 05 2006 - 18:04:24 EST


On Mon, 05 Jun 2006, Eric Sesterhenn wrote:
> > Problem is, the strings are (possibly) still not zero-terminated:
> > strncpy() only appends zeroes if src contents are short enough; if they
> > are not, dest is only zero-terminated if dest[sizeof(dest)-1] was zero
> > before.
> > strlcpy() semantics promise more sanity; dest is always zero-terminated
> > (if its size is >= 1), and the size parameter holds total dest size.
> > (See lib/string.c for more details.)
>
> In all cases there is a memset() which sets the entire structure to
> zero. Since we never write to the last byte with the strncpy() it will
> be null terminated. But if you think strlcpy() is safer for the future,
> i can make you a third patch.

I did not dig in the surrounding code that deeply, no doubt you're right
about the zero termination issue.

I just _personally_ think strlcpy(dest, src, sizeof(*dest)); looks
more sane than strncpy(dest, src, sizeof(*dest)-1);, and it additionally
does not rely on dest being zero-terminated beforehand.

Kind regards,
Horst

--
PGP-Key 0xD40E0E7A
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/