All of lore.kernel.org
 help / color / mirror / Atom feed
From: Pavel Roskin <proski@gnu.org>
To: The development of GRUB 2 <grub-devel@gnu.org>
Subject: Re: a bug in read
Date: Wed, 09 Jul 2008 02:14:32 -0400	[thread overview]
Message-ID: <1215584072.31230.9.camel@dv> (raw)
In-Reply-To: <200807080018.35215.okuji@enbug.org>

On Tue, 2008-07-08 at 00:18 +0200, Yoshinori K. Okuji wrote:
> Hello,
> 
> I have noticed that read.c has a bug. In this line:
> 
>   while ((line[i - 1] != '\n') && (line[i - 1] != '\r'))
> 
> LINE is not initialized yet at the first time, so this refers to a 
> uninitialized location.

Thank you!  What's worse, i is 0, so we are reading outside the buffer.
I think this patch should do what the code was meant to do:

diff --git a/commands/read.c b/commands/read.c
index 1995918..96519f8 100644
--- a/commands/read.c
+++ b/commands/read.c
@@ -30,15 +30,16 @@ grub_getline (void)
   int i;
   char *line;
   char *tmp;
+  char last = 0;
 
   i = 0;
   line = grub_malloc (1 + i + sizeof('\0'));
   if (! line)
     return NULL;
 
-  while ((line[i - 1] != '\n') && (line[i - 1] != '\r'))
+  while ((last != '\n') && (last != '\r'))
     {
-      line[i] = grub_getkey ();
+      last = line[i] = grub_getkey ();
       if (grub_isprint (line[i]))
 	grub_putchar (line[i]);
       i++;


We should test all grub utilities in Valgrind to find such problems.

By the way, read is not a part of grub-emu.  We'll need to improve the
build system to make such oversights less likely.  We also need "exit"
in grub-emu, as "reboot" doesn't sound right.

-- 
Regards,
Pavel Roskin



  reply	other threads:[~2008-07-09  6:14 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-07-07 22:18 a bug in read Yoshinori K. Okuji
2008-07-09  6:14 ` Pavel Roskin [this message]
2008-07-09 17:05   ` Vesa Jääskeläinen
2008-07-09 17:56   ` Javier Martín
2008-07-11 18:23     ` Pavel Roskin

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=1215584072.31230.9.camel@dv \
    --to=proski@gnu.org \
    --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.