From e216ee7e7416df48dc9550a6f18552a4ada5d419 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Fri, 9 Dec 2022 12:30:24 -0500 Subject: syscall: clean up variable declarations in forkAndExecInChild The various forkAndExecInChild implementations have comments explaining that they pre-declare variables to force allocations to occur before forking, but then later use ":=" declarations for additional variables. To make it clearer that those ":=" declarations do not allocate, we move their declarations up to the predeclared blocks. For #57208. Change-Id: Ie8cb577fa7180b51b64d6dc398169053fdf8ea97 Reviewed-on: https://go-review.googlesource.com/c/go/+/456516 Auto-Submit: Bryan Mills Reviewed-by: Ian Lance Taylor Run-TryBot: Bryan Mills TryBot-Result: Gopher Robot --- src/syscall/exec_libc.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'src/syscall/exec_libc.go') diff --git a/src/syscall/exec_libc.go b/src/syscall/exec_libc.go index ef0c87e03c..f8769b9aba 100644 --- a/src/syscall/exec_libc.go +++ b/src/syscall/exec_libc.go @@ -81,10 +81,13 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr // Declare all variables at top in case any // declarations require heap allocation (e.g., err1). var ( - r1 uintptr - err1 Errno - nextfd int - i int + r1 uintptr + err1 Errno + nextfd int + i int + pgrp _Pid_t + cred *Credential + ngroups, groups uintptr ) // guard against side effects of shuffling fds below. @@ -135,7 +138,7 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr } if sys.Foreground { - pgrp := _Pid_t(sys.Pgid) + pgrp = _Pid_t(sys.Pgid) if pgrp == 0 { r1, err1 = getpid() if err1 != 0 { @@ -165,9 +168,9 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr } // User and groups - if cred := sys.Credential; cred != nil { - ngroups := uintptr(len(cred.Groups)) - groups := uintptr(0) + if cred = sys.Credential; cred != nil { + ngroups = uintptr(len(cred.Groups)) + groups = uintptr(0) if ngroups > 0 { groups = uintptr(unsafe.Pointer(&cred.Groups[0])) } -- cgit v1.3