bytes.Buffer: restore panic on out-of-memory

Make the panic detectable, and use that in ioutil.ReadFile to
give an error if the file is too big.

R=golang-dev, minux.ma, bradfitz
CC=golang-dev
https://golang.org/cl/5563045
This commit is contained in:
Rob Pike 2012-01-21 09:46:59 -08:00
parent 4d3b9d9757
commit b0d2713b77
3 changed files with 35 additions and 22 deletions

View file

@ -392,13 +392,18 @@ func TestHuge(t *testing.T) {
if testing.Short() {
return
}
// We expect a panic.
defer func() {
if err, ok := recover().(error); ok && err == ErrTooLarge {
return
} else {
t.Error(`expected "too large" error; got`, err)
}
}()
b := new(Buffer)
big := make([]byte, 500e6)
for i := 0; i < 1000; i++ {
if _, err := b.Write(big); err != nil {
// Got error as expected. Stop
return
}
b.Write(big)
}
t.Error("error expected")
t.Error("panic expected")
}