Skip to content

Commit 2dccfee

Browse files
committed
windows: do not return invalid error for psuedo-handle functions
GetCurrentProcess and GetCurrentThread return -1 and -2 respectively. We could arguably hard code those values, but MSDN cautions not to; I'm sure this advice is old now, given that the psuedo handles for tokens (not processes/threads) are now implemented with inline functions in the headers for Windows 8, but anyway, we'll follow Microsoft's advice. However, regardless of that, these functions never ever return an error. MSDN doesn't indicate that they do, reverse engineering the functions doesn't indicate that they do, and checking against 0 is just plain wrong, considering 0!=INVALID_HANDLE_VALUE; however INVALID_HANDLE_VALUE==-1, so that's not correct either. In fact, checking any value and returning any error does not make sense. Incidently having to check code for the pseudo handle is more verbose too. In order to make this function do the correct thing and meet the spec, remove the error value from the return. Change-Id: If03c9dab001be3bf5a04999aef20dbfcf8a4f405 Reviewed-on: https://go-review.googlesource.com/c/sys/+/196798 Run-TryBot: Jason A. Donenfeld <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Alex Brainman <[email protected]>
1 parent 5c00192 commit 2dccfee

File tree

4 files changed

+10
-33
lines changed

4 files changed

+10
-33
lines changed

windows/security_windows.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -652,17 +652,9 @@ type Token Handle
652652
// associated with current process. It is a real
653653
// token that needs to be closed, unlike
654654
// GetCurrentProcessToken.
655-
func OpenCurrentProcessToken() (Token, error) {
656-
p, e := GetCurrentProcess()
657-
if e != nil {
658-
return 0, e
659-
}
660-
var t Token
661-
e = OpenProcessToken(p, TOKEN_QUERY|TOKEN_DUPLICATE, &t)
662-
if e != nil {
663-
return 0, e
664-
}
665-
return t, nil
655+
func OpenCurrentProcessToken() (token Token, err error) {
656+
err = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_DUPLICATE, &token)
657+
return
666658
}
667659

668660
// GetCurrentProcessToken returns the access token associated with

windows/syscall_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ func NewCallbackCDecl(fn interface{}) uintptr {
178178
//sys TerminateProcess(handle Handle, exitcode uint32) (err error)
179179
//sys GetExitCodeProcess(handle Handle, exitcode *uint32) (err error)
180180
//sys GetStartupInfo(startupInfo *StartupInfo) (err error) = GetStartupInfoW
181-
//sys GetCurrentProcess() (pseudoHandle Handle, err error)
182-
//sys GetCurrentThread() (pseudoHandle Handle, err error)
181+
//sys GetCurrentProcess() (pseudoHandle Handle)
182+
//sys GetCurrentThread() (pseudoHandle Handle)
183183
//sys GetProcessTimes(handle Handle, creationTime *Filetime, exitTime *Filetime, kernelTime *Filetime, userTime *Filetime) (err error)
184184
//sys DuplicateHandle(hSourceProcessHandle Handle, hSourceHandle Handle, hTargetProcessHandle Handle, lpTargetHandle *Handle, dwDesiredAccess uint32, bInheritHandle bool, dwOptions uint32) (err error)
185185
//sys WaitForSingleObject(handle Handle, waitMilliseconds uint32) (event uint32, err error) [failretval==0xffffffff]

windows/syscall_windows_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,7 @@ func TestGetNamedSecurityInfo(t *testing.T) {
250250
}
251251

252252
func TestGetSecurityInfo(t *testing.T) {
253-
process, _ := windows.GetCurrentProcess()
254-
sd, err := windows.GetSecurityInfo(process, windows.SE_KERNEL_OBJECT, windows.DACL_SECURITY_INFORMATION)
253+
sd, err := windows.GetSecurityInfo(windows.GetCurrentProcess(), windows.SE_KERNEL_OBJECT, windows.DACL_SECURITY_INFORMATION)
255254
if err != nil {
256255
t.Fatal(err)
257256
}

windows/zsyscall_windows.go

Lines changed: 4 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)