From bb83d3fce1dca0df6fd15ead4fc6acd4ebdf539e Mon Sep 17 00:00:00 2001 From: Travis McPeak Date: Wed, 27 Jan 2016 10:10:35 -0800 Subject: [PATCH] Fixing hang in get_module_qualname_from_path This commit fixes a possible hang in the get_module_qualname... function. When running against a '.' target, the while statement would loop forever looking for a head of '/', when all it would ever find was '.'. Now either option is enough to break the loop. A head of '.' already indicates we're as far down as we can get since there is no non-file path left. Closes-Bug: #1538633 Change-Id: I4aca741b816e4203d6b4da4e62c5edd37d553da4 --- bandit/core/utils.py | 2 +- tests/unit/core/test_util.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/bandit/core/utils.py b/bandit/core/utils.py index 10209484..766ff241 100644 --- a/bandit/core/utils.py +++ b/bandit/core/utils.py @@ -186,7 +186,7 @@ def get_module_qualname_from_path(path): ' Missing path or file name' % (path)) qname = [os.path.splitext(tail)[0]] - while head != '/': + while head not in ['/', '.']: if os.path.isfile(os.path.join(head, '__init__.py')): (head, tail) = os.path.split(head) qname.insert(0, tail) diff --git a/tests/unit/core/test_util.py b/tests/unit/core/test_util.py index 20c8e5dd..428aa55e 100644 --- a/tests/unit/core/test_util.py +++ b/tests/unit/core/test_util.py @@ -103,6 +103,14 @@ class UtilTests(testtools.TestCase): self.tempdir, 'good', 'a', 'b', 'c', 'test_typical.py')) self.assertEqual('good.a.b.c.test_typical', name) + def test_get_module_qualname_from_path_with_dot(self): + '''Test get_module_qualname_from_path with a "." .''' + + name = b_utils.get_module_qualname_from_path(os.path.join( + '.', '__init__.py')) + + self.assertEqual('__init__', name) + def test_get_module_qualname_from_path_abs_missingmid(self): # Test get_module_qualname_from_path with missing module # __init__.py