Skip to content

Commit d81a5f8

Browse files
committed
fix: prevent ValueError for bizarre empty modules
https://oss-fuzz.com/testcase-detail/4972497630199808 This fuzzed example from oss-fuzz caused this error: ``` # The first line of modules can lie and say 1 always, even if the first # line of code is later. If so, map 1 to the actual first line of the # module. if env.PYBEHAVIOR.module_firstline_1 and self._multiline: > self._multiline[1] = min(self.raw_statements) ^^^^^^^^^^^^^^^^^^^^^^^^ E ValueError: min() arg is an empty sequence coverage/parser.py:206: ValueError ```
1 parent 9f94c87 commit d81a5f8

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

CHANGES.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,15 @@ Unreleased
3333
- The C extension module now conforms to `PEP 489`_, closing `issue 1977`_.
3434
Thanks, `Adam Turner <pull 1978_>`_.
3535

36+
- Fixed a "ValueError: min() arg is an empty sequence" error caused by strange
37+
empty modules, found by `oss-fuzz`_.
38+
3639
.. _issue 1746: https://github.com/nedbat/coveragepy/issues/1746
3740
.. _issue 1977: https://github.com/nedbat/coveragepy/issues/1977
3841
.. _pull 1978: https://github.com/nedbat/coveragepy/pull/1978
3942
.. _issue 1980: https://github.com/nedbat/coveragepy/issues/1980
4043
.. _PEP 489: https://peps.python.org/pep-0489
44+
.. _oss-fuzz: https://google.github.io/oss-fuzz/
4145

4246

4347
.. start-releases

coverage/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def _raw_parse(self) -> None:
201201
# The first line of modules can lie and say 1 always, even if the first
202202
# line of code is later. If so, map 1 to the actual first line of the
203203
# module.
204-
if env.PYBEHAVIOR.module_firstline_1 and self._multiline:
204+
if env.PYBEHAVIOR.module_firstline_1 and self._multiline and self.raw_statements:
205205
self._multiline[1] = min(self.raw_statements)
206206

207207
self.excluded = self.first_lines(self.excluded)

tests/test_parser.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ def g2():
218218
""")
219219
assert parser.exit_counts() == {1: 1, 2: 1, 3: 1, 5: 1}
220220

221+
def test_fuzzed_weirdness(self) -> None:
222+
# This used to cause a `min of empty sequence` error:
223+
self.parse_text("\r\\\n\n\t\t\\\n\n")
224+
221225

222226
class ExclusionParserTest(PythonParserTestBase):
223227
"""Tests for the exclusion code in PythonParser."""

0 commit comments

Comments
 (0)