Skip to content

Commit

Permalink
Merge pull request #1837 from Exiv2/mergify/bp/main/pr-1831
Browse files Browse the repository at this point in the history
Safer casting from double to long in value.hpp (backport #1831)
  • Loading branch information
kevinbackhouse authored Aug 3, 2021
2 parents 0fab606 + 4674fae commit 726510f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
18 changes: 18 additions & 0 deletions include/exiv2/value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1628,6 +1628,24 @@ namespace Exiv2 {
}
// #55 crash when value_.at(n).first == LONG_MIN
#define LARGE_INT 1000000
// Specialization for double
template<>
inline long ValueType<double>::toLong(long n) const
{
const double v = value_.at(n);
ok_ = (INT_MIN <= v && v <= INT_MAX);
if (!ok_) return 0;
return static_cast<long>(v);
}
// Specialization for float
template<>
inline long ValueType<float>::toLong(long n) const
{
const double v = value_.at(n);
ok_ = (INT_MIN <= v && v <= INT_MAX);
if (!ok_) return 0;
return static_cast<long>(v);
}
// Specialization for rational
template<>
inline long ValueType<Rational>::toLong(long n) const
Expand Down
Binary file added test/data/issue_1830_poc.tiff
Binary file not shown.
17 changes: 17 additions & 0 deletions tests/bugfixes/github/test_issue_1830.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-

from system_tests import CaseMeta, CopyTmpFiles, path, check_no_ASAN_UBSAN_errors

class ValueTypeFloatToLong(metaclass=CaseMeta):
"""
Regression test for the bug described in:
https://github.com/Exiv2/exiv2/issues/1830
"""
url = "https://github.com/Exiv2/exiv2/issues/1830"

filename = path("$data_path/issue_1830_poc.tiff")
commands = ["$exiv2 -q $filename"]
stderr = [""]
retval = [0]

compare_stdout = check_no_ASAN_UBSAN_errors

0 comments on commit 726510f

Please sign in to comment.