|
From: Greg H.
|
I sent a bug report about this to the author a few days ago. It's
definitely not right. Here's my patch, based on what strncat() does.
--- mac_replace_strmem.c 2003/08/07 02:52:24 1.1
+++ mac_replace_strmem.c 2003/08/07 02:56:35
@@ -186,13 +186,15 @@
char* strncpy ( char* dst, const char* src, int n )
{
- Char* dst_orig = dst;
+ const Char* src_orig = src;
+ Char* dst_orig = dst;
Int m = 0;
- if (is_overlap(dst, src, n, n))
- complain3("strncpy", dst, src, n);
-
while (m < n && *src) { m++; *dst++ = *src++; }
+ /* Check for overlap after copying; all n bytes of dst are elevant,
+ but only m+1 bytes of src if terminator was found */
+ if (is_overlap(dst_orig, src_orig, n, (m < n) ? m+1 : n))
+ complain3("strncpy", dst, src, n);
while (m++ < n) *dst++ = 0; /* must pad remainder with nulls */
return dst_orig;
|