develooper Front page | perl.perl5.porters | Postings from September 2011

Re: [perl #98934] set{u,g}id doesn't clear $! on success

Thread Previous | Thread Next
Chris Adams
September 14, 2011 02:11
Re: [perl #98934] set{u,g}id doesn't clear $! on success
Message ID:
Once upon a time, Paul Johnson <> said:
> On Tue, Sep 13, 2011 at 02:30:27PM +0200, Leon Timmermans wrote:
> > On Mon, Sep 12, 2011 at 9:50 PM, Chris Adams <> wrote:
> > 
> > > This is highly confusing, and there's really no reason to push this off
> > > on the people writing perl code.  At a minimum, perl internally could do
> > > something like:
> > >
> > >    if (setregid(gid,-1) == 0)
> > >        errno = 0;
> > >
> > > (and that goes for all the setuid/setgid family of functions).
> > 
> > That might piss off an equal number of users because $! gets
> > overwritten when they didn't expect it.
> The problem in perl is that $! is intimately bound to errno, and you have
> no business doing anything with errno except examining it immediately
> after a failed system call.  IIRC, on some systems you cannot assign to
> errno anyway since it can be implemented as a macro expanding to a
> function.

POSIX/SuS specifcally define errno as a symbol that "shall expand to a
modifiable lvalue of type int."  It can be a macro, but the macro has to
expand to something that can be modified (so not a function).

Of course, the standard also says that no standard function shall set
errno to zero, and that it should only be examined when an error is
indicated by a function's return value.  perl isn't bound by that of
course (since $! is not exactly errno; it is documented that perl
functions _can_ modify $! to return their own errors).

Basically, my above code bit is a work-around because perl calls the
set{u,g}id functions on a variable assignment instead of a function call
(that can die).  The perl docs say you should check $!, and any sane
code would (as they are security-sensitive assignments), and then the
only valid thing to do currently is to clear $! first.  IMHO if it is
required for proper functioning anyway, the language should take care of
it for you.

Chris Adams <>
Systems and Network Administrator - HiWAAY Internet Services
I don't speak for anybody but myself - that's enough trouble.

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About