Messages in this thread | | | Date | Sat, 16 Aug 2003 05:04:15 -0500 | From | Daniel Forrest <> | Subject | Re: generic strncpy - off-by-one error |
| |
On Sat, Aug 16, 2003 at 11:19:30AM +0200, Peter Kjellerstedt wrote: > > Actually, it should be: > > while (count && ((long)tmp & (sizeof(long) - 1))) >
Oops, you're right, I forgot that the count could be small.
But, now that I think of it, maybe this would be best...
char *strncpy(char *dest, const char *src, size_t count) { char *tmp = dest; while (count && *src) { *tmp++ = *src++; count--; } - if (count) { + if (count >= sizeof(long)) { size_t count2; - while (count && ((long)tmp & (sizeof(long) - 1))) { + while ((long)tmp & (sizeof(long) - 1)) { *tmp++ = '\0'; count--; } count2 = count / sizeof(long); while (count2) { *((long *)tmp)++ = '\0'; count2--; } count &= (sizeof(long) - 1); - while (count) { - *tmp++ = '\0'; - count--; - } + } + + while (count) { + *tmp++ = '\0'; + count--; } return dest; }
-- Dan - To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
| |