Commit ffd7eba2 authored by Russ Cox's avatar Russ Cox

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 <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarAustin Clements <austin@google.com>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent ff70494b
...@@ -117,7 +117,7 @@ func iimport(pkg *types.Pkg, in *bio.Reader) { ...@@ -117,7 +117,7 @@ func iimport(pkg *types.Pkg, in *bio.Reader) {
stringData := data[:sLen] stringData := data[:sLen]
declData := data[sLen:] declData := data[sLen:]
in.Seek(int64(sLen+dLen), os.SEEK_CUR) in.MustSeek(int64(sLen+dLen), os.SEEK_CUR)
p := &iimporter{ p := &iimporter{
ipkg: pkg, ipkg: pkg,
......
...@@ -97,13 +97,13 @@ func finishArchiveEntry(bout *bio.Writer, start int64, name string) { ...@@ -97,13 +97,13 @@ func finishArchiveEntry(bout *bio.Writer, start int64, name string) {
if size&1 != 0 { if size&1 != 0 {
bout.WriteByte(0) bout.WriteByte(0)
} }
bout.Seek(start-ArhdrSize, 0) bout.MustSeek(start-ArhdrSize, 0)
var arhdr [ArhdrSize]byte var arhdr [ArhdrSize]byte
formathdr(arhdr[:], name, size) formathdr(arhdr[:], name, size)
bout.Write(arhdr[:]) bout.Write(arhdr[:])
bout.Flush() bout.Flush()
bout.Seek(start+size+(size&1), 0) bout.MustSeek(start+size+(size&1), 0)
} }
func dumpCompilerObj(bout *bio.Writer) { func dumpCompilerObj(bout *bio.Writer) {
......
...@@ -43,7 +43,7 @@ func Open(name string) (*Reader, error) { ...@@ -43,7 +43,7 @@ func Open(name string) (*Reader, error) {
return &Reader{f: f, Reader: bufio.NewReader(f)}, nil return &Reader{f: f, Reader: bufio.NewReader(f)}, nil
} }
func (r *Reader) Seek(offset int64, whence int) int64 { func (r *Reader) MustSeek(offset int64, whence int) int64 {
if whence == 1 { if whence == 1 {
offset -= int64(r.Buffered()) offset -= int64(r.Buffered())
} }
...@@ -55,7 +55,7 @@ func (r *Reader) Seek(offset int64, whence int) int64 { ...@@ -55,7 +55,7 @@ func (r *Reader) Seek(offset int64, whence int) int64 {
return off return off
} }
func (w *Writer) Seek(offset int64, whence int) int64 { func (w *Writer) MustSeek(offset int64, whence int) int64 {
if err := w.Flush(); err != nil { if err := w.Flush(); err != nil {
log.Fatalf("writing output: %v", err) log.Fatalf("writing output: %v", err)
} }
......
...@@ -57,6 +57,6 @@ func (r *Reader) sliceOS(length uint64) ([]byte, bool) { ...@@ -57,6 +57,6 @@ func (r *Reader) sliceOS(length uint64) ([]byte, bool) {
} }
data = data[off-aoff:] data = data[off-aoff:]
r.Seek(int64(length), 1) r.MustSeek(int64(length), 1)
return data, true return data, true
} }
...@@ -126,7 +126,7 @@ func hostArchive(ctxt *Link, name string) { ...@@ -126,7 +126,7 @@ func hostArchive(ctxt *Link, name string) {
libgcc := sym.Library{Pkg: "libgcc"} libgcc := sym.Library{Pkg: "libgcc"}
h := ldobj(ctxt, f, &libgcc, l, pname, name) h := ldobj(ctxt, f, &libgcc, l, pname, name)
f.Seek(h.off, 0) f.MustSeek(h.off, 0)
h.ld(ctxt, f, h.pkg, h.length, h.pn) h.ld(ctxt, f, h.pkg, h.length, h.pn)
} }
......
...@@ -741,7 +741,7 @@ func nextar(bp *bio.Reader, off int64, a *ArHdr) int64 { ...@@ -741,7 +741,7 @@ func nextar(bp *bio.Reader, off int64, a *ArHdr) int64 {
if off&1 != 0 { if off&1 != 0 {
off++ off++
} }
bp.Seek(off, 0) bp.MustSeek(off, 0)
var buf [SAR_HDR]byte var buf [SAR_HDR]byte
if n, err := io.ReadFull(bp, buf[:]); err != nil { if n, err := io.ReadFull(bp, buf[:]); err != nil {
if n == 0 && err != io.EOF { if n == 0 && err != io.EOF {
...@@ -864,8 +864,8 @@ func loadobjfile(ctxt *Link, lib *sym.Library) { ...@@ -864,8 +864,8 @@ func loadobjfile(ctxt *Link, lib *sym.Library) {
} }
/* load it as a regular file */ /* load it as a regular file */
l := f.Seek(0, 2) l := f.MustSeek(0, 2)
f.Seek(0, 0) f.MustSeek(0, 0)
ldobj(ctxt, f, lib, l, lib.File, lib.File) ldobj(ctxt, f, lib, l, lib.File, lib.File)
return return
} }
...@@ -985,7 +985,7 @@ func hostobjs(ctxt *Link) { ...@@ -985,7 +985,7 @@ func hostobjs(ctxt *Link) {
Exitf("cannot reopen %s: %v", h.pn, err) Exitf("cannot reopen %s: %v", h.pn, err)
} }
f.Seek(h.off, 0) f.MustSeek(h.off, 0)
h.ld(ctxt, f, h.pkg, h.length, h.pn) h.ld(ctxt, f, h.pkg, h.length, h.pn)
f.Close() f.Close()
} }
...@@ -1607,7 +1607,7 @@ func ldobj(ctxt *Link, f *bio.Reader, lib *sym.Library, length int64, pn string, ...@@ -1607,7 +1607,7 @@ func ldobj(ctxt *Link, f *bio.Reader, lib *sym.Library, length int64, pn string,
c2 := bgetc(f) c2 := bgetc(f)
c3 := bgetc(f) c3 := bgetc(f)
c4 := bgetc(f) c4 := bgetc(f)
f.Seek(start, 0) f.MustSeek(start, 0)
magic := uint32(c1)<<24 | uint32(c2)<<16 | uint32(c3)<<8 | uint32(c4) magic := uint32(c1)<<24 | uint32(c2)<<16 | uint32(c3)<<8 | uint32(c4)
if magic == 0x7f454c46 { // \x7F E L F 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, ...@@ -1740,9 +1740,9 @@ func ldobj(ctxt *Link, f *bio.Reader, lib *sym.Library, length int64, pn string,
import1 := f.Offset() import1 := f.Offset()
f.Seek(import0, 0) f.MustSeek(import0, 0)
ldpkg(ctxt, f, lib, import1-import0-2, pn) // -2 for !\n ldpkg(ctxt, f, lib, import1-import0-2, pn) // -2 for !\n
f.Seek(import1, 0) f.MustSeek(import1, 0)
flags := 0 flags := 0
switch *FlagStrictDups { switch *FlagStrictDups {
......
...@@ -590,9 +590,7 @@ func Load(arch *sys.Arch, syms *sym.Symbols, f *bio.Reader, pkg string, length i ...@@ -590,9 +590,7 @@ func Load(arch *sys.Arch, syms *sym.Symbols, f *bio.Reader, pkg string, length i
elfobj.nsect = uint(elfobj.shnum) elfobj.nsect = uint(elfobj.shnum)
for i := 0; uint(i) < elfobj.nsect; i++ { for i := 0; uint(i) < elfobj.nsect; i++ {
if f.Seek(int64(uint64(base)+elfobj.shoff+uint64(int64(i)*int64(elfobj.shentsize))), 0) < 0 { f.MustSeek(int64(uint64(base)+elfobj.shoff+uint64(int64(i)*int64(elfobj.shentsize))), 0)
return errorf("malformed elf file: negative seek")
}
sect := &elfobj.sect[i] sect := &elfobj.sect[i]
if is64 != 0 { if is64 != 0 {
var b ElfSectBytes64 var b ElfSectBytes64
...@@ -996,9 +994,7 @@ func elfmap(elfobj *ElfObj, sect *ElfSect) (err error) { ...@@ -996,9 +994,7 @@ func elfmap(elfobj *ElfObj, sect *ElfSect) (err error) {
} }
sect.base = make([]byte, sect.size) sect.base = make([]byte, sect.size)
if elfobj.f.Seek(int64(uint64(elfobj.base)+sect.off), 0) < 0 { elfobj.f.MustSeek(int64(uint64(elfobj.base)+sect.off), 0)
return fmt.Errorf("short read: seek not successful")
}
if _, err := io.ReadFull(elfobj.f, sect.base); err != nil { if _, err := io.ReadFull(elfobj.f, sect.base); err != nil {
return fmt.Errorf("short read: %v", err) return fmt.Errorf("short read: %v", err)
} }
......
...@@ -321,9 +321,7 @@ func macholoadrel(m *ldMachoObj, sect *ldMachoSect) int { ...@@ -321,9 +321,7 @@ func macholoadrel(m *ldMachoObj, sect *ldMachoSect) int {
rel := make([]ldMachoRel, sect.nreloc) rel := make([]ldMachoRel, sect.nreloc)
n := int(sect.nreloc * 8) n := int(sect.nreloc * 8)
buf := make([]byte, n) buf := make([]byte, n)
if m.f.Seek(m.base+int64(sect.reloff), 0) < 0 { m.f.MustSeek(m.base+int64(sect.reloff), 0)
return -1
}
if _, err := io.ReadFull(m.f, buf); err != nil { if _, err := io.ReadFull(m.f, buf); err != nil {
return -1 return -1
} }
...@@ -367,9 +365,7 @@ func macholoaddsym(m *ldMachoObj, d *ldMachoDysymtab) int { ...@@ -367,9 +365,7 @@ func macholoaddsym(m *ldMachoObj, d *ldMachoDysymtab) int {
n := int(d.nindirectsyms) n := int(d.nindirectsyms)
p := make([]byte, n*4) p := make([]byte, n*4)
if m.f.Seek(m.base+int64(d.indirectsymoff), 0) < 0 { m.f.MustSeek(m.base+int64(d.indirectsymoff), 0)
return -1
}
if _, err := io.ReadFull(m.f, p); err != nil { if _, err := io.ReadFull(m.f, p); err != nil {
return -1 return -1
} }
...@@ -387,9 +383,7 @@ func macholoadsym(m *ldMachoObj, symtab *ldMachoSymtab) int { ...@@ -387,9 +383,7 @@ func macholoadsym(m *ldMachoObj, symtab *ldMachoSymtab) int {
} }
strbuf := make([]byte, symtab.strsize) strbuf := make([]byte, symtab.strsize)
if m.f.Seek(m.base+int64(symtab.stroff), 0) < 0 { m.f.MustSeek(m.base+int64(symtab.stroff), 0)
return -1
}
if _, err := io.ReadFull(m.f, strbuf); err != nil { if _, err := io.ReadFull(m.f, strbuf); err != nil {
return -1 return -1
} }
...@@ -400,9 +394,7 @@ func macholoadsym(m *ldMachoObj, symtab *ldMachoSymtab) int { ...@@ -400,9 +394,7 @@ func macholoadsym(m *ldMachoObj, symtab *ldMachoSymtab) int {
} }
n := int(symtab.nsym * uint32(symsize)) n := int(symtab.nsym * uint32(symsize))
symbuf := make([]byte, n) symbuf := make([]byte, n)
if m.f.Seek(m.base+int64(symtab.symoff), 0) < 0 { m.f.MustSeek(m.base+int64(symtab.symoff), 0)
return -1
}
if _, err := io.ReadFull(m.f, symbuf); err != nil { if _, err := io.ReadFull(m.f, symbuf); err != nil {
return -1 return -1
} }
...@@ -463,7 +455,7 @@ func Load(arch *sys.Arch, syms *sym.Symbols, f *bio.Reader, pkg string, length i ...@@ -463,7 +455,7 @@ func Load(arch *sys.Arch, syms *sym.Symbols, f *bio.Reader, pkg string, length i
} }
if is64 { if is64 {
f.Seek(4, 1) // skip reserved word in header f.MustSeek(4, 1) // skip reserved word in header
} }
m := &ldMachoObj{ m := &ldMachoObj{
...@@ -555,9 +547,7 @@ func Load(arch *sys.Arch, syms *sym.Symbols, f *bio.Reader, pkg string, length i ...@@ -555,9 +547,7 @@ func Load(arch *sys.Arch, syms *sym.Symbols, f *bio.Reader, pkg string, length i
return errorf("load segment out of range") return errorf("load segment out of range")
} }
if f.Seek(m.base+int64(c.seg.fileoff), 0) < 0 { f.MustSeek(m.base+int64(c.seg.fileoff), 0)
return errorf("cannot load object data: seek failed")
}
dat := make([]byte, c.seg.filesz) dat := make([]byte, c.seg.filesz)
if _, err := io.ReadFull(f, dat); err != nil { if _, err := io.ReadFull(f, dat); err != nil {
return errorf("cannot load object data: %v", err) return errorf("cannot load object data: %v", err)
......
...@@ -133,7 +133,7 @@ const ( ...@@ -133,7 +133,7 @@ const (
type peBiobuf bio.Reader type peBiobuf bio.Reader
func (f *peBiobuf) ReadAt(p []byte, off int64) (int, error) { func (f *peBiobuf) ReadAt(p []byte, off int64) (int, error) {
ret := ((*bio.Reader)(f)).Seek(off, 0) ret := ((*bio.Reader)(f)).MustSeek(off, 0)
if ret < 0 { if ret < 0 {
return 0, errors.New("fail to seek") return 0, errors.New("fail to seek")
} }
......
...@@ -27,7 +27,7 @@ type ldSection struct { ...@@ -27,7 +27,7 @@ type ldSection struct {
type xcoffBiobuf bio.Reader type xcoffBiobuf bio.Reader
func (f *xcoffBiobuf) ReadAt(p []byte, off int64) (int, error) { func (f *xcoffBiobuf) ReadAt(p []byte, off int64) (int, error) {
ret := ((*bio.Reader)(f)).Seek(off, 0) ret := ((*bio.Reader)(f)).MustSeek(off, 0)
if ret < 0 { if ret < 0 {
return 0, errors.New("fail to seek") return 0, errors.New("fail to seek")
} }
......
...@@ -85,7 +85,7 @@ func Load(arch *sys.Arch, syms *sym.Symbols, f *bio.Reader, lib *sym.Library, le ...@@ -85,7 +85,7 @@ func Load(arch *sys.Arch, syms *sym.Symbols, f *bio.Reader, lib *sym.Library, le
start := f.Offset() start := f.Offset()
roObject := f.SliceRO(uint64(length)) roObject := f.SliceRO(uint64(length))
if roObject != nil { if roObject != nil {
f.Seek(int64(-length), os.SEEK_CUR) f.MustSeek(int64(-length), os.SEEK_CUR)
} }
r := &objReader{ r := &objReader{
rd: f, rd: f,
...@@ -104,7 +104,7 @@ func Load(arch *sys.Arch, syms *sym.Symbols, f *bio.Reader, lib *sym.Library, le ...@@ -104,7 +104,7 @@ func Load(arch *sys.Arch, syms *sym.Symbols, f *bio.Reader, lib *sym.Library, le
if r.roOffset != length { if r.roOffset != length {
log.Fatalf("%s: unexpected end at %d, want %d", pn, r.roOffset, start+length) log.Fatalf("%s: unexpected end at %d, want %d", pn, r.roOffset, start+length)
} }
r.rd.Seek(int64(length), os.SEEK_CUR) r.rd.MustSeek(int64(length), os.SEEK_CUR)
} else if f.Offset() != start+length { } else if f.Offset() != start+length {
log.Fatalf("%s: unexpected end at %d, want %d", pn, f.Offset(), start+length) log.Fatalf("%s: unexpected end at %d, want %d", pn, f.Offset(), start+length)
} }
......
...@@ -23,8 +23,6 @@ cmd/compile/internal/gc/testdata/short_test.go: unreachable code ...@@ -23,8 +23,6 @@ cmd/compile/internal/gc/testdata/short_test.go: unreachable code
// These cases are basically ok. // These cases are basically ok.
// Errors are handled reasonably and there's no clear need for interface satisfaction. // Errors are handled reasonably and there's no clear need for interface satisfaction.
// Except for the runtime/pprof case, the API is not exported. // Except for the runtime/pprof case, the API is not exported.
cmd/internal/bio/buf.go: method Seek(offset int64, whence int) int64 should have signature Seek(int64, int) (int64, error)
cmd/internal/bio/buf.go: method Seek(offset int64, whence int) int64 should have signature Seek(int64, int) (int64, error)
fmt/print.go: method WriteByte(c byte) should have signature WriteByte(byte) error fmt/print.go: method WriteByte(c byte) should have signature WriteByte(byte) error
// Also non-standard, but this method is on an unexported type, so it's // Also non-standard, but this method is on an unexported type, so it's
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment