Skip to content

Commit 53f2747

Browse files
committed
syscall, internal/syscall/windows: remove utf16PtrToString parameter
CL 208617 introduced syscall.utf16PtrToString and internal/syscall/windows.UTF16PtrToString functions. Original version of CL 208617 did not include syscall.utf16PtrToString and internal/syscall/windows.UTF16PtrToString max parameter. The parameter was added by Brad at the request of Ian. Ian said: "In some cases it seems at least possible that the null terminator is not present. I think it would be safer if we passed a maximum length here." The syscall.utf16PtrToString and internal/syscall/windows.UTF16PtrToString function are designed to work with only null terminated strings. So max parameter is superfluous. This change removes max parameter. Updates #34972 Change-Id: Ifea65dbd86bca8a08353579c6b9636c6f963d165 Reviewed-on: https://go-review.googlesource.com/c/go/+/228858 Run-TryBot: Alex Brainman <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent e90b0ce commit 53f2747

File tree

8 files changed

+30
-20
lines changed

8 files changed

+30
-20
lines changed

src/go/build/deps_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ var pkgDeps = map[string][]string{
152152
"syscall/js": {"L0"},
153153
"internal/oserror": {"L0"},
154154
"internal/syscall/unix": {"L0", "syscall"},
155-
"internal/syscall/windows": {"L0", "syscall", "internal/syscall/windows/sysdll", "unicode/utf16"},
155+
"internal/syscall/windows": {"L0", "syscall", "internal/syscall/windows/sysdll", "internal/unsafeheader", "unicode/utf16"},
156156
"internal/syscall/windows/registry": {"L0", "syscall", "internal/syscall/windows/sysdll", "unicode/utf16"},
157157
"internal/syscall/execenv": {"L0", "syscall", "internal/syscall/windows", "unicode/utf16"},
158158
"time": {

src/internal/syscall/windows/syscall_windows.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package windows
66

77
import (
8+
"internal/unsafeheader"
89
"sync"
910
"syscall"
1011
"unicode/utf16"
@@ -13,20 +14,24 @@ import (
1314

1415
// UTF16PtrToString is like UTF16ToString, but takes *uint16
1516
// as a parameter instead of []uint16.
16-
// max is how many times p can be advanced looking for the null terminator.
17-
// If max is hit, the string is truncated at that point.
18-
func UTF16PtrToString(p *uint16, max int) string {
17+
func UTF16PtrToString(p *uint16) string {
1918
if p == nil {
2019
return ""
2120
}
2221
// Find NUL terminator.
2322
end := unsafe.Pointer(p)
2423
n := 0
25-
for *(*uint16)(end) != 0 && n < max {
24+
for *(*uint16)(end) != 0 {
2625
end = unsafe.Pointer(uintptr(end) + unsafe.Sizeof(*p))
2726
n++
2827
}
29-
s := (*[(1 << 30) - 1]uint16)(unsafe.Pointer(p))[:n:n]
28+
// Turn *uint16 into []uint16.
29+
var s []uint16
30+
hdr := (*unsafeheader.Slice)(unsafe.Pointer(&s))
31+
hdr.Data = unsafe.Pointer(p)
32+
hdr.Cap = n
33+
hdr.Len = n
34+
// Decode []uint16 into string.
3035
return string(utf16.Decode(s))
3136
}
3237

src/net/interface_windows.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func interfaceTable(ifindex int) ([]Interface, error) {
5858
if ifindex == 0 || ifindex == int(index) {
5959
ifi := Interface{
6060
Index: int(index),
61-
Name: windows.UTF16PtrToString(aa.FriendlyName, 10000),
61+
Name: windows.UTF16PtrToString(aa.FriendlyName),
6262
}
6363
if aa.OperStatus == windows.IfOperStatusUp {
6464
ifi.Flags |= FlagUp

src/net/lookup_windows.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func (*Resolver) lookupCNAME(ctx context.Context, name string) (string, error) {
234234
defer syscall.DnsRecordListFree(r, 1)
235235

236236
resolved := resolveCNAME(syscall.StringToUTF16Ptr(name), r)
237-
cname := windows.UTF16PtrToString(resolved, 256)
237+
cname := windows.UTF16PtrToString(resolved)
238238
return absDomainName([]byte(cname)), nil
239239
}
240240

@@ -278,7 +278,7 @@ func (*Resolver) lookupMX(ctx context.Context, name string) ([]*MX, error) {
278278
mxs := make([]*MX, 0, 10)
279279
for _, p := range validRecs(r, syscall.DNS_TYPE_MX, name) {
280280
v := (*syscall.DNSMXData)(unsafe.Pointer(&p.Data[0]))
281-
mxs = append(mxs, &MX{absDomainName([]byte(windows.UTF16PtrToString(v.NameExchange, 256))), v.Preference})
281+
mxs = append(mxs, &MX{absDomainName([]byte(windows.UTF16PtrToString(v.NameExchange))), v.Preference})
282282
}
283283
byPref(mxs).sort()
284284
return mxs, nil
@@ -319,7 +319,7 @@ func (*Resolver) lookupTXT(ctx context.Context, name string) ([]string, error) {
319319
d := (*syscall.DNSTXTData)(unsafe.Pointer(&p.Data[0]))
320320
s := ""
321321
for _, v := range (*[1 << 10]*uint16)(unsafe.Pointer(&(d.StringArray[0])))[:d.StringCount:d.StringCount] {
322-
s += windows.UTF16PtrToString(v, 1<<20)
322+
s += windows.UTF16PtrToString(v)
323323
}
324324
txts = append(txts, s)
325325
}
@@ -344,7 +344,7 @@ func (*Resolver) lookupAddr(ctx context.Context, addr string) ([]string, error)
344344
ptrs := make([]string, 0, 10)
345345
for _, p := range validRecs(r, syscall.DNS_TYPE_PTR, arpa) {
346346
v := (*syscall.DNSPTRData)(unsafe.Pointer(&p.Data[0]))
347-
ptrs = append(ptrs, absDomainName([]byte(windows.UTF16PtrToString(v.Host, 256))))
347+
ptrs = append(ptrs, absDomainName([]byte(windows.UTF16PtrToString(v.Host))))
348348
}
349349
return ptrs, nil
350350
}

src/os/exec_windows.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func findProcess(pid int) (p *Process, err error) {
9898
}
9999

100100
func init() {
101-
cmd := windows.UTF16PtrToString(syscall.GetCommandLine(), 0xffff)
101+
cmd := windows.UTF16PtrToString(syscall.GetCommandLine())
102102
if len(cmd) == 0 {
103103
arg0, _ := Executable()
104104
Args = []string{arg0}

src/os/user/lookup_windows.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func lookupFullNameServer(servername, username string) (string, error) {
4444
}
4545
defer syscall.NetApiBufferFree(p)
4646
i := (*syscall.UserInfo10)(unsafe.Pointer(p))
47-
return windows.UTF16PtrToString(i.FullName, 1024), nil
47+
return windows.UTF16PtrToString(i.FullName), nil
4848
}
4949

5050
func lookupFullName(domain, username, domainAndUser string) (string, error) {
@@ -167,7 +167,7 @@ func listGroupsForUsernameAndDomain(username, domain string) ([]string, error) {
167167
if entry.Name == nil {
168168
continue
169169
}
170-
sid, err := lookupGroupName(windows.UTF16PtrToString(entry.Name, 1024))
170+
sid, err := lookupGroupName(windows.UTF16PtrToString(entry.Name))
171171
if err != nil {
172172
return nil, err
173173
}

src/syscall/security_windows.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func (sid *SID) String() (string, error) {
163163
return "", e
164164
}
165165
defer LocalFree((Handle)(unsafe.Pointer(s)))
166-
return utf16PtrToString(s, 256), nil
166+
return utf16PtrToString(s), nil
167167
}
168168

169169
// Len returns the length, in bytes, of a valid security identifier sid.

src/syscall/syscall_windows.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
errorspkg "errors"
1111
"internal/oserror"
1212
"internal/race"
13+
"internal/unsafeheader"
1314
"runtime"
1415
"sync"
1516
"unicode/utf16"
@@ -59,20 +60,24 @@ func UTF16ToString(s []uint16) string {
5960

6061
// utf16PtrToString is like UTF16ToString, but takes *uint16
6162
// as a parameter instead of []uint16.
62-
// max is how many times p can be advanced looking for the null terminator.
63-
// If max is hit, the string is truncated at that point.
64-
func utf16PtrToString(p *uint16, max int) string {
63+
func utf16PtrToString(p *uint16) string {
6564
if p == nil {
6665
return ""
6766
}
6867
// Find NUL terminator.
6968
end := unsafe.Pointer(p)
7069
n := 0
71-
for *(*uint16)(end) != 0 && n < max {
70+
for *(*uint16)(end) != 0 {
7271
end = unsafe.Pointer(uintptr(end) + unsafe.Sizeof(*p))
7372
n++
7473
}
75-
s := (*[(1 << 30) - 1]uint16)(unsafe.Pointer(p))[:n:n]
74+
// Turn *uint16 into []uint16.
75+
var s []uint16
76+
hdr := (*unsafeheader.Slice)(unsafe.Pointer(&s))
77+
hdr.Data = unsafe.Pointer(p)
78+
hdr.Cap = n
79+
hdr.Len = n
80+
// Decode []uint16 into string.
7681
return string(utf16.Decode(s))
7782
}
7883

0 commit comments

Comments
 (0)