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

Check value is in range before casting from double to uint32_t #1828

Merged
merged 3 commits into from
Aug 3, 2021
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
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