diff --git a/core/parser.go b/core/parser.go index 216f4926..8cf604e7 100755 --- a/core/parser.go +++ b/core/parser.go @@ -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 } diff --git a/core/parser_test.go b/core/parser_test.go index 78f37b20..ca072269 100644 --- a/core/parser_test.go +++ b/core/parser_test.go @@ -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