Make sure to stop seeking when reach beginning of file (#254)

* Fix infinite loop bug when looking for EOF
* Add testcases for EOF parsing
This commit is contained in:
Gunnsteinn Hall 2020-02-11 20:09:34 +00:00 committed by GitHub
parent fdee057a6e
commit fd4f4b0a02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 1 deletions

View File

@ -1137,7 +1137,7 @@ func (parser *PdfParser) seekToEOFMarker(fSize int64) error {
// Define an buffer length in terms of how many bytes to read from the end of the file.
var buflen int64 = 2048
for offset < fSize {
for offset < fSize-4 {
if fSize <= (buflen + offset) {
buflen = fSize - offset
}

View File

@ -479,6 +479,76 @@ func TestStreamParsing(t *testing.T) {
// TODO
}
func TestEOFParsing(t *testing.T) {
testcases := []struct {
Min int
Increment int
Max int
Suffix string
ShouldErr bool
}{
{
Min: 0,
Increment: 1,
Max: 30,
Suffix: "",
ShouldErr: true,
},
{
Min: 0,
Increment: 1,
Max: 30,
Suffix: "%%EOF",
ShouldErr: false,
},
{
Min: 0,
Increment: 1,
Max: 30,
Suffix: "%%EOF\n",
ShouldErr: false,
},
{
Min: 0,
Increment: 1,
Max: 30,
Suffix: "%EOF\n",
ShouldErr: true,
},
{
Min: 1010,
Increment: 1,
Max: 1040,
Suffix: "%%EOF\n",
ShouldErr: false,
},
{
Min: 2000,
Increment: 1,
Max: 2040,
Suffix: "%%EOF",
ShouldErr: false,
},
}
for _, tcase := range testcases {
for i := tcase.Min; i < tcase.Max; i += tcase.Increment {
b := make([]byte, i)
for j := 0; j < i; j++ {
b[j] = ' '
}
b = append(b, []byte(tcase.Suffix)...)
parser := makeParserForText(string(b))
err := parser.seekToEOFMarker(int64(len(b)))
if tcase.ShouldErr {
require.NotNil(t, err)
} else {
require.NoError(t, err)
}
}
}
}
func TestIndirectObjParsing1(t *testing.T) {
testcases := []struct {
description string