Skip to content

Commit dae8d8a

Browse files
committed
windows: UTF16PtrToString remove max parameter
1 parent 5e5d49e commit dae8d8a

File tree

7 files changed

+29
-37
lines changed

7 files changed

+29
-37
lines changed

windows/env_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (token Token) Environ(inheritExisting bool) (env []string, err error) {
3939
defer DestroyEnvironmentBlock(block)
4040
blockp := uintptr(unsafe.Pointer(block))
4141
for {
42-
entry := UTF16PtrToString((*uint16)(unsafe.Pointer(blockp)), (1<<30)-1)
42+
entry := UTF16PtrToString((*uint16)(unsafe.Pointer(blockp)))
4343
if len(entry) == 0 {
4444
break
4545
}

windows/security_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1230,7 +1230,7 @@ func (sd *SECURITY_DESCRIPTOR) String() string {
12301230
return ""
12311231
}
12321232
defer LocalFree(Handle(unsafe.Pointer(sddl)))
1233-
return UTF16PtrToString(sddl, int(strLen))
1233+
return UTF16PtrToString(sddl)
12341234
}
12351235

12361236
// ToAbsolute converts a self-relative security descriptor into an absolute one.

windows/svc/mgr/config.go

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,19 @@ type Config struct {
4545
DelayedAutoStart bool // the service is started after other auto-start services are started plus a short delay
4646
}
4747

48-
func toString(p *uint16) string {
49-
return windows.UTF16PtrToString(p, 4096)
50-
}
51-
5248
func toStringSlice(ps *uint16) []string {
5349
r := make([]string, 0)
5450
p := unsafe.Pointer(ps)
5551

56-
offset := func(count int) uintptr {
57-
return unsafe.Sizeof(uint16(0)) * (uintptr)(count)
58-
}
59-
6052
for {
61-
s := windows.UTF16PtrToString((*uint16)(p), 4096)
62-
if s == "" {
53+
s := windows.UTF16PtrToString((*uint16)(p))
54+
if len(s) == 0 {
6355
break
6456
}
6557

6658
r = append(r, s)
67-
p = unsafe.Pointer(uintptr(p) + offset(len(s)+1))
59+
offset := unsafe.Sizeof(uint16(0)) * (uintptr)(len(s)+1)
60+
p = unsafe.Pointer(uintptr(p) + offset)
6861
}
6962

7063
return r
@@ -109,13 +102,13 @@ func (s *Service) Config() (Config, error) {
109102
ServiceType: p.ServiceType,
110103
StartType: p.StartType,
111104
ErrorControl: p.ErrorControl,
112-
BinaryPathName: toString(p.BinaryPathName),
113-
LoadOrderGroup: toString(p.LoadOrderGroup),
105+
BinaryPathName: windows.UTF16PtrToString(p.BinaryPathName),
106+
LoadOrderGroup: windows.UTF16PtrToString(p.LoadOrderGroup),
114107
TagId: p.TagId,
115108
Dependencies: toStringSlice(p.Dependencies),
116-
ServiceStartName: toString(p.ServiceStartName),
117-
DisplayName: toString(p.DisplayName),
118-
Description: toString(p2.Description),
109+
ServiceStartName: windows.UTF16PtrToString(p.ServiceStartName),
110+
DisplayName: windows.UTF16PtrToString(p.DisplayName),
111+
Description: windows.UTF16PtrToString(p2.Description),
119112
DelayedAutoStart: delayedStart,
120113
}, nil
121114
}

windows/svc/mgr/mgr.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (m *Mgr) LockStatus() (*LockStatus, error) {
7373
status := &LockStatus{
7474
IsLocked: lockStatus.IsLocked != 0,
7575
Age: time.Duration(lockStatus.LockDuration) * time.Second,
76-
Owner: toString(lockStatus.LockOwner),
76+
Owner: windows.UTF16PtrToString(lockStatus.LockOwner),
7777
}
7878
return status, nil
7979
}
@@ -204,7 +204,7 @@ func (m *Mgr) ListServices() ([]string, error) {
204204
services := (*[1 << 20]windows.ENUM_SERVICE_STATUS_PROCESS)(unsafe.Pointer(&buf[0]))[:servicesReturned:servicesReturned]
205205
var names []string
206206
for _, s := range services {
207-
name := toString(s.ServiceName)
207+
name := windows.UTF16PtrToString(s.ServiceName)
208208
names = append(names, name)
209209
}
210210
return names, nil

windows/svc/mgr/recovery.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (s *Service) RebootMessage() (string, error) {
112112
return "", err
113113
}
114114
p := (*windows.SERVICE_FAILURE_ACTIONS)(unsafe.Pointer(&b[0]))
115-
return toString(p.RebootMsg), nil
115+
return windows.UTF16PtrToString(p.RebootMsg), nil
116116
}
117117

118118
// SetRecoveryCommand sets the command line of the process to execute in response to the RunCommand service controller action.
@@ -131,5 +131,5 @@ func (s *Service) RecoveryCommand() (string, error) {
131131
return "", err
132132
}
133133
p := (*windows.SERVICE_FAILURE_ACTIONS)(unsafe.Pointer(&b[0]))
134-
return toString(p.Command), nil
134+
return windows.UTF16PtrToString(p.Command), nil
135135
}

windows/svc/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func (s *service) run() {
227227
argv := (*[100]*uint16)(unsafe.Pointer(sArgv))[:sArgc:sArgc]
228228
args := make([]string, len(argv))
229229
for i, a := range argv {
230-
args[i] = windows.UTF16PtrToString(a, 1<<20)
230+
args[i] = windows.UTF16PtrToString(a)
231231
}
232232

233233
cmdsToHandler := make(chan ChangeRequest)

windows/syscall_windows.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -117,23 +117,22 @@ func UTF16PtrFromString(s string) (*uint16, error) {
117117
return &a[0], nil
118118
}
119119

120-
// UTF16PtrToString is like UTF16ToString, but takes *uint16
121-
// as a parameter instead of []uint16.
122-
// max is how many times p can be advanced looking for the null terminator.
123-
// If max is hit, the string is truncated at that point.
124-
func UTF16PtrToString(p *uint16, max int) string {
120+
// UTF16PtrToString takes a pointer to a UTF-16 sequence and returns the corresponding UTF-8 encoded string.
121+
// If the pointer is nil, this returns the empty string. This assumes that the UTF-16 sequence is terminated
122+
// at a zero word; if the zero word is not present, the program may crash.
123+
func UTF16PtrToString(p *uint16) string {
125124
if p == nil {
126125
return ""
127126
}
128-
// Find NUL terminator.
129-
end := unsafe.Pointer(p)
130-
n := 0
131-
for *(*uint16)(end) != 0 && n < max {
132-
end = unsafe.Pointer(uintptr(end) + unsafe.Sizeof(*p))
133-
n++
127+
128+
var encoded []uint16
129+
for *p != 0 {
130+
encoded = append(encoded, *p)
131+
132+
p = (*uint16)(unsafe.Pointer(uintptr(unsafe.Pointer(p)) + unsafe.Sizeof(*p)))
134133
}
135-
s := (*[(1 << 30) - 1]uint16)(unsafe.Pointer(p))[:n:n]
136-
return string(utf16.Decode(s))
134+
135+
return string(utf16.Decode(encoded))
137136
}
138137

139138
func Getpagesize() int { return 4096 }
@@ -1397,7 +1396,7 @@ func (t Token) KnownFolderPath(folderID *KNOWNFOLDERID, flags uint32) (string, e
13971396
return "", err
13981397
}
13991398
defer CoTaskMemFree(unsafe.Pointer(p))
1400-
return UTF16PtrToString(p, (1<<30)-1), nil
1399+
return UTF16PtrToString(p), nil
14011400
}
14021401

14031402
// RtlGetVersion returns the version of the underlying operating system, ignoring

0 commit comments

Comments
 (0)