]> Cypherpunks repositories - gostls13.git/commitdiff
debug/pe: read string table in 10M chunks
authorIan Lance Taylor <iant@golang.org>
Thu, 14 Apr 2022 23:25:43 +0000 (16:25 -0700)
committerGopher Robot <gobot@golang.org>
Fri, 15 Apr 2022 17:17:18 +0000 (17:17 +0000)
No separate test because this makes no difference for valid PE files.

Fixes #52350

Change-Id: I2aa011a4e8b34cb08052222e94c52627ebe99fbf
Reviewed-on: https://go-review.googlesource.com/c/go/+/400378
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>

src/debug/pe/string.go

index cab0366ade26facaa7314c80c5bb19ca675e54c1..6d9023d8d6f16547055b55096b2e21f5a81e86ce 100644 (file)
@@ -44,8 +44,29 @@ func readStringTable(fh *FileHeader, r io.ReadSeeker) (StringTable, error) {
                return nil, nil
        }
        l -= 4
-       buf := make([]byte, l)
-       _, err = io.ReadFull(r, buf)
+
+       // If the string table is large, the file may be corrupt.
+       // Read in chunks to avoid crashing due to out of memory.
+       const chunk = 10 << 20 // 10M
+       var buf []byte
+       if l < chunk {
+               buf = make([]byte, l)
+               _, err = io.ReadFull(r, buf)
+       } else {
+               for l > 0 {
+                       n := l
+                       if n > chunk {
+                               n = chunk
+                       }
+                       buf1 := make([]byte, n)
+                       _, err = io.ReadFull(r, buf1)
+                       if err != nil {
+                               break
+                       }
+                       buf = append(buf, buf1...)
+                       l -= n
+               }
+       }
        if err != nil {
                return nil, fmt.Errorf("fail to read string table: %v", err)
        }