All of lore.kernel.org
 help / color / mirror / Atom feed
* strndup broken
@ 2005-01-08 23:48 Hollis Blanchard
  2005-01-21 20:19 ` Marco Gerards
  0 siblings, 1 reply; 4+ messages in thread
From: Hollis Blanchard @ 2005-01-08 23:48 UTC (permalink / raw)
  To: grub-devel

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  <iga20@cam.ac.uk>

	* 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 *



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2005-01-22 14:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-01-08 23:48 strndup broken Hollis Blanchard
2005-01-21 20:19 ` Marco Gerards
2005-01-22  5:38   ` Hollis Blanchard
2005-01-22 13:46     ` Marco Gerards

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.