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

Safer casting from double to long in value.hpp #1831

Merged
merged 2 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
18 changes: 18 additions & 0 deletions include/exiv2/value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1640,6 +1640,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