]> Cypherpunks repositories - gostls13.git/commitdiff
io/ioutil: fix crash when Stat fails
authorRuss Cox <rsc@golang.org>
Wed, 14 Mar 2012 18:47:13 +0000 (14:47 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 14 Mar 2012 18:47:13 +0000 (14:47 -0400)
Fixes #3320.

R=golang-dev, gri
CC=golang-dev
https://golang.org/cl/5824051

src/pkg/io/ioutil/ioutil.go

index 180afc2c221459f39a4d20d89b55e3540d8efb79..f072b8c754a53f14fafedfc4214c35385d4fa909 100644 (file)
@@ -53,10 +53,13 @@ func ReadFile(filename string) ([]byte, error) {
        defer f.Close()
        // It's a good but not certain bet that FileInfo will tell us exactly how much to
        // read, so let's try it but be prepared for the answer to be wrong.
-       fi, err := f.Stat()
        var n int64
-       if size := fi.Size(); err == nil && size < 2e9 { // Don't preallocate a huge buffer, just in case.
-               n = size
+
+       if fi, err := f.Stat(); err == nil {
+               // Don't preallocate a huge buffer, just in case.
+               if size := fi.Size(); size < 1e9 {
+                       n = size
+               }
        }
        // As initial capacity for readAll, use n + a little extra in case Size is zero,
        // and to avoid another allocation after Read has filled the buffer.  The readAll