summaryrefslogtreecommitdiffstats
path: root/parser.go
diff options
context:
space:
mode:
authorThibaut Horel <thibaut.horel@gmail.com>2016-02-21 21:30:47 -0500
committerThibaut Horel <thibaut.horel@gmail.com>2016-02-21 21:30:47 -0500
commitac7a1a413b9566e0b052248502206eb3d6f8941c (patch)
treee75354a77fcfbec290e34e6e6667620f6acaaebf /parser.go
parent061b85376e76c3b28afa488369dff76241cc5c7c (diff)
downloadbibtex-ac7a1a413b9566e0b052248502206eb3d6f8941c.tar.gz
Fix bug in parser where it could get trapped in infinite loop
Diffstat (limited to 'parser.go')
-rw-r--r--parser.go26
1 files changed, 20 insertions, 6 deletions
diff --git a/parser.go b/parser.go
index fae51d6..35999e1 100644
--- a/parser.go
+++ b/parser.go
@@ -66,10 +66,21 @@ func (p *Parser) Warning(msg string) {
log.Printf("%s:%d:%d warning: %s", p.fname, p.lineno, p.colno, msg)
}
+func (p *Parser) readUnsafe() rune {
+ ch, _, _ := p.ReadRune()
+ if ch == '\n' {
+ p.lineno += 1
+ p.colno = 0
+ }
+ p.colno += 1
+ p.lastread = ch
+ return ch
+}
+
func (p *Parser) read() rune {
ch, _, err := p.ReadRune()
if err == io.EOF {
- return eof
+ panic(err)
}
if ch == '\n' {
p.lineno += 1
@@ -400,9 +411,14 @@ func (p *Parser) readDeclaration() error {
}
func (p *Parser) Parse(strict bool) error {
- var ch rune
- for {
- ch = p.read()
+
+ defer func() {
+ if r := recover(); r != nil {
+ p.Warning("Reached end of file while parsing.")
+ }
+ }()
+
+ for ch := p.readUnsafe(); ch != eof; ch = p.readUnsafe() {
if ch == '@' {
p.eatSpace()
err := p.readDeclaration()
@@ -418,8 +434,6 @@ func (p *Parser) Parse(strict bool) error {
return err
}
}
- } else if ch == eof {
- break
}
}
return nil