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

* Re: grub_strdup() error; or: grub_malloc() zeroes memory?
  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
  0 siblings, 1 reply; 3+ messages in thread
From: Vesa Jääskeläinen @ 2008-06-28 15:25 UTC (permalink / raw)
  To: The development of GRUB 2

Colin D Bennett wrote:
> 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);
>    }

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

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

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




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

* Re: grub_strdup() error; or: grub_malloc() zeroes memory?
  2008-06-28 15:25 ` Vesa Jääskeläinen
@ 2008-06-28 15:35   ` Colin D Bennett
  0 siblings, 0 replies; 3+ messages in thread
From: Colin D Bennett @ 2008-06-28 15:35 UTC (permalink / raw)
  To: grub-devel

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

On Sat, 28 Jun 2008 18:25:01 +0300
Vesa Jääskeläinen <chaac@nic.fi> wrote:

> Colin D Bennett wrote:
> > 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);
> >    }
> 
> Zero is copied from source string... notice strlen() + 1.

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

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