Repository navigation
Expand file tree
/
Copy pathlex_test.go
More file actions
106 lines (94 loc) · 3.42 KB
/
Copy pathlex_test.go
File metadata and controls
106 lines (94 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package pdf
import (
"bytes"
"fmt"
"testing"
"time"
)
// buildUnterminatedArrayPDF constructs a minimal single-page PDF whose
// content stream ends inside an array that is never closed. Real-world
// truncated or malformed PDFs exhibit the same shape: the tokenizer hits
// end of input while readArray is still collecting elements.
func buildUnterminatedArrayPDF() []byte {
// The content stream ends inside "[ ... " with no closing "]".
return buildContentPDF("BT /F1 12 Tf [ (hello) 1 2")
}
// buildContentPDF constructs a minimal single-page PDF whose content stream
// is content.
func buildContentPDF(content string) []byte {
var buf bytes.Buffer
offsets := make([]int, 5)
buf.WriteString("%PDF-1.4\n")
objs := []string{
"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n",
"2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n",
"3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Resources << >> /Contents 4 0 R >>\nendobj\n",
fmt.Sprintf("4 0 obj\n<< /Length %d >>\nstream\n%s\nendstream\nendobj\n", len(content), content),
}
for i, obj := range objs {
offsets[i+1] = buf.Len()
buf.WriteString(obj)
}
xrefOffset := buf.Len()
buf.WriteString("xref\n0 5\n")
buf.WriteString("0000000000 65535 f \n")
for i := 1; i <= 4; i++ {
fmt.Fprintf(&buf, "%010d 00000 n \n", offsets[i])
}
buf.WriteString("trailer\n<< /Size 5 /Root 1 0 R >>\nstartxref\n")
fmt.Fprintf(&buf, "%d\n%%%%EOF\n", xrefOffset)
return buf.Bytes()
}
// TestUnterminatedArrayTerminates verifies that text extraction terminates
// on a PDF whose content stream is truncated inside an unterminated array.
// Before readArray handled io.EOF, readToken returned io.EOF as a token
// value, which matched neither nil nor keyword("]"), so readArray appended
// io.EOF objects forever, allocating memory without bound.
func TestUnterminatedArrayTerminates(t *testing.T) {
data := buildUnterminatedArrayPDF()
r, err := NewReader(bytes.NewReader(data), int64(len(data)))
if err != nil {
t.Fatalf("NewReader: %v", err)
}
done := make(chan struct{})
go func() {
defer close(done)
// The extracted text is irrelevant; the call just has to return.
r.GetPlainText()
}()
select {
case <-done:
// ok: extraction terminated
case <-time.After(5 * time.Second):
t.Fatal("GetPlainText did not return within 5s: readArray is looping on io.EOF at end of a truncated content stream")
}
}
// TestUnterminatedHexStringTerminates verifies that text extraction
// terminates on a PDF whose content stream ends inside a hex string, as it
// does when the stream's /Length is short. At end of input readByte returns
// '\n', and readHexString skipped it as white space for ever.
func TestUnterminatedHexStringTerminates(t *testing.T) {
for _, content := range []string{
"BT /F1 12 Tf <4142", // between two pairs of digits
"BT /F1 12 Tf <414", // inside a pair
"BT /F1 12 Tf [ <41", // inside an array too
} {
data := buildContentPDF(content)
r, err := NewReader(bytes.NewReader(data), int64(len(data)))
if err != nil {
t.Fatalf("%q: NewReader: %v", content, err)
}
done := make(chan struct{})
go func() {
defer close(done)
// The extracted text is irrelevant; the call just has to return.
r.GetPlainText()
}()
select {
case <-done:
// ok: extraction terminated
case <-time.After(5 * time.Second):
t.Fatalf("%q: GetPlainText did not return within 5s: readHexString is looping at the end of a content stream", content)
}
}
}