Skip to content

Commit bdbe75f

Browse files
committed
add regression test
1 parent fbf7836 commit bdbe75f

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

src/debug/elf/file_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"compress/gzip"
1010
"debug/dwarf"
1111
"encoding/binary"
12+
"fmt"
1213
"io"
1314
"math/rand"
1415
"net"
@@ -17,6 +18,7 @@ import (
1718
"reflect"
1819
"runtime"
1920
"testing"
21+
"time"
2022
)
2123

2224
type fileTest struct {
@@ -954,3 +956,53 @@ func TestIssue10996(t *testing.T) {
954956
t.Fatalf("opening invalid ELF file unexpectedly succeeded")
955957
}
956958
}
959+
960+
// TestInvalidBssSection is a regression test for golang.org/issue/54967.
961+
//
962+
// (*Section).Data should not use safeio to read data from the SHT_NOBITS
963+
// section. Otherwise, when the section size is an incorrect huge value,
964+
// it will result in OOM.
965+
func TestInvalidBssSection(t *testing.T) {
966+
c := make(chan error)
967+
go func() {
968+
defer func() {
969+
switch err := recover().(type) {
970+
case nil:
971+
c <- nil
972+
case error:
973+
c <- err
974+
default:
975+
c <- fmt.Errorf("unexpected panic value: %T(%v)", err, err)
976+
}
977+
}()
978+
979+
size := uint64(3349099659509720927)
980+
s := new(Section)
981+
s.SectionHeader = SectionHeader{
982+
Name: ".bss",
983+
Type: SHT_NOBITS,
984+
Flags: SHF_WRITE + SHF_ALLOC,
985+
Addr: 0x80496d4,
986+
Offset: 0x6d4,
987+
Size: size,
988+
Link: 0x0,
989+
Info: 0x0,
990+
Addralign: 0x4,
991+
Entsize: 0x0,
992+
FileSize: size,
993+
}
994+
_, _ = s.Data()
995+
}()
996+
997+
select {
998+
case err := <-c:
999+
wantErr := "runtime error: makeslice: len out of range"
1000+
if err == nil {
1001+
t.Errorf("got error nil; want error %q", wantErr)
1002+
} else if errMsg := err.Error(); errMsg != wantErr {
1003+
t.Errorf("got error %q; want error %q", errMsg, wantErr)
1004+
}
1005+
case <-time.After(time.Second):
1006+
t.Fatal("test timed out (saferio.ReadData is called?)")
1007+
}
1008+
}

0 commit comments

Comments
 (0)