From a27f51242b041f8afd79d4fa2d4d6eaf1dda9f0c Mon Sep 17 00:00:00 2001 From: Randall Nortman Date: Wed, 13 Apr 2016 21:05:07 -0400 Subject: [PATCH] Handle IndexError during version string parsing Some odd version strings can cause SemanticVersion.from_pip_string() to raise IndexError. This change converts IndexError to ValueError. Change-Id: Ic3046817b6c5808c61c4a3bc3d912501e6a52274 Closes-Bug: #1570145 --- pbr/tests/test_setup.py | 4 +++- pbr/version.py | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/pbr/tests/test_setup.py b/pbr/tests/test_setup.py index cd95c736..f3fcd409 100644 --- a/pbr/tests/test_setup.py +++ b/pbr/tests/test_setup.py @@ -99,7 +99,8 @@ class SkipFileWrites(base.BaseTestCase): (self.option_value.lower() in options.TRUE_VALUES or self.env_value is not None)) -_changelog_content = """04316fe\x00Make python\x00 (review/monty_taylor/27519) +_changelog_content = """7780758\x00Break parser\x00 (tag: 1_foo.1) +04316fe\x00Make python\x00 (review/monty_taylor/27519) 378261a\x00Add an integration test script.\x00 3c373ac\x00Merge "Lib\x00 (HEAD, tag: 2013.2.rc2, tag: 2013.2, mile-proposed) 182feb3\x00Fix pip invocation for old versions of pip.\x00 (tag: 0.5.17) @@ -159,6 +160,7 @@ class GitLogsTest(base.BaseTestCase): self.assertNotIn("ev)il", changelog_contents) self.assertNotIn("e(vi)l", changelog_contents) self.assertNotIn('Merge "', changelog_contents) + self.assertNotIn('1_foo.1', changelog_contents) def test_generate_authors(self): author_old = u"Foo Foo " diff --git a/pbr/version.py b/pbr/version.py index d3fe4013..619c4327 100644 --- a/pbr/version.py +++ b/pbr/version.py @@ -142,6 +142,14 @@ class SemanticVersion(object): ever released - we're treating that as a critical bug that we ever made them and have stopped doing that. """ + + try: + return klass._from_pip_string_unsafe(version_string) + except IndexError: + raise ValueError("Invalid version %r" % version_string) + + @classmethod + def _from_pip_string_unsafe(klass, version_string): # Versions need to start numerically, ignore if not if not version_string[:1].isdigit(): raise ValueError("Invalid version %r" % version_string)