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_libc2.go | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'src/syscall/exec_libc2.go') diff --git a/src/syscall/exec_libc2.go b/src/syscall/exec_libc2.go index 41bc79a721..9b04f96c81 100644 --- a/src/syscall/exec_libc2.go +++ b/src/syscall/exec_libc2.go @@ -52,14 +52,17 @@ func runtime_AfterForkInChild() // functions that do not grow the stack. // //go:norace -func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr *ProcAttr, sys *SysProcAttr, pipe int) (pid int, err Errno) { +func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr *ProcAttr, sys *SysProcAttr, pipe int) (pid int, err1 Errno) { // 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 + nextfd int + i int + err error + pgrp _C_int + cred *Credential + ngroups, groups uintptr ) // guard against side effects of shuffling fds below. @@ -94,7 +97,7 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr // Enable tracing if requested. if sys.Ptrace { - if err := ptrace(PTRACE_TRACEME, 0, 0, 0); err != nil { + if err = ptrace(PTRACE_TRACEME, 0, 0, 0); err != nil { err1 = err.(Errno) goto childerror } @@ -120,7 +123,7 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr if sys.Foreground { // This should really be pid_t, however _C_int (aka int32) is // generally equivalent. - pgrp := _C_int(sys.Pgid) + pgrp = _C_int(sys.Pgid) if pgrp == 0 { r1, _, err1 = rawSyscall(abi.FuncPCABI0(libc_getpid_trampoline), 0, 0, 0) if err1 != 0 { @@ -149,9 +152,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