LWN.net Logo

Off by one?

Off by one?

Posted May 29, 2003 22:21 UTC (Thu) by raph (guest, #326)
In reply to: strlcpy() by nas
Parent article: strlcpy()

I'm pretty sure you have an off-by-one error there - your code can write up to (size+1) bytes of dst, while from the paper it looks like the correct semantics are to write up to size bytes only - so that strlcat(buf, src, sizeof(buf)) is safe.


(Log in to post comments)

Off by one?

Posted May 30, 2003 4:40 UTC (Fri) by ncm (subscriber, #165) [Link]

I agree, Neil's is buggy. Walk through it with size == 1. It clobbers one beyond the end of the input array.

I wonder if we should bother about what to do if size == 0. Mine crashes spectacularly, which is a Good Thing.

Getting within 15% of memcpy is pretty damn good, in my estimation. Of course I didn't read Linus's version, or OpenBSD's; that would be cheating, and I would be tainted besides. Of course now that I have been told, via cleanroom methods, I can adjust mine to be equally fast, and maybe (one can hope) actually identical to both Linus's and OpenBSD's.

Off by one?

Posted May 30, 2003 16:28 UTC (Fri) by tjc (guest, #137) [Link]

Getting within 15% of memcpy is pretty damn good, in my estimation. Of course I didn't read Linus's version, or OpenBSD's; that would be cheating, and I would be tainted besides. Of course now that I have been told, via cleanroom methods, I can adjust mine to be equally fast, and maybe (one can hope) actually identical to both Linus's and OpenBSD's.

You're probably going to have to copy more than one char at a time to match memcpy() for speed.

Copyright © 2012, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds