8000 Make sure to stop seeking when reach beginning of file by gunnsth · Pull Request #254 · unidoc/unipdf · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
70 changes: 70 additions & 0 deletions core/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
0