[PATCH v2 5/8] posix: Do not clobber errno by atfork handlers

Andreas Schwab schwab@linux-m68k.org
Thu Mar 11 22:21:22 GMT 2021


On Mär 11 2021, Florian Weimer via Libc-alpha wrote:

> * Adhemerval Zanella via Libc-alpha:
>
>> Changes from v1:
>> * Move the errno set/restore to parent branch.
>> ---
>> Checked on x86_64-linux-gnu.
>> ---
>>  posix/fork.c | 9 ++++++++-
>>  1 file changed, 8 insertions(+), 1 deletion(-)
>>
>> diff --git a/posix/fork.c b/posix/fork.c
>> index cc1bdc1232..a4fdd44e1e 100644
>> --- a/posix/fork.c
>> +++ b/posix/fork.c
>> @@ -68,7 +68,6 @@ __libc_fork (void)
>>      }
>>  
>>    pid_t pid = _Fork ();
>> -
>>    if (pid == 0)
>>      {
>>        fork_system_setup ();
>> @@ -99,6 +98,11 @@ __libc_fork (void)
>>      }
>>    else
>>      {
>> +      /* If _Fork failed, preserve its errno value.  */
>> +      int save_errno;
>> +      if (pid < 0)
>> +	save_errno = errno;
>> +
>>        /* Release acquired locks in the multi-threaded case.  */
>>        if (multiple_threads)
>>  	{
>> @@ -111,6 +115,9 @@ __libc_fork (void)
>>  
>>        /* Run the handlers registered for the parent.  */
>>        __run_fork_handlers (atfork_run_parent, multiple_threads);
>> +
>> +      if (pid < 0)
>> +       __set_errno (save_errno);
>>      }
>>  
>>    return pid;
>
> This is okay.  GCC does not warn about the uninitialized save_errno
> here; I checked with build-many-glibcs.py.  Apparently the middle-end is
> smart enough to detect that the conditions match.

But I think it wouldn't be bad to make that unconditional.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."


More information about the Libc-alpha mailing list