windows: replace all StringToUTF16 calls with UTF16FromString

`StringToUTF16` is deprecated and will panic if given an "invalid"
string (in particular, one that has a null byte in it).  The replacement
function is `UTF16FromString`, and it returns an error if there was
a problem.

This change replaces all uses of `StringToUTF16` with `UTF16FromString`.
The `service` struct now no longer stores a `string` name but rather
a `*uint16` pointer to the name.

It should not be possible to panic due to UTF16 string conversion
at this point.

Fixes golang/go#73006

Change-Id: Idce9cdbb4651fef8481f0cad19b5df0314fd4277
Reviewed-on: https://go-review.googlesource.com/c/sys/+/659936
Reviewed-by: Carlos Amedee <carlos@golang.org>
Auto-Submit: Carlos Amedee <carlos@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
This commit is contained in:
Douglas Danger Manley
2025-04-02 11:32:38 -07:00
committed by Gopher Robot
parent 1c3b72f1c1
commit 1b2bd6bb49
8 changed files with 132 additions and 30 deletions
+29 -5
View File
@@ -121,7 +121,11 @@ func (s *Service) Config() (Config, error) {
}
func updateDescription(handle windows.Handle, desc string) error {
d := windows.SERVICE_DESCRIPTION{Description: toPtr(desc)}
descPointer, err := toPtr(desc)
if err != nil {
return err
}
d := windows.SERVICE_DESCRIPTION{Description: descPointer}
return windows.ChangeServiceConfig2(handle,
windows.SERVICE_CONFIG_DESCRIPTION, (*byte)(unsafe.Pointer(&d)))
}
@@ -141,10 +145,30 @@ func updateStartUp(handle windows.Handle, isDelayed bool) error {
// UpdateConfig updates service s configuration parameters.
func (s *Service) UpdateConfig(c Config) error {
err := windows.ChangeServiceConfig(s.Handle, c.ServiceType, c.StartType,
c.ErrorControl, toPtr(c.BinaryPathName), toPtr(c.LoadOrderGroup),
nil, toStringBlock(c.Dependencies), toPtr(c.ServiceStartName),
toPtr(c.Password), toPtr(c.DisplayName))
binaryPathNamePointer, err := toPtr(c.BinaryPathName)
if err != nil {
return err
}
loadOrderGroupPointer, err := toPtr(c.LoadOrderGroup)
if err != nil {
return err
}
serviceStartNamePointer, err := toPtr(c.ServiceStartName)
if err != nil {
return err
}
passwordPointer, err := toPtr(c.Password)
if err != nil {
return err
}
displayNamePointer, err := toPtr(c.DisplayName)
if err != nil {
return err
}
err = windows.ChangeServiceConfig(s.Handle, c.ServiceType, c.StartType,
c.ErrorControl, binaryPathNamePointer, loadOrderGroupPointer,
nil, toStringBlock(c.Dependencies), serviceStartNamePointer,
passwordPointer, displayNamePointer)
if err != nil {
return err
}