Re: generic strncpy - off-by-one error

Timothy Miller
2003-08-18T16:24:44Z

Daniel Forrest wrote:

>
> - if (count) {
> + if (count >= sizeof(long)) {
> size_t count2;
>

I like this size check here, but the comparison should be to some number
greater than sizeof(long). There is a threshold below which it's not
worth it to do all of the extra loops. If you're going to fill only
four bytes, it's probably best to do it just using the last loop.

Maybe through some trial and error, we could determine what that
threshold is. I'm betting it's something around 2* or 3* word size.

[snip]

> +
> + while (count) {
> + *tmp++ = '\0';
> + count--;
> }
>
> return dest;
> }
>