Skip to content

Commit

Permalink
windows: use cgo.Handle for service object
Browse files Browse the repository at this point in the history
This fixes go vet complaints and checkptr crashes when the handler
receives an event, e.g. a session change notification.

Fixes golang/go#59687
  • Loading branch information
elmeyer committed Apr 28, 2023
1 parent 90abad3 commit fe43297
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions windows/svc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package svc

import (
"errors"
"runtime/cgo"
"sync"
"unsafe"

Expand Down Expand Up @@ -192,7 +193,7 @@ var (
)

func ctlHandler(ctl, evtype, evdata, context uintptr) uintptr {
s := (*service)(unsafe.Pointer(context))
s := cgo.Handle(context).Value().(service)
e := ctlEvent{cmd: Cmd(ctl), eventType: uint32(evtype), eventData: evdata, context: 123456} // Set context to 123456 to test issue #25660.
s.c <- e
return 0
Expand All @@ -203,7 +204,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, uintptr(cgo.NewHandle(theService)))
if sysErr, ok := err.(windows.Errno); ok {
return uintptr(sysErr)
} else if err != nil {
Expand Down

0 comments on commit fe43297

Please sign in to comment.