Skip to contentSkip to conversation

grub_strdup() error; or: grub_malloc() zeroes memory?

DISCUSSION Colin D Bennett 2 participants 3 messages Jun 28, 2008

lore
Share & tools
lore
#0 lore raw

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

#1 lore raw
Quoted 12 lines · Colin D Bennett

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;
Quoted 7 lines
     len = grub_strlen (s) + 1;
     p = (char *) grub_malloc (len);
     if (! p)
       return 0;

     return grub_memcpy (p, s, len);

}

Zero is copied from source string... notice strlen() + 1.

Quoted 18 lines

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;
Quoted 5 lines

}

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

Here if string is not fully copied there needs to be NUL terminator.

#2 lore raw
Quoted 18 lines · On Sat, 28 Jun 2008 18:25:01 +0300 Vesa Jääskeläinen <[email protected]> wrote:
Quoted 17 lines · Colin D Bennett

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;
Quoted 4 lines
Quoted 2 lines
     return grub_memcpy (p, s, len);

}

Zero is copied from source string... notice strlen() + 1.

Doh!
/me smacks his forehead.
Thanks for pointing that out.

Cheers,
Colin