diff --git a/pycparser/c_lexer.py b/pycparser/c_lexer.py index 4f66b244..53ac4383 100644 --- a/pycparser/c_lexer.py +++ b/pycparser/c_lexer.py @@ -278,7 +278,11 @@ def success(pp_line: Optional[str], pp_filename: Optional[str]) -> None: if pp_line is None: self._error("line number missing in #line", self._pos + line_len) else: - self._lineno = int(pp_line) + try: + self._lineno = int(pp_line) + except ValueError: + self._error("invalid #line directive", self._pos + line_len) + return if pp_filename is not None: self._filename = pp_filename self._pos = line_end + 1 diff --git a/tests/test_c_lexer.py b/tests/test_c_lexer.py index 00acddcd..01bc2c2c 100644 --- a/tests/test_c_lexer.py +++ b/tests/test_c_lexer.py @@ -572,6 +572,10 @@ def test_preprocessor(self): self.assertLexerError('#line "ka"', ERR_FILENAME_BEFORE_LINE) self.assertLexerError("#line df", ERR_INVALID_LINE_DIRECTIVE) self.assertLexerError("#line \n", ERR_LINENUM_MISSING) + # Integer suffixes are not valid in #line directives (issue #587). + self.assertLexerError("#line 0u", ERR_INVALID_LINE_DIRECTIVE) + self.assertLexerError("#line 1U", ERR_INVALID_LINE_DIRECTIVE) + self.assertLexerError("#line 10uLL", ERR_INVALID_LINE_DIRECTIVE) # a compatible preprocessor must remove comments. self.assertLexerError("//", ERR_COMMENT) self.assertLexerError("/*", ERR_COMMENT)