Skip to content

Commit ffd7eba

Browse files
committed
cmd/internal/bio: rename Reader.Seek to MustSeek
Renaming the method makes clear, both to readers and to vet, that this method is not the implementation of io.Seeker: it cannot fail. Working toward making the tree vet-safe instead of having so many exceptions in cmd/vet/all/whitelist. For #31916. Change-Id: I3e6ad7264cb0121b4b76935450cccb71d533e96b Reviewed-on: https://go-review.googlesource.com/c/go/+/176108 Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Austin Clements <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent ff70494 commit ffd7eba

File tree

12 files changed

+26
-42
lines changed

12 files changed

+26
-42
lines changed

src/cmd/compile/internal/gc/iimport.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func iimport(pkg *types.Pkg, in *bio.Reader) {
117117
stringData := data[:sLen]
118118
declData := data[sLen:]
119119

120-
in.Seek(int64(sLen+dLen), os.SEEK_CUR)
120+
in.MustSeek(int64(sLen+dLen), os.SEEK_CUR)
121121

122122
p := &iimporter{
123123
ipkg: pkg,

src/cmd/compile/internal/gc/obj.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ func finishArchiveEntry(bout *bio.Writer, start int64, name string) {
9797
if size&1 != 0 {
9898
bout.WriteByte(0)
9999
}
100-
bout.Seek(start-ArhdrSize, 0)
100+
bout.MustSeek(start-ArhdrSize, 0)
101101

102102
var arhdr [ArhdrSize]byte
103103
formathdr(arhdr[:], name, size)
104104
bout.Write(arhdr[:])
105105
bout.Flush()
106-
bout.Seek(start+size+(size&1), 0)
106+
bout.MustSeek(start+size+(size&1), 0)
107107
}
108108

109109
func dumpCompilerObj(bout *bio.Writer) {

src/cmd/internal/bio/buf.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func Open(name string) (*Reader, error) {
4343
return &Reader{f: f, Reader: bufio.NewReader(f)}, nil
4444
}
4545

46-
func (r *Reader) Seek(offset int64, whence int) int64 {
46+
func (r *Reader) MustSeek(offset int64, whence int) int64 {
4747
if whence == 1 {
4848
offset -= int64(r.Buffered())
4949
}
@@ -55,7 +55,7 @@ func (r *Reader) Seek(offset int64, whence int) int64 {
5555
return off
5656
}
5757

58-
func (w *Writer) Seek(offset int64, whence int) int64 {
58+
func (w *Writer) MustSeek(offset int64, whence int) int64 {
5959
if err := w.Flush(); err != nil {
6060
log.Fatalf("writing output: %v", err)
6161
}

src/cmd/internal/bio/buf_mmap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ func (r *Reader) sliceOS(length uint64) ([]byte, bool) {
5757
}
5858

5959
data = data[off-aoff:]
60-
r.Seek(int64(length), 1)
60+
r.MustSeek(int64(length), 1)
6161
return data, true
6262
}

src/cmd/link/internal/ld/ar.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func hostArchive(ctxt *Link, name string) {
126126

127127
libgcc := sym.Library{Pkg: "libgcc"}
128128
h := ldobj(ctxt, f, &libgcc, l, pname, name)
129-
f.Seek(h.off, 0)
129+
f.MustSeek(h.off, 0)
130130
h.ld(ctxt, f, h.pkg, h.length, h.pn)
131131
}
132132

src/cmd/link/internal/ld/lib.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ func nextar(bp *bio.Reader, off int64, a *ArHdr) int64 {
741741
if off&1 != 0 {
742742
off++
743743
}
744-
bp.Seek(off, 0)
744+
bp.MustSeek(off, 0)
745745
var buf [SAR_HDR]byte
746746
if n, err := io.ReadFull(bp, buf[:]); err != nil {
747747
if n == 0 && err != io.EOF {
@@ -864,8 +864,8 @@ func loadobjfile(ctxt *Link, lib *sym.Library) {
864864
}
865865

866866
/* load it as a regular file */
867-
l := f.Seek(0, 2)
868-
f.Seek(0, 0)
867+
l := f.MustSeek(0, 2)
868+
f.MustSeek(0, 0)
869869
ldobj(ctxt, f, lib, l, lib.File, lib.File)
870870
return
871871
}
@@ -985,7 +985,7 @@ func hostobjs(ctxt *Link) {
985985
Exitf("cannot reopen %s: %v", h.pn, err)
986986
}
987987

988-
f.Seek(h.off, 0)
988+
f.MustSeek(h.off, 0)
989989
h.ld(ctxt, f, h.pkg, h.length, h.pn)
990990
f.Close()
991991
}
@@ -1607,7 +1607,7 @@ func ldobj(ctxt *Link, f *bio.Reader, lib *sym.Library, length int64, pn string,
16071607
c2 := bgetc(f)
16081608
c3 := bgetc(f)
16091609
c4 := bgetc(f)
1610-
f.Seek(start, 0)
1610+
f.MustSeek(start, 0)
16111611

16121612
magic := uint32(c1)<<24 | uint32(c2)<<16 | uint32(c3)<<8 | uint32(c4)
16131613
if magic == 0x7f454c46 { // \x7F E L F
@@ -1740,9 +1740,9 @@ func ldobj(ctxt *Link, f *bio.Reader, lib *sym.Library, length int64, pn string,
17401740

17411741
import1 := f.Offset()
17421742

1743-
f.Seek(import0, 0)
1743+
f.MustSeek(import0, 0)
17441744
ldpkg(ctxt, f, lib, import1-import0-2, pn) // -2 for !\n
1745-
f.Seek(import1, 0)
1745+
f.MustSeek(import1, 0)
17461746

17471747
flags := 0
17481748
switch *FlagStrictDups {

src/cmd/link/internal/loadelf/ldelf.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -590,9 +590,7 @@ func Load(arch *sys.Arch, syms *sym.Symbols, f *bio.Reader, pkg string, length i
590590

591591
elfobj.nsect = uint(elfobj.shnum)
592592
for i := 0; uint(i) < elfobj.nsect; i++ {
593-
if f.Seek(int64(uint64(base)+elfobj.shoff+uint64(int64(i)*int64(elfobj.shentsize))), 0) < 0 {
594-
return errorf("malformed elf file: negative seek")
595-
}
593+
f.MustSeek(int64(uint64(base)+elfobj.shoff+uint64(int64(i)*int64(elfobj.shentsize))), 0)
596594
sect := &elfobj.sect[i]
597595
if is64 != 0 {
598596
var b ElfSectBytes64
@@ -996,9 +994,7 @@ func elfmap(elfobj *ElfObj, sect *ElfSect) (err error) {
996994
}
997995

998996
sect.base = make([]byte, sect.size)
999-
if elfobj.f.Seek(int64(uint64(elfobj.base)+sect.off), 0) < 0 {
1000-
return fmt.Errorf("short read: seek not successful")
1001-
}
997+
elfobj.f.MustSeek(int64(uint64(elfobj.base)+sect.off), 0)
1002998
if _, err := io.ReadFull(elfobj.f, sect.base); err != nil {
1003999
return fmt.Errorf("short read: %v", err)
10041000
}

src/cmd/link/internal/loadmacho/ldmacho.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,7 @@ func macholoadrel(m *ldMachoObj, sect *ldMachoSect) int {
321321
rel := make([]ldMachoRel, sect.nreloc)
322322
n := int(sect.nreloc * 8)
323323
buf := make([]byte, n)
324-
if m.f.Seek(m.base+int64(sect.reloff), 0) < 0 {
325-
return -1
326-
}
324+
m.f.MustSeek(m.base+int64(sect.reloff), 0)
327325
if _, err := io.ReadFull(m.f, buf); err != nil {
328326
return -1
329327
}
@@ -367,9 +365,7 @@ func macholoaddsym(m *ldMachoObj, d *ldMachoDysymtab) int {
367365
n := int(d.nindirectsyms)
368366

369367
p := make([]byte, n*4)
370-
if m.f.Seek(m.base+int64(d.indirectsymoff), 0) < 0 {
371-
return -1
372-
}
368+
m.f.MustSeek(m.base+int64(d.indirectsymoff), 0)
373369
if _, err := io.ReadFull(m.f, p); err != nil {
374370
return -1
375371
}
@@ -387,9 +383,7 @@ func macholoadsym(m *ldMachoObj, symtab *ldMachoSymtab) int {
387383
}
388384

389385
strbuf := make([]byte, symtab.strsize)
390-
if m.f.Seek(m.base+int64(symtab.stroff), 0) < 0 {
391-
return -1
392-
}
386+
m.f.MustSeek(m.base+int64(symtab.stroff), 0)
393387
if _, err := io.ReadFull(m.f, strbuf); err != nil {
394388
return -1
395389
}
@@ -400,9 +394,7 @@ func macholoadsym(m *ldMachoObj, symtab *ldMachoSymtab) int {
400394
}
401395
n := int(symtab.nsym * uint32(symsize))
402396
symbuf := make([]byte, n)
403-
if m.f.Seek(m.base+int64(symtab.symoff), 0) < 0 {
404-
return -1
405-
}
397+
m.f.MustSeek(m.base+int64(symtab.symoff), 0)
406398
if _, err := io.ReadFull(m.f, symbuf); err != nil {
407399
return -1
408400
}
@@ -463,7 +455,7 @@ func Load(arch *sys.Arch, syms *sym.Symbols, f *bio.Reader, pkg string, length i
463455
}
464456

465457
if is64 {
466-
f.Seek(4, 1) // skip reserved word in header
458+
f.MustSeek(4, 1) // skip reserved word in header
467459
}
468460

469461
m := &ldMachoObj{
@@ -555,9 +547,7 @@ func Load(arch *sys.Arch, syms *sym.Symbols, f *bio.Reader, pkg string, length i
555547
return errorf("load segment out of range")
556548
}
557549

558-
if f.Seek(m.base+int64(c.seg.fileoff), 0) < 0 {
559-
return errorf("cannot load object data: seek failed")
560-
}
550+
f.MustSeek(m.base+int64(c.seg.fileoff), 0)
561551
dat := make([]byte, c.seg.filesz)
562552
if _, err := io.ReadFull(f, dat); err != nil {
563553
return errorf("cannot load object data: %v", err)

src/cmd/link/internal/loadpe/ldpe.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ const (
133133
type peBiobuf bio.Reader
134134

135135
func (f *peBiobuf) ReadAt(p []byte, off int64) (int, error) {
136-
ret := ((*bio.Reader)(f)).Seek(off, 0)
136+
ret := ((*bio.Reader)(f)).MustSeek(off, 0)
137137
if ret < 0 {
138138
return 0, errors.New("fail to seek")
139139
}

src/cmd/link/internal/loadxcoff/ldxcoff.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type ldSection struct {
2727
type xcoffBiobuf bio.Reader
2828

2929
func (f *xcoffBiobuf) ReadAt(p []byte, off int64) (int, error) {
30-
ret := ((*bio.Reader)(f)).Seek(off, 0)
30+
ret := ((*bio.Reader)(f)).MustSeek(off, 0)
3131
if ret < 0 {
3232
return 0, errors.New("fail to seek")
3333
}

src/cmd/link/internal/objfile/objfile.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func Load(arch *sys.Arch, syms *sym.Symbols, f *bio.Reader, lib *sym.Library, le
8585
start := f.Offset()
8686
roObject := f.SliceRO(uint64(length))
8787
if roObject != nil {
88-
f.Seek(int64(-length), os.SEEK_CUR)
88+
f.MustSeek(int64(-length), os.SEEK_CUR)
8989
}
9090
r := &objReader{
9191
rd: f,
@@ -104,7 +104,7 @@ func Load(arch *sys.Arch, syms *sym.Symbols, f *bio.Reader, lib *sym.Library, le
104104
if r.roOffset != length {
105105
log.Fatalf("%s: unexpected end at %d, want %d", pn, r.roOffset, start+length)
106106
}
107-
r.rd.Seek(int64(length), os.SEEK_CUR)
107+
r.rd.MustSeek(int64(length), os.SEEK_CUR)
108108
} else if f.Offset() != start+length {
109109
log.Fatalf("%s: unexpected end at %d, want %d", pn, f.Offset(), start+length)
110110
}

src/cmd/vet/all/whitelist/all.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ cmd/compile/internal/gc/testdata/short_test.go: unreachable code
2323
// These cases are basically ok.
2424
// Errors are handled reasonably and there's no clear need for interface satisfaction.
2525
// Except for the runtime/pprof case, the API is not exported.
26-
cmd/internal/bio/buf.go: method Seek(offset int64, whence int) int64 should have signature Seek(int64, int) (int64, error)
27-
cmd/internal/bio/buf.go: method Seek(offset int64, whence int) int64 should have signature Seek(int64, int) (int64, error)
2826
fmt/print.go: method WriteByte(c byte) should have signature WriteByte(byte) error
2927

3028
// Also non-standard, but this method is on an unexported type, so it's

0 commit comments

Comments
 (0)