grub-devel
[Top][All Lists]
Advanced

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

Re: strndup broken


From: Marco Gerards
Subject: Re: strndup broken
Date: Fri, 21 Jan 2005 20:19:00 +0000
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

Hollis Blanchard <address@hidden> writes:

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

Because of copyright problems (Ian did not assign his copyrights yet)
and especially because the old function was just plain wrong I have
rewritten it.

Can you check if it works for you?  If I don't hear anything I will
commit it next week or so.

Thanks,
Marco

2005-01-21  Marco Gerards  <address@hidden>

        * kern/misc.c (grub_strndup): Function rewritten.


Index: kern/misc.c
===================================================================
RCS file: /cvsroot/grub/grub2/kern/misc.c,v
retrieving revision 1.16
diff -u -p -u -p -r1.16 misc.c
--- kern/misc.c 1 Nov 2004 16:14:16 -0000       1.16
+++ kern/misc.c 21 Jan 2005 20:15:00 -0000
@@ -354,18 +354,19 @@ grub_strdup (const char *s)
 char *
 grub_strndup (const char *s, grub_size_t n)
 {
-  grub_size_t len = 0;
-  char *p = (char *) s;
+  grub_size_t len;
+  char *p;
   
-  while (*(p++) && len < n)
-    len++;
-
-  len = grub_strlen (s) + 1;
-  p = (char *) grub_malloc (len);
+  len = grub_strlen (s);
+  if (len > n)
+    len = n;
+  p = (char *) grub_malloc (len + 1);
   if (! p)
     return 0;
-
-  return grub_memcpy (p, s, len);
+  
+  grub_memcpy (p, s, len);
+  p[len] = '\0';
+  return p;
 }
 
 void *






reply via email to

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