Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 25 Jun 2000 15:52:03 -0400
From:      Garance A Drosihn <drosih@rpi.edu>
To:        Chuck Robey <chuckr@picnic.mat.net>, <Peter.Bohne@hboc.com>
Cc:        <fengyue@bluerose.windmoon.nu>, <andrew@ugh.net.au>, hackers@FreeBSD.ORG
Subject:   RE: libc_r/_read(), should the errno be reset to 0?
Message-ID:  <v0421010db57c10345a22@[128.113.24.47]>
In-Reply-To: <Pine.BSF.4.21.0006250025020.318-100000@picnic.mat.net>
References:  <Pine.BSF.4.21.0006250025020.318-100000@picnic.mat.net>

next in thread | previous in thread | raw e-mail | index | archive | help
At 12:28 AM -0400 6/25/00, Chuck Robey wrote:
>On Fri, 23 Jun 2000, Bohne, Peter wrote:
>
> > True enough.  That's the best advice.  However, your original
> > post indicated an expectation on your part that errno would
> > be somehow automatically reset to 0 before a system call, which
> > is definitely *not* the case.  You have to clear it - the kernel
> > does not do it for you.
>
>Why would you bother to clear it?  You don't check errno to determine
>fail/success, you check the function return.  If it returns fail, then
>errno is going to be correct whether or not you bothered to reset it or
>not.  Setting it to zero might give you a warm fuzzy feeling, but if
>you're correctly detected errors, it's not going to change anything.

There are times when clearing errno is significant.  What you said is
correct for the routine which actually calls the system routine (is it
some *_read() routine here?), but some library routines will call
several system routines, and only return an error to caller (of that
library routine) in some cases.

For instance, it might stat() some file, which sets errno if that
file does not exist.  To the library routine, it isn't really an
error if that file does not exist, it just creates the file or
perhaps skips over some optional processing.  Later on, it may
return an error due to some OTHER condition, a condition which
did not set errno.  Someone checking errno will think that "file
does not exist" is the problem, when the problem is elsewhere.

As usual with this topic, I can never remember a good example of
this, except that I know I've run into it on some occasions...
For those occasions, it IS important to zero out errno "at the
right place" (wherever that might be...).  One of these days I'll
have the presence of mind to save a good example of this...

Please note that I'm not saying the system routine should necessarily
set errno to zero, I'm just saying that there ARE occasions where it
IS important for the programmer to reset it to zero before calling
some other routines.  In those situations, it does much more than
just giving someone a "warm and fuzzy" feeling.


---
Garance Alistair Drosehn           =   gad@eclipse.acs.rpi.edu
Senior Systems Programmer          or  drosih@rpi.edu
Rensselaer Polytechnic Institute


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?v0421010db57c10345a22>