Fix numeric parsing bug when only a single number. Added testcase.

This commit is contained in:
Gunnsteinn Hall 2019-03-14 01:07:32 +00:00
parent 0686b01fa9
commit 443b168e48
3 changed files with 48 additions and 22 deletions

View File

@ -85,7 +85,7 @@ func (parser *PdfParser) lookupObjectViaOS(sobjNumber int, objNum int) (PdfObjec
}
sod := so.PdfObjectDictionary
common.Log.Trace("so d: %s\n", *sod)
common.Log.Trace("so d: %s\n", sod.String())
name, ok := sod.Get("Type").(*PdfObjectName)
if !ok {
common.Log.Debug("ERROR: Object stream should always have a Type")

View File

@ -526,8 +526,15 @@ func (parser *PdfParser) parseObject() (PdfObject, error) {
for {
bb, err := parser.reader.Peek(2)
if err != nil {
// If EOFs after 1 byte then should still try to continue parsing.
if err != io.EOF || len(bb) == 0 {
return nil, err
}
if len(bb) == 1 {
// Add space as code below is expecting 2 bytes.
bb = append(bb, ' ')
}
}
common.Log.Trace("Peek string: %s", string(bb))
// Determine type.

View File

@ -588,6 +588,7 @@ endobj`
common.Log.Debug("Xref dict: %s", xrefDict)
}
// TODO(gunnsth): Clear up. Should define clear inputs and expectation data and then run it.
func TestObjectParse(t *testing.T) {
parser := PdfParser{}
@ -601,6 +602,24 @@ func TestObjectParse(t *testing.T) {
return
}
// Integer
rawText = "0"
parser.rs, parser.reader, parser.fileSize = makeReaderForText(rawText)
obj, err = parser.parseObject()
if err != nil {
t.Errorf("Error parsing object: %v", err)
return
}
nump, ok := obj.(*PdfObjectInteger)
if !ok {
t.Errorf("Unable to identify integer")
return
}
if *nump != 0 {
t.Errorf("Wrong value, expecting 9 (%d)", *nump)
return
}
// Integer
rawText = "9 0 false"
parser.rs, parser.reader, parser.fileSize = makeReaderForText(rawText)
@ -610,7 +629,7 @@ func TestObjectParse(t *testing.T) {
t.Errorf("Error parsing object")
return
}
nump, ok := obj.(*PdfObjectInteger)
nump, ok = obj.(*PdfObjectInteger)
if !ok {
t.Errorf("Unable to identify integer")
return