From 4674fae4ef20f5b52095dc1701a2d01a3761d67a Mon Sep 17 00:00:00 2001 From: Kevin Backhouse Date: Mon, 2 Aug 2021 23:04:58 +0100 Subject: [PATCH] Safer casting from double to long. (cherry picked from commit 372e28c41044a9e8b65d7d7093e028e30a7c8c8a) --- include/exiv2/value.hpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/include/exiv2/value.hpp b/include/exiv2/value.hpp index 788a65b4..486da970 100644 --- a/include/exiv2/value.hpp +++ b/include/exiv2/value.hpp @@ -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::toLong(long n) const + { + const double v = value_.at(n); + ok_ = (INT_MIN <= v && v <= INT_MAX); + if (!ok_) return 0; + return static_cast(v); + } + // Specialization for float + template<> + inline long ValueType::toLong(long n) const + { + const double v = value_.at(n); + ok_ = (INT_MIN <= v && v <= INT_MAX); + if (!ok_) return 0; + return static_cast(v); + } // Specialization for rational template<> inline long ValueType::toLong(long n) const