|
Home > Archive > Unix Programming > June 2005 > Testing system call return values
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Testing system call return values
|
|
| tom dailey 2005-06-25, 8:47 pm |
| Much code that I've seen tests the return value from a system call as
follows:
rv = some_system_call...
if ( rv < 0 ) {
...handle error condition
}
However, almost every man page for a system call explicitly says that
-1 (and only -1) is the return code value indicating an error has
occurred (I'm not counting pthread return values here). So, I always
test for an error as follows:
rv = some_system_call...
if ( rv == -1 ) {
...handle error condition
}
because this explicitly follows the documentation. My question is, why
is the first above test used in so much UNIX code? Am I missing something?
Tom Dailey
| |
| Paul Pluzhnikov 2005-06-25, 8:47 pm |
| tom dailey <tom.dailey@verizon.net> writes:
> My question is, why is the first above test used in so much
> UNIX code?
Because people got away with it for a long time?
Consider lseek(2). Before 2GB-capable filesystems,
if (lseek(fd, 0, SEEK_END) < 0) { /* error */ }
was "as good as"
if (-1 == lseek(fd, 0, SEEK_END)) { /* error */ }
But if a file can be bigger then 2GB, then the "old" code will
mistakenly believe that lseek() have failed when it didn't.
Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
| |
| Ari Lukumies 2005-06-25, 8:47 pm |
| tom dailey wrote:
> Much code that I've seen tests the return value from a system call as
> follows:
>
> rv = some_system_call...
> if ( rv < 0 ) {
> ...handle error condition
> }
>
> My question is, why
> is the first above test used in so much UNIX code? Am I missing something?
Because -1 is less than zero, and the test against 0 (or less) taken
apart, takes less clock cycles than comparison with -1.
-atl-
--
A multiverse is figments of its own creations
| |
| Loic Domaigne 2005-06-27, 2:48 am |
| Salut Tom,
> Much code that I've seen tests the return value from a system call as
> follows:
>
> rv = some_system_call...
> if ( rv < 0 ) {
> ...handle error condition
> }
>
> However, almost every man page for a system call explicitly says that
> -1 (and only -1) is the return code value indicating an error has
> occurred (I'm not counting pthread return values here). So, I always
> test for an error as follows:
>
> rv = some_system_call...
> if ( rv == -1 ) {
> ...handle error condition
> }
>
> because this explicitly follows the documentation. My question is, why
> is the first above test used in so much UNIX code? Am I missing something?
A code is considered conform from a POSIX point of view if it tested
explicitly for the error condition (rv==-1).
The reason why (rv<0) is not portable has been nicely illustrated by
Paul: the APIs might change over the time, and returns negative value as
correct value.
In our day, the clock cycles saved by replacing (rv==-1) with (rv<0)
shouldn't be relevant.
Cheers,
Loic.
|
|
|
|
|