Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mediafile._safe_cast: be safer when converting to int #2741

Merged
merged 1 commit into from
Nov 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions beets/mediafile.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,8 @@ def _safe_cast(out_type, val):
elif not isinstance(val, six.string_types):
val = six.text_type(val)
# Get a number from the front of the string.
val = re.match(r'[\+-]?[0-9]*', val.strip()).group(0)
if not val:
return 0
else:
return int(val)
match = re.match(r'[\+-]?[0-9]+', val.strip())
return int(match.group(0)) if match else 0

elif out_type == bool:
try:
Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ Fixes:
versions of Mutagen. :bug:`2716`
* :doc:`/plugins/fetchart`: Fix: don't skip running the fetchart plugin during import, when the
"Edit Candidates" option is used. :bug:`2734`
* Fix a crash when numeric metadata fields contain just a minus or plus sign
with no following numbers.

For developers:

Expand Down
3 changes: 3 additions & 0 deletions test/test_mediafile_edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ class InvalidValueToleranceTest(unittest.TestCase):
def test_safe_cast_string_to_int(self):
self.assertEqual(_sc(int, u'something'), 0)

def test_safe_cast_string_to_int_with_no_numbers(self):
self.assertEqual(_sc(int, u'-'), 0)

def test_safe_cast_int_string_to_int(self):
self.assertEqual(_sc(int, u'20'), 20)

Expand Down