grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

strndup broken


From: Hollis Blanchard
Subject: strndup broken
Date: Sat, 8 Jan 2005 17:48:20 -0600
User-agent: Mutt/1.5.6+20040907i

A while back I made the unpleasent discovery that grub_strndup was
broken. This patch fixes it, but the author has been having trouble
sending it out.

-Hollis

2005-01-08  Ian Abel  <address@hidden>

        * kern/misc.c (grub_strndup): Don't call grub_strlen.
        Null-terminate the new string if possible.

Index: kern/misc.c
===================================================================
RCS file: /cvsroot/grub/grub2/kern/misc.c,v
retrieving revision 1.15
diff -u -p -r1.15 misc.c
--- kern/misc.c 14 Sep 2004 08:56:51 -0000      1.15
+++ kern/misc.c 8 Jan 2005 23:47:55 -0000
@@ -341,16 +341,21 @@ grub_strndup (const char *s, grub_size_t
 {
   grub_size_t len = 0;
   char *p = (char *) s;
-  
+
   while (*(p++) && len < n)
     len++;
 
-  len = grub_strlen (s) + 1;
+  if (*p)
+    len++;
+
   p = (char *) grub_malloc (len);
   if (! p)
     return 0;
 
-  return grub_memcpy (p, s, len);
+  if (n < len)
+    p[n] = '\0';
+
+  return grub_memcpy (p, s, n);
 }
 
 void *




reply via email to

[Prev in Thread] Current Thread [Next in Thread]