Skip to content

Commit ecb9ce6

Browse files
alexbrainmanaclements
authored andcommitted
[release-branch.go1.5] net: fix off by one error while counting interfaces on windows
Fixes #12301 Change-Id: I8d01ec9551c6cff7e6129e06a7deb36a3be9de41 Reviewed-on: https://go-review.googlesource.com/16751 Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-on: https://go-review.googlesource.com/16984 Run-TryBot: Austin Clements <[email protected]> Reviewed-by: Alex Brainman <[email protected]> Reviewed-by: Russ Cox <[email protected]>
1 parent 5dda3bc commit ecb9ce6

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/net/interface_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func getInterfaceInfos() ([]syscall.InterfaceInfo, error) {
4848
return nil, os.NewSyscallError("wsaioctl", err)
4949
}
5050
iilen := ret / uint32(unsafe.Sizeof(iia[0]))
51-
return iia[:iilen-1], nil
51+
return iia[:iilen], nil
5252
}
5353

5454
func bytesEqualIP(a []byte, b []int8) bool {

src/net/net_windows_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ package net
66

77
import (
88
"bufio"
9+
"bytes"
910
"fmt"
1011
"io"
1112
"os"
1213
"os/exec"
14+
"sort"
15+
"strings"
1316
"syscall"
1417
"testing"
1518
"time"
@@ -163,3 +166,53 @@ func TestAcceptIgnoreSomeErrors(t *testing.T) {
163166
t.Fatalf(`"%s" received from recv, but "abc" expected`, s)
164167
}
165168
}
169+
170+
func isWindowsXP(t *testing.T) bool {
171+
v, err := syscall.GetVersion()
172+
if err != nil {
173+
t.Fatalf("GetVersion failed: %v", err)
174+
}
175+
major := byte(v)
176+
return major < 6
177+
}
178+
179+
func listInterfacesWithNetsh() ([]string, error) {
180+
out, err := exec.Command("netsh", "interface", "ip", "show", "config").CombinedOutput()
181+
if err != nil {
182+
return nil, fmt.Errorf("netsh failed: %v: %q", err, string(out))
183+
}
184+
lines := bytes.Split(out, []byte{'\r', '\n'})
185+
names := make([]string, 0)
186+
for _, line := range lines {
187+
f := bytes.Split(line, []byte{'"'})
188+
if len(f) == 3 {
189+
names = append(names, string(f[1]))
190+
}
191+
}
192+
return names, nil
193+
}
194+
195+
func TestInterfaceList(t *testing.T) {
196+
if isWindowsXP(t) {
197+
t.Skip("Windows XP netsh command does not provide required functionality")
198+
}
199+
ift, err := Interfaces()
200+
if err != nil {
201+
t.Fatal(err)
202+
}
203+
have := make([]string, 0)
204+
for _, ifi := range ift {
205+
have = append(have, ifi.Name)
206+
}
207+
sort.Strings(have)
208+
209+
want, err := listInterfacesWithNetsh()
210+
if err != nil {
211+
t.Fatal(err)
212+
}
213+
sort.Strings(want)
214+
215+
if strings.Join(want, "/") != strings.Join(have, "/") {
216+
t.Fatalf("unexpected interface list %q, want %q", have, want)
217+
}
218+
}

0 commit comments

Comments
 (0)