diff options
author | Marc Chevrier <marc.chevrier@gmail.com> | 2020-11-23 14:03:25 (GMT) |
---|---|---|
committer | Marc Chevrier <marc.chevrier@gmail.com> | 2020-11-23 14:03:25 (GMT) |
commit | b4c994f69c6e62ab95a5746db237d0237bc87bc1 (patch) | |
tree | ff184e7a53b4809c5146addbc32eb45d89912a4a /Source | |
parent | 4549027a09756d7421651bc8312e4619707f076d (diff) | |
download | CMake-b4c994f69c6e62ab95a5746db237d0237bc87bc1.zip CMake-b4c994f69c6e62ab95a5746db237d0237bc87bc1.tar.gz CMake-b4c994f69c6e62ab95a5746db237d0237bc87bc1.tar.bz2 |
cmFileTime: Fix overflow on time computation
On Windows, time starting point is Januray, 1st of 1601.
So computing number of nanoseconds from this date exceeds 64bit
capabilities.
Diffstat (limited to 'Source')
-rw-r--r-- | Source/cmFileTime.cxx | 15 | ||||
-rw-r--r-- | Source/cmFileTime.h | 52 |
2 files changed, 40 insertions, 27 deletions
diff --git a/Source/cmFileTime.cxx b/Source/cmFileTime.cxx index 96c70fe..0606baf 100644 --- a/Source/cmFileTime.cxx +++ b/Source/cmFileTime.cxx @@ -24,13 +24,13 @@ bool cmFileTime::Load(std::string const& fileName) } # if CMake_STAT_HAS_ST_MTIM // Nanosecond resolution - this->NS = fst.st_mtim.tv_sec * NsPerS + fst.st_mtim.tv_nsec; + this->Time = fst.st_mtim.tv_sec * UtPerS + fst.st_mtim.tv_nsec; # elif CMake_STAT_HAS_ST_MTIMESPEC // Nanosecond resolution - this->NS = fst.st_mtimespec.tv_sec * NsPerS + fst.st_mtimespec.tv_nsec; + this->Time = fst.st_mtimespec.tv_sec * UtPerS + fst.st_mtimespec.tv_nsec; # else // Second resolution - this->NS = fst.st_mtime * NsPerS; + this->Time = fst.st_mtime * UtPerS; # endif #else // Windows version. Get the modification time from extended file attributes. @@ -41,10 +41,11 @@ bool cmFileTime::Load(std::string const& fileName) } // Copy the file time to the output location. - this->NS = (static_cast<NSC>(fdata.ftLastWriteTime.dwHighDateTime) << 32) | - static_cast<NSC>(fdata.ftLastWriteTime.dwLowDateTime); - // The file time resolution is 100 ns. - this->NS *= 100; + using uint64 = unsigned long long; + + this->Time = static_cast<TimeType>( + (uint64(fdata.ftLastWriteTime.dwHighDateTime) << 32) + + fdata.ftLastWriteTime.dwLowDateTime); #endif return true; } diff --git a/Source/cmFileTime.h b/Source/cmFileTime.h index f496cdc..4419880 100644 --- a/Source/cmFileTime.h +++ b/Source/cmFileTime.h @@ -13,9 +13,15 @@ class cmFileTime { public: - using NSC = long long; - static constexpr NSC NsPerS = 1000000000; - + using TimeType = long long; + // unit time per second +#if !defined(_WIN32) || defined(__CYGWIN__) + // unit time is one nanosecond + static constexpr TimeType UtPerS = 1000000000; +#else + // unit time is 100 nanosecond + static constexpr TimeType UtPerS = 10000000; +#endif cmFileTime() = default; ~cmFileTime() = default; @@ -28,22 +34,28 @@ public: /** * @brief Return true if this is older than ftm */ - bool Older(cmFileTime const& ftm) const { return (this->NS - ftm.NS) < 0; } + bool Older(cmFileTime const& ftm) const + { + return (this->Time - ftm.Time) < 0; + } /** * @brief Return true if this is newer than ftm */ - bool Newer(cmFileTime const& ftm) const { return (ftm.NS - this->NS) < 0; } + bool Newer(cmFileTime const& ftm) const + { + return (ftm.Time - this->Time) < 0; + } /** * @brief Return true if this is the same as ftm */ - bool Equal(cmFileTime const& ftm) const { return this->NS == ftm.NS; } + bool Equal(cmFileTime const& ftm) const { return this->Time == ftm.Time; } /** * @brief Return true if this is not the same as ftm */ - bool Differ(cmFileTime const& ftm) const { return this->NS != ftm.NS; } + bool Differ(cmFileTime const& ftm) const { return this->Time != ftm.Time; } /** * @brief Compare file modification times. @@ -51,7 +63,7 @@ public: */ int Compare(cmFileTime const& ftm) const { - NSC const diff = this->NS - ftm.NS; + TimeType const diff = this->Time - ftm.Time; if (diff == 0) { return 0; } @@ -65,7 +77,7 @@ public: */ bool OlderS(cmFileTime const& ftm) const { - return (ftm.NS - this->NS) >= cmFileTime::NsPerS; + return (ftm.Time - this->Time) >= cmFileTime::UtPerS; } /** @@ -73,7 +85,7 @@ public: */ bool NewerS(cmFileTime const& ftm) const { - return (this->NS - ftm.NS) >= cmFileTime::NsPerS; + return (this->Time - ftm.Time) >= cmFileTime::UtPerS; } /** @@ -81,11 +93,11 @@ public: */ bool EqualS(cmFileTime const& ftm) const { - NSC diff = this->NS - ftm.NS; + TimeType diff = this->Time - ftm.Time; if (diff < 0) { diff = -diff; } - return (diff < cmFileTime::NsPerS); + return (diff < cmFileTime::UtPerS); } /** @@ -93,11 +105,11 @@ public: */ bool DifferS(cmFileTime const& ftm) const { - NSC diff = this->NS - ftm.NS; + TimeType diff = this->Time - ftm.Time; if (diff < 0) { diff = -diff; } - return (diff >= cmFileTime::NsPerS); + return (diff >= cmFileTime::UtPerS); } /** @@ -107,21 +119,21 @@ public: */ int CompareS(cmFileTime const& ftm) const { - NSC const diff = this->NS - ftm.NS; - if (diff <= -cmFileTime::NsPerS) { + TimeType const diff = this->Time - ftm.Time; + if (diff <= -cmFileTime::UtPerS) { return -1; } - if (diff >= cmFileTime::NsPerS) { + if (diff >= cmFileTime::UtPerS) { return 1; } return 0; } /** - * @brief The file modification time in nanoseconds + * @brief The file modification time in unit time per second */ - NSC GetNS() const { return this->NS; } + TimeType GetTime() const { return this->Time; } private: - NSC NS = 0; + TimeType Time = 0; }; |