Skip to content

Commit

Permalink
use UTF16PtrToString function instead of slicing
Browse files Browse the repository at this point in the history
  • Loading branch information
rikysya committed Mar 25, 2020
1 parent a7604b9 commit 8f8c737
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion windows/security_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,7 @@ func (sd *SECURITY_DESCRIPTOR) String() string {
return ""
}
defer LocalFree(Handle(unsafe.Pointer(sddl)))
return UTF16ToString((*[(1 << 30) - 1]uint16)(unsafe.Pointer(sddl))[:strLen:strLen])
return UTF16PtrToString(sddl, int(strLen))
}

// ToAbsolute converts a self-relative security descriptor into an absolute one.
Expand Down
2 changes: 1 addition & 1 deletion windows/svc/mgr/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func toString(p *uint16) string {
if p == nil {
return ""
}
return syscall.UTF16ToString((*[4096]uint16)(unsafe.Pointer(p))[:])
return windows.UTF16PtrToString(p, 4096)
}

func toStringSlice(ps *uint16) []string {
Expand Down
4 changes: 2 additions & 2 deletions windows/svc/mgr/mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (m *Mgr) LockStatus() (*LockStatus, error) {
status := &LockStatus{
IsLocked: lockStatus.IsLocked != 0,
Age: time.Duration(lockStatus.LockDuration) * time.Second,
Owner: windows.UTF16ToString((*[(1 << 30) - 1]uint16)(unsafe.Pointer(lockStatus.LockOwner))[:]),
Owner: toString(lockStatus.LockOwner),
}
return status, nil
}
Expand Down Expand Up @@ -204,7 +204,7 @@ func (m *Mgr) ListServices() ([]string, error) {
services := (*[1 << 20]windows.ENUM_SERVICE_STATUS_PROCESS)(unsafe.Pointer(&buf[0]))[:servicesReturned:servicesReturned]
var names []string
for _, s := range services {
name := syscall.UTF16ToString((*[1 << 20]uint16)(unsafe.Pointer(s.ServiceName))[:])
name := toString(s.ServiceName)
names = append(names, name)
}
return names, nil
Expand Down
4 changes: 2 additions & 2 deletions windows/svc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,10 @@ const (
func (s *service) run() {
s.goWaits.Wait()
s.h = windows.Handle(ssHandle)
argv := (*[100]*int16)(unsafe.Pointer(sArgv))[:sArgc:sArgc]
argv := (*[100]*uint16)(unsafe.Pointer(sArgv))[:sArgc:sArgc]
args := make([]string, len(argv))
for i, a := range argv {
args[i] = syscall.UTF16ToString((*[1 << 20]uint16)(unsafe.Pointer(a))[:])
args[i] = windows.UTF16PtrToString(a, 1<<20)
}

cmdsToHandler := make(chan ChangeRequest)
Expand Down
19 changes: 19 additions & 0 deletions windows/syscall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,25 @@ func UTF16PtrFromString(s string) (*uint16, error) {
return &a[0], nil
}

// UTF16PtrToString is like UTF16ToString, but takes *uint16
// as a parameter instead of []uint16.
// max is how many times p can be advanced looking for the null terminator.
// If max is hit, the string is truncated at that point.
func UTF16PtrToString(p *uint16, max int) string {
if p == nil {
return ""
}
// Find NUL terminator.
end := unsafe.Pointer(p)
n := 0
for *(*uint16)(end) != 0 && n < max {
end = unsafe.Pointer(uintptr(end) + unsafe.Sizeof(*p))
n++
}
s := (*[(1 << 30) - 1]uint16)(unsafe.Pointer(p))[:n:n]
return string(utf16.Decode(s))
}

func Getpagesize() int { return 4096 }

// NewCallback converts a Go function to a function pointer conforming to the stdcall calling convention.
Expand Down

0 comments on commit 8f8c737

Please sign in to comment.