Skip to content

Commit

Permalink
Fixed: crash on invalid chr number.
Browse files Browse the repository at this point in the history
  • Loading branch information
euske committed Dec 9, 2014
1 parent 75206ba commit 0112112
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions pdfminer/psparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def intern(self, name):
def literal_name(x):
if not isinstance(x, PSLiteral):
if STRICT:
raise PSTypeError('Literal required: %r' % x)
raise PSTypeError('Literal required: %r' % (x,))
else:
return str(x)
return x.name
Expand All @@ -131,7 +131,7 @@ def literal_name(x):
def keyword_name(x):
if not isinstance(x, PSKeyword):
if STRICT:
raise PSTypeError('Keyword required: %r' % x)
raise PSTypeError('Keyword required: %r' % (x,))
else:
return str(x)
return x.name
Expand Down Expand Up @@ -361,7 +361,10 @@ def _parse_literal_hex(self, s, i):
self.hex += c
return i+1
if self.hex:
self._curtoken += chr(int(self.hex, 16))
try:
self._curtoken += chr(int(self.hex, 16))
except ValueError:
pass
self._parse1 = self._parse_literal
return i

Expand Down Expand Up @@ -446,7 +449,10 @@ def _parse_string_1(self, s, i):
self.oct += c
return i+1
if self.oct:
self._curtoken += chr(int(self.oct, 8))
try:
self._curtoken += chr(int(self.oct, 8))
except ValueError:
pass
self._parse1 = self._parse_string
return i
if c in ESC_STRING:
Expand Down Expand Up @@ -479,9 +485,12 @@ def _parse_hexstring(self, s, i):
return len(s)
j = m.start(0)
self._curtoken += s[i:j]
token = HEX_PAIR.sub(lambda m: chr(int(m.group(0), 16)),
SPC.sub(b'', self._curtoken))
self._add_token(token)
try:
token = HEX_PAIR.sub(lambda m: chr(int(m.group(0), 16)),
SPC.sub(b'', self._curtoken))
self._add_token(token)
except ValueError:
pass
self._parse1 = self._parse_main
return j

Expand All @@ -491,7 +500,7 @@ def nexttoken(self):
self.charpos = self._parse1(self.buf, self.charpos)
token = self._tokens.pop(0)
if self.debug:
logging.debug('nexttoken: %r' % token)
logging.debug('nexttoken: %r' % (token,))
return token


Expand Down Expand Up @@ -532,7 +541,7 @@ def popall(self):

def add_results(self, *objs):
if self.debug:
logging.debug('add_results: %r' % objs)
logging.debug('add_results: %r' % (objs,))
self.results.extend(objs)
return

Expand Down Expand Up @@ -585,7 +594,7 @@ def nextobject(self):
try:
(pos, objs) = self.end_type('d')
if len(objs) % 2 != 0:
raise PSSyntaxError('Invalid dictionary construct: %r' % objs)
raise PSSyntaxError('Invalid dictionary construct: %r' % (objs,))
# construct a Python dictionary.
d = dict((literal_name(k), v) for (k, v) in choplist(2, objs) if v is not None)
self.push((pos, d))
Expand Down Expand Up @@ -613,7 +622,7 @@ def nextobject(self):
self.flush()
obj = self.results.pop(0)
if self.debug:
logging.debug('nextobject: %r' % obj)
logging.debug('nextobject: %r' % (obj,))
return obj


Expand Down

0 comments on commit 0112112

Please sign in to comment.