Skip to content

Commit

Permalink
Merge pull request #1828 from kevinbackhouse/FixIssue1827
Browse files Browse the repository at this point in the history
Check value is in range before casting from double to uint32_t
  • Loading branch information
kevinbackhouse authored Aug 3, 2021
2 parents b364b1d + ad2e722 commit ca32016
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/tags_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2585,12 +2585,22 @@ namespace Exiv2 {
URational exposureTime(float shutterSpeedValue)
{
URational ur(1, 1);
double tmp = std::exp(std::log(2.0) * static_cast<double>(shutterSpeedValue));
const double tmp = std::exp(std::log(2.0) * static_cast<double>(shutterSpeedValue));
if (tmp > 1) {
ur.second = static_cast<long>(tmp + 0.5);
// Add 0.5 for rounding.
const double x = tmp + 0.5;
// Check that x is within the range of a uint32_t before casting.
if (x <= std::numeric_limits<uint32_t>::max()) {
ur.second = static_cast<uint32_t>(x);
}
}
else {
ur.first = static_cast<long>(1/tmp + 0.5);
// Add 0.5 for rounding.
const double x = 1/tmp + 0.5;
// Check that x is within the range of a uint32_t before casting.
if (0 <= x && x <= std::numeric_limits<uint32_t>::max()) {
ur.first = static_cast<uint32_t>(x);
}
}
return ur;
}
Expand Down
Binary file added test/data/issue_1827_poc.crw
Binary file not shown.
17 changes: 17 additions & 0 deletions tests/bugfixes/github/test_issue_1827.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 ExposureTimeCastDoubleToLong(metaclass=CaseMeta):
"""
Regression test for the bug described in:
https://github.com/Exiv2/exiv2/issues/1827
"""
url = "https://github.com/Exiv2/exiv2/issues/1827"

filename = path("$data_path/issue_1827_poc.crw")
commands = ["$exiv2 $filename"]
stderr = [""]
retval = [0]

compare_stdout = check_no_ASAN_UBSAN_errors

0 comments on commit ca32016

Please sign in to comment.