From: "Vesa Jääskeläinen" <chaac@nic.fi>
To: The development of GRUB 2 <grub-devel@gnu.org>
Subject: Re: grub_strdup() error; or: grub_malloc() zeroes memory?
Date: Sat, 28 Jun 2008 18:25:01 +0300 [thread overview]
Message-ID: <486657CD.9000506@nic.fi> (raw)
In-Reply-To: <20080628080344.76c766f8@gibibit.com>
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.
next prev parent reply other threads:[~2008-06-28 15:25 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2008-06-28 15:35 ` Colin D Bennett
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=486657CD.9000506@nic.fi \
--to=chaac@nic.fi \
--cc=grub-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.