Skip to content

Commit

Permalink
windows/svc: fix manager config toStringSlice pointer casting
Browse files Browse the repository at this point in the history
  • Loading branch information
rikysya committed Mar 28, 2020
1 parent f1d8baa commit 2ff63b3
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions windows/svc/mgr/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package mgr

import (
"syscall"
"unicode/utf16"
"unsafe"

"golang.org/x/sys/windows"
Expand Down Expand Up @@ -51,20 +50,23 @@ func toString(p *uint16) string {
}

func toStringSlice(ps *uint16) []string {
if ps == nil {
return nil
}
r := make([]string, 0)
for from, i, p := 0, 0, (*[1 << 24]uint16)(unsafe.Pointer(ps)); true; i++ {
if p[i] == 0 {
// empty string marks the end
if i <= from {
break
}
r = append(r, string(utf16.Decode(p[from:i])))
from = i + 1
p := unsafe.Pointer(ps)

offset := func(count int) uintptr {
return unsafe.Sizeof(uint16(0)) * (uintptr)(count)
}

for {
s := windows.UTF16PtrToString((*uint16)(p), 4096)
if s == "" {
break
}

r = append(r, s)
p = unsafe.Pointer(uintptr(p) + offset(len(s)+1))
}

return r
}

Expand Down

0 comments on commit 2ff63b3

Please sign in to comment.