lkml.org 
[lkml]   [2003]   [Aug]   [12]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: generic strncpy - off-by-one error
On Tue Aug 12, 2003 at 10:18:21PM -0400, Albert Cahalan wrote:
> You're all wrong. This is some kind of programming
> test for sure!
>
> Let us imagine that glibc has a correct version.
> By exhaustive testing, I found a version that works.
> Here it is, along with the test code:
>
> //////////////////////////////////////////////////////
> #define _GNU_SOURCE
> #include <string.h>
> #include <stdlib.h>
> #include <stdio.h>
>
> // first correct implementation!
> char * strncpy_good(char *dest, const char *src, size_t count){
> char *tmp = dest;
> memset(dest,'\0',count);
> while (count-- && (*tmp++ = *src++))
> ;
> return dest;
> }

char *strncpy(char * s1, const char * s2, size_t n)
{
register char *s = s1;
while (n) {
if ((*s = *s2) != 0) s2++;
++s;
--n;
}
return s1;
}

-Erik

--
Erik B. Andersen http://codepoet-consulting.com/
--This message was written using 73% post-consumer electrons--
-
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/

\
 
 \ /
  Last update: 2005-03-22 13:47    [W:0.082 / U:0.996 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site