Skip to contentSkip to conversation

Bug fix for loadenv.c

PATCH In review Bean 3 participants 4 messages Sep 3, 2009 – Sep 4, 2009

lore
Share & tools
lore

Bean

author
#0 lore raw

PATCH 1/1 Bug fix for loadenv.c

Hi,

This is the bug fix for command/loadenv.c. Here is a small program to illustrate what happen:

#include <stdio.h>
#include <stdlib.h>

struct data
{
struct data *next;
int length;
};

struct data *
new_data ()
{
struct data *p;

  p = malloc (sizeof (*p));
  p->length = 1;
  p->next = 0;

return p;
}

int
main ()
{
int index;
struct data *head, *p;

  head = new_data ();
  for (p = head, index = 0; p; p = p->next, index += p->length)
    printf ("%d\n", index);

return 0;
}

Compile it with gcc-4.0 from OSX:
i686-apple-dawin8-gcc-4.0.1 -Os test.c

The assembly dump shows:

L4:

	movl	%edi, 4(%esp)
	movl	-28(%ebp), %eax
	movl	%eax, (%esp)
	call	L_printf$stub
	movl	(%esi), %esi
	addl	4(%esi), %edi
	jmp	L4

Why would it uses unconditional jumps ? I think it's some over optimization in gcc. We have index += p->length after p = p->next, if p is null, p->length would be invalid, so gcc figure that p would never be null and the end condition is always false. In fact, the bug is in the code itself, even if it tests p, in OS environment, access null pointer is not allowed, so index += p->length can't be executed anyway.

The correct way is:

  for (p = head, index = 0; p;)
  {
    printf ("%d\n", index);
   p = p->next;
  if (p)
   index += p->length;
  }
Signature · 4 lines
Bean

gitgrub home: http://github.com/grub/grub/
my fork page: http://github.com/bean123/grub/
commands/loadenv.c +10 -2
index 51cffd3..15dc49a 100644
@@ -240,7 +240,7 @@ check_blocklists (grub_envblk_t envblk, struct blocklist *blocklists,
   part_start = grub_partition_get_start (disk->partition);
 
   buf = grub_envblk_buffer (envblk);
-  for (p = blocklists, index = 0; p; p = p->next, index += p->length)
+  for (p = blocklists, index = 0; p;)
     {
       char blockbuf[GRUB_DISK_SECTOR_SIZE];
 
@@ -253,6 +253,10 @@ check_blocklists (grub_envblk_t envblk, struct blocklist *blocklists,
           grub_error (GRUB_ERR_FILE_READ_ERROR, "invalid blocklist");
           return 0;
         }
+
+      p = p->next;
+      if (p)
+	index += p->length;
     }
 
   return 1;
@@ -273,11 +277,15 @@ write_blocklists (grub_envblk_t envblk, struct blocklist *blocklists,
   part_start = grub_partition_get_start (disk->partition);
 
   index = 0;
-  for (p = blocklists; p; p = p->next, index += p->length)
+  for (p = blocklists; p;)
     {
       if (grub_disk_write (disk, p->sector - part_start,
                            p->offset, p->length, buf + index))
         return 0;
+
+      p = p->next;
+      if (p)
+	index += p->length;
     }
 
   return 1;
#1 lore raw

Hi,

Maybe that compiler has been configured with -fdelete-null-pointer-checks as
the default?

--S

Quoting Bean, who wrote the following on Fri, 4 Sep 2009:

Quoted 78 lines

Hi,

This is the bug fix for command/loadenv.c. Here is a small program to illustrate what happen:

#include <stdio.h>
#include <stdlib.h>

struct data
{
struct data *next;
int length;
};

struct data *
new_data ()
{
struct data *p;

p = malloc (sizeof (*p));
p->length = 1;
p->next = 0;

return p;
}

int
main ()
{
int index;
struct data *head, *p;

head = new_data ();
for (p = head, index = 0; p; p = p->next, index += p->length)
printf ("%d\n", index);

return 0;
}

Compile it with gcc-4.0 from OSX:
i686-apple-dawin8-gcc-4.0.1 -Os test.c

The assembly dump shows:

L4:

	movl	%edi, 4(%esp)
	movl	-28(%ebp), %eax
	movl	%eax, (%esp)
	call	L_printf$stub
	movl	(%esi), %esi
	addl	4(%esi), %edi
	jmp	L4

Why would it uses unconditional jumps ? I think it's some over optimization in gcc. We have index += p->length after p = p->next, if p is null, p->length would be invalid, so gcc figure that p would never be null and the end condition is always false. In fact, the bug is in the code itself, even if it tests p, in OS environment, access null pointer is not allowed, so index += p->length can't be executed anyway.

The correct way is:

for (p = head, index = 0; p;)

 {
   printf ("%d\n", index);

p = p->next;
if (p)
index += p->length;
}

Signature · 4 lines
Bean

gitgrub home: http://github.com/grub/grub/
my fork page: http://github.com/bean123/grub/
#2 lore raw
In reply to Bean
Quoted 4 lines · Bean

Hi,

This is the bug fix for command/loadenv.c. Here is a small program to illustrate what happen:

Update your branch:

2009-09-01 Vladimir Serbinenko <[email protected]>

* commands/loadenv.c (check_blocklists): Fix off-by-one error. (write_blocklists): Likewise.

Quoted 80 lines

#include <stdio.h>
#include <stdlib.h>

struct data
{
 struct data *next;
 int length;
};

struct data *
new_data ()
{
 struct data *p;

 p = malloc (sizeof (*p));
 p->length = 1;
 p->next = 0;

 return p;
}

int
main ()
{
 int index;
 struct data *head, *p;

 head = new_data ();
 for (p = head, index = 0; p; p = p->next, index += p->length) printf ("%d\n", index);

 return 0;
}

Compile it with gcc-4.0 from OSX:
i686-apple-dawin8-gcc-4.0.1 -Os test.c

The assembly dump shows:

L4:
       movl    %edi, 4(%esp)
       movl    -28(%ebp), %eax
       movl    %eax, (%esp)
       call    L_printf$stub
       movl    (%esi), %esi
       addl    4(%esi), %edi
       jmp     L4

Why would it uses unconditional jumps ? I think it's some over optimization in gcc. We have index += p->length after p = p->next, if p is null, p->length would be invalid, so gcc figure that p would never be null and the end condition is always false. In fact, the bug is in the code itself, even if it tests p, in OS environment, access null pointer is not allowed, so index += p->length can't be executed anyway.

The correct way is:

 for (p = head, index = 0; p;)
 {
   printf ("%d\n", index);
  p = p->next;
 if (p)
  index += p->length;
 }

--
Bean

gitgrub home: http://github.com/grub/grub/
my fork page: http://github.com/bean123/grub/

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

Signature · 4 lines
Regards
Vladimir 'phcoder' Serbinenko

Personal git repository: http://repo.or.cz/w/grub2/phcoder.git
author
#3 lore raw
Quoted 11 lines · Vladimir 'phcoder' Serbinenko
Quoted 4 lines · Bean

Hi,

This is the bug fix for command/loadenv.c. Here is a small program to illustrate what happen:

Update your branch:

2009-09-01  Vladimir Serbinenko  <[email protected]>

       * commands/loadenv.c (check_blocklists): Fix off-by-one error. (write_blocklists): Likewise.

Hi,

Oh sorry, I miss that one, sync with cvs now.

But then I'm a little confused, if you have found the bug, then you should already know this is not gcc's fault, why would you suggest to bump up gcc's version to 4.2 ?

Signature · 4 lines
Bean

gitgrub home: http://github.com/grub/grub/
my fork page: http://github.com/bean123/grub/