All of lore.kernel.org
 help / color / mirror / Atom feed
* grub_strdup() error; or: grub_malloc() zeroes memory?
@ 2008-06-28 15:03 Colin D Bennett
  2008-06-28 15:25 ` Vesa Jääskeläinen
  0 siblings, 1 reply; 3+ messages in thread
From: Colin D Bennett @ 2008-06-28 15:03 UTC (permalink / raw)
  To: grub-devel

[-- Attachment #1: Type: text/plain, Size: 1043 bytes --]

It looks like grub_strdup() does not terminate the returned string with
a 0 byte.  The only way I could see it working is if grub_malloc()
filled the returned memory with zeroes.  Does it?

From kern/misc.c: (circa line 476)

   char *
   grub_strdup (const char *s)
   {
     grub_size_t len;
     char *p;
     
     len = grub_strlen (s) + 1;
     p = (char *) grub_malloc (len);
     if (! p)
       return 0;

     return grub_memcpy (p, s, len);
   }

But right after that, we have

   char *
   grub_strndup (const char *s, grub_size_t n)
   {
     grub_size_t len;
     char *p;
     
     len = grub_strlen (s);
     if (len > n)
       len = n;
     p = (char *) grub_malloc (len + 1);
     if (! p)
       return 0;
     
     grub_memcpy (p, s, len);
     p[len] = '\0';
     return p;
   }

which explicitly stores a terminating null byte.  If grub_malloc() did
initialize the memory to zero, then this explicity store would be
unnecessary.

Am I missing something?

Regards,
Colin

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 197 bytes --]

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

end of thread, other threads:[~2008-06-28 15:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-06-28 15:03 grub_strdup() error; or: grub_malloc() zeroes memory? Colin D Bennett
2008-06-28 15:25 ` Vesa Jääskeläinen
2008-06-28 15:35   ` Colin D Bennett

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.