Commit de7ee57c authored by Marvin Stenger's avatar Marvin Stenger Committed by Dave Cheney

cmd: remove bio.Bread

Replace calls to bio.Bread with calls to io.ReadFull.

Change-Id: I2ee8739d01e04a4da9c20b6ce7d1d5b89914b8ad
Reviewed-on: https://go-review.googlesource.com/21750Reviewed-by: 's avatarDave Cheney <dave@cheney.net>
parent e4f1d9cf
...@@ -7,7 +7,6 @@ package bio ...@@ -7,7 +7,6 @@ package bio
import ( import (
"bufio" "bufio"
"io"
"log" "log"
"os" "os"
) )
...@@ -86,16 +85,6 @@ func (w *Writer) Offset() int64 { ...@@ -86,16 +85,6 @@ func (w *Writer) Offset() int64 {
return off return off
} }
func Bread(r *Reader, p []byte) int {
n, err := io.ReadFull(r, p)
if n == 0 {
if err != nil && err != io.EOF {
n = -1
}
}
return n
}
func (r *Reader) Close() error { func (r *Reader) Close() error {
return r.f.Close() return r.f.Close()
} }
......
...@@ -35,6 +35,7 @@ import ( ...@@ -35,6 +35,7 @@ import (
"cmd/internal/obj" "cmd/internal/obj"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"io"
"os" "os"
) )
...@@ -76,8 +77,8 @@ func hostArchive(name string) { ...@@ -76,8 +77,8 @@ func hostArchive(name string) {
} }
defer f.Close() defer f.Close()
magbuf := make([]byte, len(ARMAG)) var magbuf [len(ARMAG)]byte
if bio.Bread(f, magbuf) != len(magbuf) { if _, err := io.ReadFull(f, magbuf[:]); err != nil {
Exitf("file %s too short", name) Exitf("file %s too short", name)
} }
...@@ -138,9 +139,8 @@ func readArmap(filename string, f *bio.Reader, arhdr ArHdr) archiveMap { ...@@ -138,9 +139,8 @@ func readArmap(filename string, f *bio.Reader, arhdr ArHdr) archiveMap {
wordSize = 8 wordSize = 8
} }
l := atolwhex(arhdr.size) contents := make([]byte, atolwhex(arhdr.size))
contents := make([]byte, l) if _, err := io.ReadFull(f, contents); err != nil {
if bio.Bread(f, contents) != int(l) {
Exitf("short read from %s", filename) Exitf("short read from %s", filename)
} }
......
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
"cmd/internal/bio" "cmd/internal/bio"
"cmd/internal/obj" "cmd/internal/obj"
"fmt" "fmt"
"io"
"os" "os"
"strings" "strings"
) )
...@@ -49,7 +50,7 @@ func ldpkg(f *bio.Reader, pkg string, length int64, filename string, whence int) ...@@ -49,7 +50,7 @@ func ldpkg(f *bio.Reader, pkg string, length int64, filename string, whence int)
} }
bdata := make([]byte, length) bdata := make([]byte, length)
if int64(bio.Bread(f, bdata)) != length { if _, err := io.ReadFull(f, bdata); err != nil {
fmt.Fprintf(os.Stderr, "%s: short pkg read %s\n", os.Args[0], filename) fmt.Fprintf(os.Stderr, "%s: short pkg read %s\n", os.Args[0], filename)
if Debug['u'] != 0 { if Debug['u'] != 0 {
errorexit() errorexit()
......
...@@ -476,7 +476,7 @@ func ldelf(f *bio.Reader, pkg string, length int64, pn string) { ...@@ -476,7 +476,7 @@ func ldelf(f *bio.Reader, pkg string, length int64, pn string) {
var sect *ElfSect var sect *ElfSect
var sym ElfSym var sym ElfSym
var symbols []*LSym var symbols []*LSym
if bio.Bread(f, hdrbuf[:]) != len(hdrbuf) { if _, err := io.ReadFull(f, hdrbuf[:]); err != nil {
goto bad goto bad
} }
hdr = new(ElfHdrBytes) hdr = new(ElfHdrBytes)
...@@ -986,9 +986,11 @@ func elfmap(elfobj *ElfObj, sect *ElfSect) (err error) { ...@@ -986,9 +986,11 @@ func elfmap(elfobj *ElfObj, sect *ElfSect) (err error) {
} }
sect.base = make([]byte, sect.size) sect.base = make([]byte, sect.size)
err = fmt.Errorf("short read") if elfobj.f.Seek(int64(uint64(elfobj.base)+sect.off), 0) < 0 {
if elfobj.f.Seek(int64(uint64(elfobj.base)+sect.off), 0) < 0 || bio.Bread(elfobj.f, sect.base) != len(sect.base) { return fmt.Errorf("short read: seek not successful")
return err }
if _, err := io.ReadFull(elfobj.f, sect.base); err != nil {
return fmt.Errorf("short read: %v", err)
} }
return nil return nil
......
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"cmd/internal/sys" "cmd/internal/sys"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"io"
"log" "log"
"sort" "sort"
) )
...@@ -299,7 +300,10 @@ func macholoadrel(m *LdMachoObj, sect *LdMachoSect) int { ...@@ -299,7 +300,10 @@ 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 || bio.Bread(m.f, buf) != n { if m.f.Seek(m.base+int64(sect.reloff), 0) < 0 {
return -1
}
if _, err := io.ReadFull(m.f, buf); err != nil {
return -1 return -1
} }
var p []byte var p []byte
...@@ -345,7 +349,10 @@ func macholoaddsym(m *LdMachoObj, d *LdMachoDysymtab) int { ...@@ -345,7 +349,10 @@ 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 || bio.Bread(m.f, p) != len(p) { if m.f.Seek(m.base+int64(d.indirectsymoff), 0) < 0 {
return -1
}
if _, err := io.ReadFull(m.f, p); err != nil {
return -1 return -1
} }
...@@ -362,7 +369,10 @@ func macholoadsym(m *LdMachoObj, symtab *LdMachoSymtab) int { ...@@ -362,7 +369,10 @@ 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 || bio.Bread(m.f, strbuf) != len(strbuf) { if m.f.Seek(m.base+int64(symtab.stroff), 0) < 0 {
return -1
}
if _, err := io.ReadFull(m.f, strbuf); err != nil {
return -1 return -1
} }
...@@ -372,7 +382,10 @@ func macholoadsym(m *LdMachoObj, symtab *LdMachoSymtab) int { ...@@ -372,7 +382,10 @@ 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 || bio.Bread(m.f, symbuf) != len(symbuf) { if m.f.Seek(m.base+int64(symtab.symoff), 0) < 0 {
return -1
}
if _, err := io.ReadFull(m.f, symbuf); err != nil {
return -1 return -1
} }
sym := make([]LdMachoSym, symtab.nsym) sym := make([]LdMachoSym, symtab.nsym)
...@@ -433,7 +446,7 @@ func ldmacho(f *bio.Reader, pkg string, length int64, pn string) { ...@@ -433,7 +446,7 @@ func ldmacho(f *bio.Reader, pkg string, length int64, pn string) {
Ctxt.IncVersion() Ctxt.IncVersion()
base := f.Offset() base := f.Offset()
if bio.Bread(f, hdr[:]) != len(hdr) { if _, err := io.ReadFull(f, hdr[:]); err != nil {
goto bad goto bad
} }
...@@ -455,8 +468,7 @@ func ldmacho(f *bio.Reader, pkg string, length int64, pn string) { ...@@ -455,8 +468,7 @@ func ldmacho(f *bio.Reader, pkg string, length int64, pn string) {
} }
if is64 { if is64 {
var tmp [4]uint8 f.Seek(4, 1) // skip reserved word in header
bio.Bread(f, tmp[:4]) // skip reserved word in header
} }
m = new(LdMachoObj) m = new(LdMachoObj)
...@@ -494,7 +506,7 @@ func ldmacho(f *bio.Reader, pkg string, length int64, pn string) { ...@@ -494,7 +506,7 @@ func ldmacho(f *bio.Reader, pkg string, length int64, pn string) {
m.cmd = make([]LdMachoCmd, ncmd) m.cmd = make([]LdMachoCmd, ncmd)
off = uint32(len(hdr)) off = uint32(len(hdr))
cmdp = make([]byte, cmdsz) cmdp = make([]byte, cmdsz)
if bio.Bread(f, cmdp) != len(cmdp) { if _, err2 := io.ReadFull(f, cmdp); err2 != nil {
err = fmt.Errorf("reading cmds: %v", err) err = fmt.Errorf("reading cmds: %v", err)
goto bad goto bad
} }
...@@ -557,7 +569,11 @@ func ldmacho(f *bio.Reader, pkg string, length int64, pn string) { ...@@ -557,7 +569,11 @@ func ldmacho(f *bio.Reader, pkg string, length int64, pn string) {
} }
dat = make([]byte, c.seg.filesz) dat = make([]byte, c.seg.filesz)
if f.Seek(m.base+int64(c.seg.fileoff), 0) < 0 || bio.Bread(f, dat) != len(dat) { if f.Seek(m.base+int64(c.seg.fileoff), 0) < 0 {
err = fmt.Errorf("cannot load object data: %v", err)
goto bad
}
if _, err2 := io.ReadFull(f, dat); err2 != nil {
err = fmt.Errorf("cannot load object data: %v", err) err = fmt.Errorf("cannot load object data: %v", err)
goto bad goto bad
} }
......
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
"cmd/internal/sys" "cmd/internal/sys"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"io"
"log" "log"
"sort" "sort"
"strconv" "strconv"
...@@ -176,13 +177,13 @@ func ldpe(f *bio.Reader, pkg string, length int64, pn string) { ...@@ -176,13 +177,13 @@ func ldpe(f *bio.Reader, pkg string, length int64, pn string) {
// load string table // load string table
f.Seek(int64(base)+int64(peobj.fh.PointerToSymbolTable)+int64(len(symbuf))*int64(peobj.fh.NumberOfSymbols), 0) f.Seek(int64(base)+int64(peobj.fh.PointerToSymbolTable)+int64(len(symbuf))*int64(peobj.fh.NumberOfSymbols), 0)
if bio.Bread(f, symbuf[:4]) != 4 { if _, err := io.ReadFull(f, symbuf[:4]); err != nil {
goto bad goto bad
} }
l = Le32(symbuf[:]) l = Le32(symbuf[:])
peobj.snames = make([]byte, l) peobj.snames = make([]byte, l)
f.Seek(int64(base)+int64(peobj.fh.PointerToSymbolTable)+int64(len(symbuf))*int64(peobj.fh.NumberOfSymbols), 0) f.Seek(int64(base)+int64(peobj.fh.PointerToSymbolTable)+int64(len(symbuf))*int64(peobj.fh.NumberOfSymbols), 0)
if bio.Bread(f, peobj.snames) != len(peobj.snames) { if _, err := io.ReadFull(f, peobj.snames); err != nil {
goto bad goto bad
} }
...@@ -205,7 +206,7 @@ func ldpe(f *bio.Reader, pkg string, length int64, pn string) { ...@@ -205,7 +206,7 @@ func ldpe(f *bio.Reader, pkg string, length int64, pn string) {
f.Seek(int64(base)+int64(peobj.fh.PointerToSymbolTable), 0) f.Seek(int64(base)+int64(peobj.fh.PointerToSymbolTable), 0)
for i := 0; uint32(i) < peobj.fh.NumberOfSymbols; i += numaux + 1 { for i := 0; uint32(i) < peobj.fh.NumberOfSymbols; i += numaux + 1 {
f.Seek(int64(base)+int64(peobj.fh.PointerToSymbolTable)+int64(len(symbuf))*int64(i), 0) f.Seek(int64(base)+int64(peobj.fh.PointerToSymbolTable)+int64(len(symbuf))*int64(i), 0)
if bio.Bread(f, symbuf[:]) != len(symbuf) { if _, err := io.ReadFull(f, symbuf[:]); err != nil {
goto bad goto bad
} }
...@@ -293,7 +294,7 @@ func ldpe(f *bio.Reader, pkg string, length int64, pn string) { ...@@ -293,7 +294,7 @@ func ldpe(f *bio.Reader, pkg string, length int64, pn string) {
f.Seek(int64(peobj.base)+int64(rsect.sh.PointerToRelocations), 0) f.Seek(int64(peobj.base)+int64(rsect.sh.PointerToRelocations), 0)
for j = 0; j < int(rsect.sh.NumberOfRelocations); j++ { for j = 0; j < int(rsect.sh.NumberOfRelocations); j++ {
rp = &r[j] rp = &r[j]
if bio.Bread(f, symbuf[:10]) != 10 { if _, err := io.ReadFull(f, symbuf[:10]); err != nil {
goto bad goto bad
} }
rva := Le32(symbuf[0:]) rva := Le32(symbuf[0:])
...@@ -466,7 +467,10 @@ func pemap(peobj *PeObj, sect *PeSect) int { ...@@ -466,7 +467,10 @@ func pemap(peobj *PeObj, sect *PeSect) int {
if sect.sh.PointerToRawData == 0 { // .bss doesn't have data in object file if sect.sh.PointerToRawData == 0 { // .bss doesn't have data in object file
return 0 return 0
} }
if peobj.f.Seek(int64(peobj.base)+int64(sect.sh.PointerToRawData), 0) < 0 || bio.Bread(peobj.f, sect.base) != len(sect.base) { if peobj.f.Seek(int64(peobj.base)+int64(sect.sh.PointerToRawData), 0) < 0 {
return -1
}
if _, err := io.ReadFull(peobj.f, sect.base); err != nil {
return -1 return -1
} }
......
...@@ -745,12 +745,12 @@ func nextar(bp *bio.Reader, off int64, a *ArHdr) int64 { ...@@ -745,12 +745,12 @@ func nextar(bp *bio.Reader, off int64, a *ArHdr) int64 {
off++ off++
} }
bp.Seek(off, 0) bp.Seek(off, 0)
buf := make([]byte, SAR_HDR) var buf [SAR_HDR]byte
if n := bio.Bread(bp, buf); n < len(buf) { if n, err := io.ReadFull(bp, buf[:]); err != nil {
if n >= 0 { if n == 0 && err != io.EOF {
return 0 return -1
} }
return -1 return 0
} }
a.name = artrim(buf[0:16]) a.name = artrim(buf[0:16])
...@@ -780,8 +780,11 @@ func objfile(lib *Library) { ...@@ -780,8 +780,11 @@ func objfile(lib *Library) {
Exitf("cannot open file %s: %v", lib.File, err) Exitf("cannot open file %s: %v", lib.File, err)
} }
magbuf := make([]byte, len(ARMAG)) for i := 0; i < len(ARMAG); i++ {
if bio.Bread(f, magbuf) != len(magbuf) || !strings.HasPrefix(string(magbuf), ARMAG) { if c, err := f.ReadByte(); err == nil && c == ARMAG[i] {
continue
}
/* load it as a regular file */ /* load it as a regular file */
l := f.Seek(0, 2) l := f.Seek(0, 2)
...@@ -811,7 +814,9 @@ func objfile(lib *Library) { ...@@ -811,7 +814,9 @@ func objfile(lib *Library) {
if Buildmode == BuildmodeShared { if Buildmode == BuildmodeShared {
before := f.Offset() before := f.Offset()
pkgdefBytes := make([]byte, atolwhex(arhdr.size)) pkgdefBytes := make([]byte, atolwhex(arhdr.size))
bio.Bread(f, pkgdefBytes) if _, err := io.ReadFull(f, pkgdefBytes); err != nil {
Diag("%s: short read on archive file symbol header: %v", lib.File, err)
}
hash := sha1.Sum(pkgdefBytes) hash := sha1.Sum(pkgdefBytes)
lib.hash = hash[:] lib.hash = hash[:]
f.Seek(before, 0) f.Seek(before, 0)
......
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