Skip to content

Commit fe43297

Browse files
committed
windows: use cgo.Handle for service object
This fixes go vet complaints and checkptr crashes when the handler receives an event, e.g. a session change notification. Fixes golang/go#59687
1 parent 90abad3 commit fe43297

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

windows/svc/service.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package svc
1010

1111
import (
1212
"errors"
13+
"runtime/cgo"
1314
"sync"
1415
"unsafe"
1516

@@ -192,7 +193,7 @@ var (
192193
)
193194

194195
func ctlHandler(ctl, evtype, evdata, context uintptr) uintptr {
195-
s := (*service)(unsafe.Pointer(context))
196+
s := cgo.Handle(context).Value().(service)
196197
e := ctlEvent{cmd: Cmd(ctl), eventType: uint32(evtype), eventData: evdata, context: 123456} // Set context to 123456 to test issue #25660.
197198
s.c <- e
198199
return 0
@@ -203,7 +204,7 @@ var theService service // This is, unfortunately, a global, which means only one
203204
// serviceMain is the entry point called by the service manager, registered earlier by
204205
// the call to StartServiceCtrlDispatcher.
205206
func serviceMain(argc uint32, argv **uint16) uintptr {
206-
handle, err := windows.RegisterServiceCtrlHandlerEx(windows.StringToUTF16Ptr(theService.name), ctlHandlerCallback, uintptr(unsafe.Pointer(&theService)))
207+
handle, err := windows.RegisterServiceCtrlHandlerEx(windows.StringToUTF16Ptr(theService.name), ctlHandlerCallback, uintptr(cgo.NewHandle(theService)))
207208
if sysErr, ok := err.(windows.Errno); ok {
208209
return uintptr(sysErr)
209210
} else if err != nil {

0 commit comments

Comments
 (0)