errno handling
Hello,
Last night I spent some time with a confussion that maybe it can be avoided (or maybe not).
Let's say that a module calls grub_file_open and it fails (file doesn't exist or whatever). grub_errno is setted up.
This module is not resetting grub_errno and makes another call to grub_file_open with a valid file. But grub_file_open does:
------
device_name = grub_file_get_device_name (name);
if (grub_errno)
return 0;
------
(without resetting grub_errno before or in grub_file_get_device_name)
So caling grub_file_open with an invalid file name and then with valid one returns error two times.
I was expecting the library functions to setup errno if there is some error. But not that the functions behaves different in function of the previous errno. Mainly because this can cause some strange effects changing the code (adding some call before that changes errno and the user is not resetting)
I would reset errno at beginning of grub_file_open, and other places.
I've search some documentation about the errno standard without succeeding, I'm sure that here there is people who is more familiar with it.
Signature · 2 lines
Carles Pina i Estany http://pinux.info
Quoted 11 lines · Carles Pina i Estany
Hello,
Last night I spent some time with a confussion that maybe it can be avoided (or maybe not).
Let's say that a module calls
grub_file_openand it fails (file doesn't exist or whatever).grub_errnois setted up.This module is not resetting
grub_errnoand makes another call togrub_file_openwith a valid file. Butgrub_file_opendoes:
In theory, each layer should check errno after a call that may be setting it. It's up to the layer whether errno has to be ignored (and reset), handled or passed to the upper layer.
If it's completely unchecked (i.e. not even reset), I think this is a bug. As you observed, it's a bit nasty; it can cause a routine to fail with an error that's completely unrelated to what the routine itself does.
Help is much welcome on cleaning this kind of problems up. But it needs to be done carefully; it's very easy to unadvertedly work around the problem instead of fixing it (this happened sometimes in the past when sorting out errno handling problems).
Signature · 5 lines
Robert Millan The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and how) you may access your data; but nobody's threatening your freedom: we still allow you to remove your data and not access it at all."
Hello,
Quoted 10 lines · Robert Millan
Quoted 9 lines · Carles Pina i Estany
Hello,
Last night I spent some time with a confussion that maybe it can be avoided (or maybe not).
Let's say that a module calls
grub_file_openand it fails (file doesn't exist or whatever).grub_errnois setted up.
Quoted 6 lines
Quoted 2 lines
This module is not resetting
grub_errnoand makes another call togrub_file_openwith a valid file. Butgrub_file_opendoes:In theory, each layer should check errno after a call that may be setting it. It's up to the layer whether errno has to be ignored (and reset), handled or passed to the upper layer.
In my opinion grub_file_open should reset grub_errno because grub_file_open behaviour is different up to the grub_errno when it enters in the function (if grub_errno != GRUB_ERRNO then grub_file_open will not open any file). And this for more functions.
Not sure if some parts of Grub are relaying on the behaviour that I would remove :-) or what happens in the standard libraries related to it.
Signature · 2 lines
Carles Pina i Estany http://pinux.info
Hi,
Quoted 17 lines · Carles Pina i Estany
Hello,
Quoted 13 lines · Robert Millan
Quoted 11 lines · Carles Pina i Estany
Hello,
Last night I spent some time with a confussion that maybe it can be avoided (or maybe not).
Let's say that a module calls
grub_file_openand it fails (file doesn't exist or whatever).grub_errnois setted up.This module is not resetting
grub_errnoand makes another call togrub_file_openwith a valid file. Butgrub_file_opendoes:
Quoted 8 lines
Quoted 3 lines
In theory, each layer should check errno after a call that may be setting it. It's up to the layer whether errno has to be ignored (and reset), handled or passed to the upper layer.
In my opinion
grub_file_openshould resetgrub_errnobecausegrub_file_openbehaviour is different up to thegrub_errnowhen it enters in the function (ifgrub_errno!=GRUB_ERRNOthengrub_file_openwill not open any file). And this for more functions.
discussed in irc with phcoder: no changes here because would be a caller bug. Some day I will take a look on a possible callers that are not resetting grub_errno after functions that are setting up (so can lead to problems later)
The discussion:
<phcoder> cpina: there is nothing wrong with red herring on bugs for the sake of kernel size
<cpina> phcoder: so you would not touch grub_file_open to keep the size as small as possible in the kernel and move this code to the users of grub_file_open?
<cpina> to keep the kernel size as you can understand :-)
<phcoder> cpina: it's not what I said. Caller has to handle error return. If it doesn't it's a bug. If caller bugs core is allowed to do anything it wants
<cpina> phcoder: i understand, but when i design api's i try to make it hard for the users to miss-use the api. And in this case it's easy to miss-use the api.
<phcoder> cpina: with grub we have other requirements. If it really bothers you so much you can add assertions which would be enabled with ./configure --debug-mode
...
Cheers,
Signature · 2 lines
Carles Pina i Estany http://pinux.info
Quoted 5 lines · Carles Pina i Estany
discussed in irc with phcoder: no changes here because would be a caller bug. Some day I will take a look on a possible callers that are not resetting
grub_errnoafter functions that are setting up (so can lead to problems later)
Yes. We really need more quality checks, but not at the expense of size as Vladimir points out.
Another possibility would be something automated like the "fortity source" feature of glibc + gcc, as recently enabled in some OSes.
Unit tests are also helpful (there was a patch sent recently, which implements a "make check" command, but there were some issues and still got no response).
Signature · 5 lines
Robert Millan The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and how) you may access your data; but nobody's threatening your freedom: we still allow you to remove your data and not access it at all."