Skip to contentSkip to conversation

/kern/file.c BUG

PATCH Oleg Strikov 6 participants 14 messages Jan 23, 2008 – Jan 26, 2008

lore
Share & tools
lore
#0 lore raw

Incorrect behavior of grub_file_open () function in e.g. loop context:

char *file_names[] =
{
"(hd0,1)/file1", //file do not exist
"(hd0,1)/file2" //file exist
};
grub_file_t file;
int i;
for (i = 0; i < 2; i++)

{
     file  = grub_file_open (file_names[i]);
     if (file) {...}
}

There, we should get positive return in the second case (i == 1), but grub_file_open() returns 0.

Using gdb i've found that this problem connected with incorrect errno check in /kern/file.c

Let's look:

Quoted 11 lines

grub_file_t
grub_file_open (const char *name)
{
grub_device_t device;
grub_file_t file = 0;
char *device_name;
char *file_name;

device_name = grub_file_get_device_name (name);
if (grub_errno)
return 0;

But, we DO NOT set grub_errno to 0 at the begining of the function, thats why next loop round it always returns 0

PATCH:

grub2.stable/kern/file.c 2008-01-23 22:45:17.788904935 +0000 renamed from grub2.stable/kern/file.c.orig 2008-01-23 22:30:17.850755634 +0000 +2 -0
@@ -59,6 +59,8 @@ grub_file_open (const char *name)
   char *device_name;
   char *file_name;
 
+  grub_errno = 0;
+
   device_name = grub_file_get_device_name (name);
   if (grub_errno)
     return 0;
#1 lore raw
Quoted 31 lines · Oleg Strikov

Incorrect behavior of grub_file_open () function in e.g. loop context:

char *file_names[] =
{
"(hd0,1)/file1", //file do not exist
"(hd0,1)/file2" //file exist
};
grub_file_t file;
int i;
for (i = 0; i < 2; i++)

{
     file  = grub_file_open (file_names[i]);
     if (file) {...}
}

There, we should get positive return in the second case (i == 1), but grub_file_open() returns 0.

Using gdb i've found that this problem connected with incorrect errno check in /kern/file.c

Let's look:

Quoted 7 lines

grub_file_t
grub_file_open (const char *name)
{
grub_device_t device;
grub_file_t file = 0;
char *device_name;
char *file_name;

Quoted 6 lines
Quoted 3 lines

device_name = grub_file_get_device_name (name);
if (grub_errno)
return 0;

But, we DO NOT set grub_errno to 0 at the begining of the function, thats why next loop round it always returns 0

Fixed, thank you.

Signature · 5 lines
Robert Millan

<GPLv2> I know my rights; I want my phone call!
<DRM> What use is a phone call… if you are unable to speak?
(as seen on /.)
#2 lore raw
Quoted 34 lines · Robert Millan
Quoted 33 lines · Oleg Strikov

Incorrect behavior of grub_file_open () function in e.g. loop context:

char *file_names[] =
{
"(hd0,1)/file1", //file do not exist
"(hd0,1)/file2" //file exist
};
grub_file_t file;
int i;
for (i = 0; i < 2; i++)

{
     file  = grub_file_open (file_names[i]);
     if (file) {...}
}

There, we should get positive return in the second case (i == 1), but grub_file_open() returns 0.

Using gdb i've found that this problem connected with incorrect errno check in /kern/file.c

Let's look:

Quoted 10 lines

grub_file_t
grub_file_open (const char *name)
{
grub_device_t device;
grub_file_t file = 0;
char *device_name;
char *file_name;
device_name = grub_file_get_device_name (name);
if (grub_errno)
return 0;

Quoted 4 lines
Quoted 2 lines

But, we DO NOT set grub_errno to 0 at the begining of the function, thats why next loop round it always returns 0

Fixed, thank you.

Now you broke it ;)

Previous behavior was working correctly. You have to handle errorcodes at some point and that means when error is handled it is zeroed (or GRUB_ERR_NONE). So code is in callee where that loop was.

#3 lore raw
Quoted 4 lines · Vesa Jääskeläinen

Previous behavior was working correctly. You have to handle errorcodes
at some point and that means when error is handled it is zeroed (or GRUB_ERR_NONE). So code is in callee where that loop was.

I suggest that we never set grub_errno to 0 (except the initialization). That would match the standard errno behavior:

http://www.opengroup.org/onlinepubs/009695399/functions/errno.html

Signature · 2 lines
Regards,
Pavel Roskin
#4 lore raw
Quoted 10 lines · Pavel Roskin
Quoted 4 lines · Vesa Jääskeläinen

Previous behavior was working correctly. You have to handle errorcodes
at some point and that means when error is handled it is zeroed (or GRUB_ERR_NONE). So code is in callee where that loop was.

I suggest that we never set grub_errno to 0 (except the initialization). That would match the standard errno behavior:

http://www.opengroup.org/onlinepubs/009695399/functions/errno.html

Marco is right. As you pointed out, our error handling is different from errno on Unix, but this is intentional, because I stole the model from GRUB Legacy and Parted.

Okuji

#5 lore raw
Quoted 7 lines · Yoshinori K. Okuji
Quoted 6 lines · Pavel Roskin
Quoted 4 lines · Vesa Jääskeläinen

Previous behavior was working correctly. You have to handle errorcodes
at some point and that means when error is handled it is zeroed (or GRUB_ERR_NONE). So code is in callee where that loop was.

Quoted 8 lines
Quoted 4 lines

I suggest that we never set grub_errno to 0 (except the initialization). That would match the standard errno behavior:

http://www.opengroup.org/onlinepubs/009695399/functions/errno.html

Marco is right. As you pointed out, our error handling is different from errno on Unix, but this is intentional, because I stole the model from GRUB Legacy and Parted.

It's nice when people say I am right (it should happen more often). But I do not recall participating in this thread. Did I miss something, like I usually do? ;-)

--
Marco

#6 lore raw
Quoted 2 lines · Marco Gerards

It's nice when people say I am right (it should happen more often).

You're right.

Signature · 5 lines
Robert Millan

<GPLv2> I know my rights; I want my phone call!
<DRM> What use is a phone call… if you are unable to speak?
(as seen on /.)
#7 lore raw
In reply to Yoshinori K. Okuji
Quoted 7 lines · Yoshinori K. Okuji
Quoted 6 lines · Pavel Roskin
Quoted 4 lines · Vesa Jääskeläinen

Previous behavior was working correctly. You have to handle errorcodes
at some point and that means when error is handled it is zeroed (or GRUB_ERR_NONE). So code is in callee where that loop was.

Quoted 8 lines
Quoted 4 lines

I suggest that we never set grub_errno to 0 (except the initialization). That would match the standard errno behavior:

http://www.opengroup.org/onlinepubs/009695399/functions/errno.html

Marco is right. As you pointed out, our error handling is different from errno on Unix, but this is intentional, because I stole the model from GRUB Legacy and Parted.

Could you explain how is it supposed to work? There were clearly two bugs, which I "fixed" in grub_disk_open first, and in grub_file_open. The solution can be wrong, but the bugs still existed.

Signature · 5 lines
Robert Millan

<GPLv2> I know my rights; I want my phone call!
<DRM> What use is a phone call… if you are unable to speak?
(as seen on /.)
#8 lore raw
Quoted 13 lines · Robert Millan
Quoted 12 lines · Yoshinori K. Okuji
Quoted 10 lines · Pavel Roskin
Quoted 4 lines · Vesa Jääskeläinen

Previous behavior was working correctly. You have to handle errorcodes
at some point and that means when error is handled it is zeroed (or GRUB_ERR_NONE). So code is in callee where that loop was.

I suggest that we never set grub_errno to 0 (except the initialization). That would match the standard errno behavior:

http://www.opengroup.org/onlinepubs/009695399/functions/errno.html

Quoted 7 lines
Quoted 3 lines

Marco is right. As you pointed out, our error handling is different from errno on Unix, but this is intentional, because I stole the model from GRUB Legacy and Parted.

Could you explain how is it supposed to work? There were clearly two bugs, which I "fixed" in grub_disk_open first, and in grub_file_open. The solution can be wrong, but the bugs still existed.

Hopefully my previous mail makes that clear?

Were these previous problems committed?

What is the bug?

--
Marco

#9 lore raw
In reply to Pavel Roskin
Quoted 11 lines · Pavel Roskin
Quoted 4 lines · Vesa Jääskeläinen

Previous behavior was working correctly. You have to handle errorcodes
at some point and that means when error is handled it is zeroed (or GRUB_ERR_NONE). So code is in callee where that loop was.

I suggest that we never set grub_errno to 0 (except the initialization). That would match the standard errno behavior:

http://www.opengroup.org/onlinepubs/009695399/functions/errno.html

That just wouldn't work, it is not how the current code was designed.

For example when you open a file, first the filesystem type has to be detected. So first you try filesystem A, after that filesystem B, etc. A returns an error, B too, etc. Some of these errors (out of memory) should immediately be reported back to the caller. Others, like wrong filesystem type are normal. We just clear the error and continue.

It's like exception handling in C++. You can choose which errors you handle at a certain level.

--
Marco

#10 lore raw
In reply to Vesa Jääskeläinen

Vesa Jääskeläinen <[email protected]> writes:

(replying to this mail, I didn't receive the first mail over the list...)

Quoted 17 lines
Quoted 16 lines · Robert Millan
Quoted 15 lines · Oleg Strikov

Incorrect behavior of grub_file_open () function in e.g. loop context:

char *file_names[] =
{
"(hd0,1)/file1", //file do not exist
"(hd0,1)/file2" //file exist
};
grub_file_t file;
int i;
for (i = 0; i < 2; i++)

{
     file  = grub_file_open (file_names[i]);
     if (file) {...}
}
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

There, we should get positive return in the second case (i == 1), but grub_file_open() returns 0.

No, that is not right. This behavior is correct.

When you open the first file, you get an error which you ignore. If you think this error is harmless, clear it. So your code is wrong.

--
Marco

#11 lore raw
Quoted 21 lines · Marco Gerards

Vesa Jääskeläinen <[email protected]> writes:

(replying to this mail, I didn't receive the first mail over the list...)

Quoted 17 lines
Quoted 16 lines · Robert Millan
Quoted 15 lines · Oleg Strikov

Incorrect behavior of grub_file_open () function in e.g. loop context:

char *file_names[] =
{
"(hd0,1)/file1", //file do not exist
"(hd0,1)/file2" //file exist
};
grub_file_t file;
int i;
for (i = 0; i < 2; i++)

{
     file  = grub_file_open (file_names[i]);
     if (file) {...}
}
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

There, we should get positive return in the second case (i == 1), but grub_file_open() returns 0.

No, that is not right. This behavior is correct.

When you open the first file, you get an error which you ignore. If you think this error is harmless, clear it. So your code is wrong.

Ok, I reverted it. This will uncover another bug somewhere else. I can't remember what it was about, though.

Signature · 5 lines
Robert Millan

<GPLv2> I know my rights; I want my phone call!
<DRM> What use is a phone call… if you are unable to speak?
(as seen on /.)
#12 lore raw
Quoted 25 lines · Robert Millan
Quoted 24 lines · Marco Gerards

Vesa Jääskeläinen <[email protected]> writes:

(replying to this mail, I didn't receive the first mail over the list...)

Quoted 19 lines
Quoted 18 lines · Robert Millan
Quoted 17 lines · Oleg Strikov

Incorrect behavior of grub_file_open () function in e.g. loop context:

char *file_names[] =
{
"(hd0,1)/file1", //file do not exist
"(hd0,1)/file2" //file exist
};
grub_file_t file;
int i;
for (i = 0; i < 2; i++)

{
     file  = grub_file_open (file_names[i]);
     if (file) {...}
}

There, we should get positive return in the second case (i == 1), but grub_file_open() returns 0.

Quoted 7 lines
Quoted 4 lines

No, that is not right. This behavior is correct.

When you open the first file, you get an error which you ignore. If you think this error is harmless, clear it. So your code is wrong.

Ok, I reverted it. This will uncover another bug somewhere else. I can't remember what it was about, though.

Thanks. Can you look that up?

If you found it, I can help you fixing it, if you'd like?

--
Marco

#13 lore raw
Quoted 5 lines · Marco Gerards
Quoted 3 lines

Ok, I reverted it. This will uncover another bug somewhere else. I can't remember what it was about, though.

Thanks. Can you look that up?

Sorry, I can't. I don't remember what I found broken, but chances are somebody will run through it again.

Quoted 1 line

If you found it, I can help you fixing it, if you'd like?

It wasn't a difficult bug. The problem is just I don't know which bug is it!

Don't worry, we'll eventually run through it.

Signature · 5 lines
Robert Millan

<GPLv2> I know my rights; I want my phone call!
<DRM> What use is a phone call… if you are unable to speak?
(as seen on /.)