lib/tfork: Don't overwrite 'ret' in cleanup phase
authorJoseph Sutton <josephsutton@catalyst.net.nz>
Tue, 10 Jan 2023 00:06:25 +0000 (13:06 +1300)
committerJeremy Allison <jra@samba.org>
Tue, 10 Jan 2023 20:22:32 +0000 (20:22 +0000)
The cleanup phase of tfork_create() saves errno prior to calling
functions that might modify it, with the intention of restoring it
afterwards. However, the value of 'ret' is accidentally overwritten. It
will always be equal to 0, and hence errno will not be restored.

Fix this by introducing a new variable, ret2, for calling functions in
the cleanup phase.

Signed-off-by: Joseph Sutton <josephsutton@catalyst.net.nz>
Reviewed-by: Jeremy Allison <jra@samba.org>
lib/util/tfork.c

index 9867543702e817df29c00a8bc6592572c22a02d9..57a4e18638bc919962d4537ab876cd09969d4d01 100644 (file)
@@ -739,8 +739,9 @@ struct tfork *tfork_create(void)
        struct tfork_state *state = NULL;
        struct tfork *t = NULL;
        pid_t pid;
-       int saved_errno;
+       int saved_errno = 0;
        int ret = 0;
+       int ret2;
 
 #ifdef HAVE_PTHREAD
        ret = pthread_once(&tfork_global_is_initialized,
@@ -816,16 +817,16 @@ cleanup:
                                close(t->event_fd);
                        }
 
-                       ret = tfork_create_reap_waiter(state->waiter_pid);
-                       assert(ret == 0);
+                       ret2 = tfork_create_reap_waiter(state->waiter_pid);
+                       assert(ret2 == 0);
 
                        free(t);
                        t = NULL;
                }
        }
 
-       ret = tfork_uninstall_sigchld_handler();
-       assert(ret == 0);
+       ret2 = tfork_uninstall_sigchld_handler();
+       assert(ret2 == 0);
 
        tfork_global_free();