[klibc] [PATCH] strncat appends not enough, doesnt terminate correctly

Olaf Hering olh at suse.de
Fri Jan 21 10:40:47 PST 2005


The klibc strncat doesnt behave like described in the man page.
It doesnt terminate the string if size < strlen(src).
It doesnt make dst longer than size.
This breaks scsi_id from udev/extras

#include <string.h>
#include <stdio.h>
int main(void)
{
        unsigned char olh[42];
        memset(olh, 'A', sizeof(olh));
        sprintf(olh, "abc");
        fprintf(stderr, "olh: '%s'\n", olh);
        bar(olh, "123456789", 7);
        fprintf(stderr, "olh: '%s'\n", olh);
  return 0;
}

olh: 'abc'
olh: 'abc1234AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'


Index: klibc/strncat.c
===================================================================
RCS file: /home/cvs/klibc/klibc/klibc/strncat.c,v
retrieving revision 1.3
diff -u -p -r1.3 strncat.c
--- klibc/strncat.c	1 Oct 2004 05:28:35 -0000	1.3
+++ klibc/strncat.c	21 Jan 2005 18:26:47 -0000
@@ -12,14 +12,12 @@ char *strncat(char *dst, const char *src
   char ch;
   size_t nn = q-dst;
 
-  if ( __likely(nn <= n) )
-    n -= nn;
-
   while (n--) {
     *q++ = ch = *p++;
     if ( !ch )
-      break;
+      return dst;
   }
+  *q = '\0';
 
   return dst;
 }



More information about the klibc mailing list