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
+16 -4
View File
@@ -29,11 +29,19 @@ func OpenRemote(host, source string) (*Log, error) {
if source == "" {
return nil, errors.New("Specify event log source")
}
var s *uint16
var hostPointer *uint16
if host != "" {
s = syscall.StringToUTF16Ptr(host)
var err error
hostPointer, err = syscall.UTF16PtrFromString(host)
if err != nil {
return nil, err
}
}
h, err := windows.RegisterEventSource(s, syscall.StringToUTF16Ptr(source))
sourcePointer, err := syscall.UTF16PtrFromString(source)
if err != nil {
return nil, err
}
h, err := windows.RegisterEventSource(hostPointer, sourcePointer)
if err != nil {
return nil, err
}
@@ -46,7 +54,11 @@ func (l *Log) Close() error {
}
func (l *Log) report(etype uint16, eid uint32, msg string) error {
ss := []*uint16{syscall.StringToUTF16Ptr(msg)}
msgPointer, err := syscall.UTF16PtrFromString(msg)
if err != nil {
return err
}
ss := []*uint16{msgPointer}
return windows.ReportEvent(l.Handle, etype, 0, eid, 0, 1, 0, &ss[0], nil)
}