Skip to content

Commit

Permalink
windows/svc: do not pass theService to windows.RegisterServiceCtrlHan…
Browse files Browse the repository at this point in the history
…dlerEx

windows.RegisterServiceCtrlHandlerEx context parameter is uintptr, so it
is unsafe to pass pointers in that parameter. Use theService global
variable in ctlHandler function instead.

Fixes golang/go#67437

Change-Id: I84c910eaa354603c2a5114421cd61788cb40b86f
Reviewed-on: https://go-review.googlesource.com/c/sys/+/591475
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Michael Pratt <[email protected]>
Reviewed-by: Quim Muntal <[email protected]>
Reviewed-by: David Chase <[email protected]>
  • Loading branch information
alexbrainman committed Jun 13, 2024
1 parent 673e0f9 commit 348425a
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions windows/svc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,8 @@ var (
)

func ctlHandler(ctl, evtype, evdata, context uintptr) uintptr {
s := (*service)(unsafe.Pointer(context))
e := ctlEvent{cmd: Cmd(ctl), eventType: uint32(evtype), eventData: evdata, context: 123456} // Set context to 123456 to test issue #25660.
s.c <- e
theService.c <- e
return 0
}

Expand All @@ -210,7 +209,7 @@ var theService service // This is, unfortunately, a global, which means only one
// serviceMain is the entry point called by the service manager, registered earlier by
// the call to StartServiceCtrlDispatcher.
func serviceMain(argc uint32, argv **uint16) uintptr {
handle, err := windows.RegisterServiceCtrlHandlerEx(windows.StringToUTF16Ptr(theService.name), ctlHandlerCallback, uintptr(unsafe.Pointer(&theService)))
handle, err := windows.RegisterServiceCtrlHandlerEx(windows.StringToUTF16Ptr(theService.name), ctlHandlerCallback, 0)
if sysErr, ok := err.(windows.Errno); ok {
return uintptr(sysErr)
} else if err != nil {
Expand Down

0 comments on commit 348425a

Please sign in to comment.