Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/dosbox-staging/dosbox-staging.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkcgen <kcgen@users.noreply.github.com>2022-10-15 22:45:00 +0300
committerkcgen <1557255+kcgen@users.noreply.github.com>2022-10-16 00:39:09 +0300
commit898fccd04487bd9f75f513be694278f2cf6cdf1a (patch)
tree6d78f873c638a1b81e24965489f40cf51e4d1184
parentd4b157b63c6de29bf54425c97b431eab72e45e42 (diff)
Fix bitwise shift of negative in DOS date pack call
Flagged in Stettlers II: - runtime error: left shift of negative value -76
-rw-r--r--include/dos_inc.h10
1 files changed, 9 insertions, 1 deletions
diff --git a/include/dos_inc.h b/include/dos_inc.h
index 58ea2c23c..caddb9b06 100644
--- a/include/dos_inc.h
+++ b/include/dos_inc.h
@@ -24,6 +24,7 @@
#include "dosbox.h"
+#include <algorithm>
#include <cstddef>
#include <type_traits>
@@ -145,7 +146,14 @@ constexpr uint16_t DOS_PackDate(const uint16_t year,
const uint16_t mon,
const uint16_t day) noexcept
{
- const auto y_bits = 0b1111111000000000 & ((year - 1980) << 9);
+ const int delta_year = year - 1980;
+
+ constexpr int delta_year_min = 0;
+ constexpr int delta_year_max = INT8_MAX;
+ const auto years_after_1980 = static_cast<uint16_t>(
+ std::clamp(delta_year, delta_year_min, delta_year_max));
+
+ const auto y_bits = 0b1111111000000000 & (years_after_1980 << 9);
const auto m_bits = 0b0000000111100000 & (mon << 5);
const auto d_bits = 0b0000000000011111 & day;
const auto packed = y_bits | m_bits | d_bits;