Skip to content

Commit dcacdad

Browse files
kolyshkingopherbot
authored andcommitted
unix: use ByteSliceFromString in (*Ifreq).Name
CL 340370 introduced (*Ifreq).Name method which scans the Ifrn field for \0 twice -- first explicitly, when by calling BytePtrToString. It seems more straightforward to use ByteSliceToString instead. The differences are: - simpler code; - no double scanning for \0; - in case there is no \0 in the Ifrn (unlikely), the full string (rather than an empty string) is now returned. With the last item in mind, the test case with no \0 as input that expects empty output fails, so drop it. Alternatively, we could test that it returns the full string, but it will be essentially testing ByteSliceToString, which is not the point. Cc: Matt Layher <[email protected]> Change-Id: I31d4f6dbe98aae5120f9b2246c93ceaa6a165395 Reviewed-on: https://go-review.googlesource.com/c/sys/+/407194 Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Matt Layher <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Matt Layher <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]>
1 parent 5e4e11f commit dcacdad

File tree

2 files changed

+1
-19
lines changed

2 files changed

+1
-19
lines changed

unix/ifreq_linux.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
package unix
99

1010
import (
11-
"bytes"
1211
"unsafe"
1312
)
1413

@@ -45,13 +44,7 @@ func NewIfreq(name string) (*Ifreq, error) {
4544

4645
// Name returns the interface name associated with the Ifreq.
4746
func (ifr *Ifreq) Name() string {
48-
// BytePtrToString requires a NULL terminator or the program may crash. If
49-
// one is not present, just return the empty string.
50-
if !bytes.Contains(ifr.raw.Ifrn[:], []byte{0x00}) {
51-
return ""
52-
}
53-
54-
return BytePtrToString(&ifr.raw.Ifrn[0])
47+
return ByteSliceToString(ifr.raw.Ifrn[:])
5548
}
5649

5750
// According to netdevice(7), only AF_INET addresses are returned for numerous

unix/ifreq_linux_test.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,6 @@ func TestIfreqSize(t *testing.T) {
3939
}
4040

4141
func TestIfreqName(t *testing.T) {
42-
// Invalid ifreq (no NULL terminator), so expect empty string.
43-
var name [IFNAMSIZ]byte
44-
for i := range name {
45-
name[i] = 0xff
46-
}
47-
48-
bad := &Ifreq{raw: ifreq{Ifrn: name}}
49-
if got := bad.Name(); got != "" {
50-
t.Fatalf("expected empty ifreq name, but got: %q", got)
51-
}
52-
5342
// Valid ifreq, expect the hard-coded testIfreq name.
5443
ifr := testIfreq(t)
5544
if want, got := ifreqName, ifr.Name(); want != got {

0 commit comments

Comments
 (0)