From 3437e478ba932edbab18a90638c20be1f0141156 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Mon, 26 Oct 2020 18:06:18 -0400 Subject: fix reintroduction of errno clobbering by atfork handlers commit bd153422f28634bb6e53f13f80beb8289d405267 reintroduced the bug fixed in c21051e90cd27a0b26be0ac66950b7396a156ba1 by refactoring the __syscall_ret into _Fork where it once again runs before the atfork handlers are called. since _Fork is a public interface that sets errno, this can't be fixed the way it was fixed last time without making new internal interfaces. instead, just save errno, and restore it only on error to ensure that a value of 0 is never restored. --- src/process/fork.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/process/fork.c b/src/process/fork.c index a12da01a..8d34a9c4 100644 --- a/src/process/fork.c +++ b/src/process/fork.c @@ -1,4 +1,5 @@ #include +#include #include "libc.h" static void dummy(int x) { } @@ -8,6 +9,8 @@ pid_t fork(void) { __fork_handler(-1); pid_t ret = _Fork(); + int errno_save = errno; __fork_handler(!ret); + if (ret<0) errno = errno_save; return ret; } -- cgit v1.2.1