Skip to contentSkip to conversation

a bug in read

DISCUSSION Yoshinori K. Okuji 4 participants 5 messages Jul 7, 2008 – Jul 11, 2008

lore
Share & tools
lore
#0 lore raw

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.

Regards,
Okuji

#1 lore raw
Quoted 8 lines · Yoshinori K. Okuji

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:

commands/read.c +3 -2
index 1995918..96519f8 100644
@@ -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.

Signature · 2 lines
Regards,
Pavel Roskin
#2 lore raw
Quoted 3 lines · Pavel Roskin

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.

I think there should be emu arch that provides architecture specific functionality. This way we use exactly same code paths for everything. Then we can catch bugs more easily.

#3 lore raw
In reply to Pavel Roskin

Couldn't this have been worked around changing while to do-while and refactoring the loop instead of creating a new variable, whose handling takes space? I'm not sure it can be done because I'm on vacation and reading mail through dialup access, but it might be worth a try.

2008/7/9, Pavel Roskin <[email protected]>:

Quoted 54 lines · Yoshinori K. Okuji
Quoted 8 lines

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:

commands/read.c +3 -2
index 1995918..96519f8 100644
@@ -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

_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel

#4 lore raw
Quoted 5 lines · Javier Martín

Couldn't this have been worked around changing while to do-while and refactoring the loop instead of creating a new variable, whose handling takes space? I'm not sure it can be done because I'm on vacation and reading mail through dialup access, but it might be worth a try.

A new variable was useful anyway to strip the final newline, but changing the loop logic made the code more readable. It's a standard case where the exit condition occurs in the middle of the block, not in the beginning or in the end.

The patch has been applied.

Signature · 2 lines
Regards,
Pavel Roskin