The errno variable can get trashed

rml rml at hpfcls.UUCP
Wed Aug 15 08:50:00 AEST 1984


> The solution would seem to be to have the C signal handling mechanism
> save errno before calling the signal routine and restore it after
> the routine has returned.

This is impossible with the standard implementation of the system call
interface and errno.  Errno is is an arbitrary location in the user's
address space which is unknown to the kernel.  That's why the kernel passes
the errno value back in a register and an assembly language library routine
copies the value to the global variable.

If this problem is a concern for you, you can simulate your suggested
solution in your own signal handler:

void i()
{ int save_errno;
  extern int errno;

  save_errno = errno;
  signal(SIGALRM,i);
  alarm(1);
  open(".",1); /* Fails, since is a directory */
  errno = save_errno;
}

> Is the fact that this bug has been present for upwards of ten years
> in all versions of Unix, apparently without anyone noticing, an
> indication of how often Unix programmers check for errors?

The fact that you can cause this behavior about once a minute with a
contrived example does not indicate that this is a serious problem.
Admittedly, many programs running on UNIX* systems (including a number
of commands shipped with UNIX systems) are lax about checking for errors
in system calls or about reporting the reason for the error when they do
check.  However, the fact that a simple workaround exists for those
programmers who are concerned is a point in favor of UNIX systems.

				Bob Lenk
				{hplabs, ihnp4}!hpfcla!rml

*UNIX is a trademark of AT&T Bell Laboratories



More information about the Comp.unix.wizards mailing list