Skip to content

Commit e74d51d

Browse files
committed
Fixed compilation error, moved some stypecast.h non templated functions to .cpp
1 parent 061dbd6 commit e74d51d

File tree

3 files changed

+204
-160
lines changed

3 files changed

+204
-160
lines changed

src/common/resource/sections/CDialogDef.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,19 +158,27 @@ bool CDialogDef::r_Verb( CScript & s, CTextConsole * pSrc ) // some command on t
158158
if ( *ptcArgs_ == '-' && IsSpace(ptcArgs_[1]))
159159
c = iCoordBase_, ++ptcArgs_;
160160
else if ( *ptcArgs_ == '+' )
161-
c = iCoordBase_ + gExprParser.GetSingle(++ptcArgs_ );
161+
c = iCoordBase_ + n64_narrow_n32_checked(gExprParser.GetSingle(++ptcArgs_ ));
162162
else if ( *ptcArgs_ == '-' )
163-
c = iCoordBase_ - gExprParser.GetSingle(++ptcArgs_ );
163+
c = iCoordBase_ - n64_narrow_n32_checked(gExprParser.GetSingle(++ptcArgs_ ));
164164
else if ( *ptcArgs_ == '*' )
165-
iCoordBase_ = c = iCoordBase_ + gExprParser.GetSingle( ++ptcArgs_ );
165+
iCoordBase_ = c = iCoordBase_ + n64_narrow_n32_checked(gExprParser.GetSingle( ++ptcArgs_ ));
166166
else
167-
c = gExprParser.GetSingle( ptcArgs_ );
167+
c = n64_narrow_n32_checked(gExprParser.GetSingle( ptcArgs_ ));
168168
return c;
169169
};
170170

171-
# define GET_RELATIVE(c, base) _SkipAll(ptcArgs); const int c = _CalcRelative(ptcArgs, base);
172-
# define GET_ABSOLUTE(c) _SkipAll(ptcArgs); const int c = gExprParser.GetSingle(ptcArgs);
173-
# define GET_EVAL(c) _SkipAll(ptcArgs); const int c = gExprParser.GetVal(ptcArgs);
171+
# define GET_RELATIVE(c, base) \
172+
_SkipAll(ptcArgs); \
173+
const int c = _CalcRelative(ptcArgs, base);
174+
175+
# define GET_ABSOLUTE(c) \
176+
_SkipAll(ptcArgs); \
177+
const int c = n64_narrow_n32_checked(gExprParser.GetSingle(ptcArgs), false);
178+
179+
# define GET_EVAL(c) \
180+
_SkipAll(ptcArgs); \
181+
const int c = n64_narrow_n32_checked(gExprParser.GetVal(ptcArgs), false);
174182

175183
switch( index )
176184
{
@@ -432,17 +440,17 @@ bool CDialogDef::r_Verb( CScript & s, CTextConsole * pSrc ) // some command on t
432440
if ( *ptcArgs == '-' && (IsSpace( ptcArgs[1] ) || !ptcArgs[1]) )
433441
++ptcArgs;
434442
else if ( *ptcArgs == '*' )
435-
m_iOriginX += gExprParser.GetSingle( ++ptcArgs );
443+
m_iOriginX += n64_narrow_n32_checked(gExprParser.GetSingle( ++ptcArgs ), false);
436444
else
437-
m_iOriginX = gExprParser.GetSingle( ptcArgs );
445+
m_iOriginX = n64_narrow_n32_checked(gExprParser.GetSingle( ptcArgs ), false);
438446

439447
_SkipAll( ptcArgs );
440448
if ( *ptcArgs == '-' && (IsSpace( ptcArgs[1] ) || !ptcArgs[1]) )
441449
++ptcArgs;
442450
else if ( *ptcArgs == '*' )
443-
m_iOriginY += gExprParser.GetSingle( ++ptcArgs );
451+
m_iOriginY += n64_narrow_n32_checked(gExprParser.GetSingle( ++ptcArgs ), false);
444452
else
445-
m_iOriginY = gExprParser.GetSingle( ptcArgs );
453+
m_iOriginY = n64_narrow_n32_checked(gExprParser.GetSingle( ptcArgs ), false);
446454

447455
return true;
448456
}

src/common/sphere_library/stypecast.cpp

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,127 @@ void LogEventWarnWrapper(const char* warn_str)
99
}
1010

1111
}
12+
13+
[[nodiscard]]
14+
uint32 usize_narrow_u32_checked(const size_t source_val, bool should_assert)
15+
{
16+
if (should_assert) {
17+
ASSERT(source_val <= std::numeric_limits<uint32>::max());
18+
}
19+
else if (source_val > std::numeric_limits<uint32>::max()) {
20+
detail_stypecast::LogEventWarnWrapper("[Internal] Narrowing conversion from size_t to 32 bits unsigned integer will overflow.\n");
21+
}
22+
return usize_narrow_u32(source_val);
23+
}
24+
25+
[[nodiscard]]
26+
int8 i8_from_u8_checked(const uint8 source_val, bool should_assert) // not clamping/capping
27+
{
28+
const bool would_overflow = (source_val > (uint8)std::numeric_limits<int8>::max());
29+
if (should_assert) {
30+
ASSERT(!would_overflow);
31+
}
32+
else if (would_overflow) {
33+
detail_stypecast::LogEventWarnWrapper("[Internal] Narrowing conversion from 8 bits unsigned integer to 8 bits signed integer will overflow.\n");
34+
}
35+
36+
return static_cast<int8>(source_val);
37+
}
38+
39+
[[nodiscard]]
40+
int16 i8_from_u16_checked(const uint16 source_val, bool should_assert) // not clamping/capping
41+
{
42+
const bool would_overflow = (source_val > (uint16)std::numeric_limits<int8>::max());
43+
if (should_assert) {
44+
ASSERT(!would_overflow);
45+
}
46+
else if (would_overflow) {
47+
detail_stypecast::LogEventWarnWrapper("[Internal] Narrowing conversion from 16 bits unsigned integer to 8 bits signed integer will overflow.\n");
48+
}
49+
50+
return static_cast<int16>(source_val);
51+
}
52+
53+
[[nodiscard]]
54+
int16 i16_from_u16_checked(const uint16 source_val, bool should_assert) // not clamping/capping
55+
{
56+
const bool would_overflow = (source_val > (uint16)std::numeric_limits<int16>::max());
57+
if (should_assert) {
58+
ASSERT(!would_overflow);
59+
}
60+
else if (would_overflow) {
61+
detail_stypecast::LogEventWarnWrapper("[Internal] Narrowing conversion from 16 bits unsigned integer to 16 bits signed integer will overflow.\n");
62+
}
63+
64+
return static_cast<int16>(source_val);
65+
}
66+
67+
[[nodiscard]]
68+
int16 i16_from_u32_checked(const uint32 source_val, bool should_assert) // not clamping/capping
69+
{
70+
const bool would_overflow = (source_val > (uint32)std::numeric_limits<int16>::max());
71+
if (should_assert) {
72+
ASSERT(!would_overflow);
73+
}
74+
else if (would_overflow) {
75+
detail_stypecast::LogEventWarnWrapper("[Internal] Narrowing conversion from 32 bits unsigned integer to 16 bits signed integer will overflow.\n");
76+
}
77+
78+
return static_cast<int16>(source_val);
79+
}
80+
81+
[[nodiscard]]
82+
int16 i16_from_u64_checked(const uint64 source_val, bool should_assert) // not clamping/capping
83+
{
84+
const bool would_overflow = (source_val > (uint64)std::numeric_limits<int16>::max());
85+
if (should_assert) {
86+
ASSERT(!would_overflow);
87+
}
88+
else if (would_overflow) {
89+
detail_stypecast::LogEventWarnWrapper("[Internal] Narrowing conversion from 64 bits unsigned integer to 16 bits signed integer will overflow.\n");
90+
}
91+
92+
return static_cast<int16>(source_val);
93+
}
94+
95+
[[nodiscard]]
96+
int32 i32_from_u32_checked(const uint32 source_val, bool should_assert) // not clamping/capping
97+
{
98+
const bool would_overflow = (source_val > (uint32)std::numeric_limits<int32>::max());
99+
if (should_assert) {
100+
ASSERT(!would_overflow);
101+
}
102+
else if (would_overflow) {
103+
detail_stypecast::LogEventWarnWrapper("[Internal] Narrowing conversion from 32 bits unsigned integer to 32 bits signed integer will overflow.\n");
104+
}
105+
106+
return static_cast<int32>(source_val);
107+
}
108+
109+
[[nodiscard]]
110+
int32 i32_from_u64_checked(const uint64 source_val, bool should_assert) // not clamping/capping
111+
{
112+
const bool would_overflow = (source_val > (uint64)std::numeric_limits<int64>::max());
113+
if (should_assert) {
114+
ASSERT(!would_overflow);
115+
}
116+
else if (would_overflow) {
117+
detail_stypecast::LogEventWarnWrapper("[Internal] Narrowing conversion from 64 bits unsigned integer to 32 bits signed integer will overflow.\n");
118+
}
119+
120+
return static_cast<int32>(source_val);
121+
}
122+
123+
[[nodiscard]]
124+
int64 i64_from_u64_checked(const uint64 source_val, bool should_assert) // not clamping/capping
125+
{
126+
const bool would_overflow = (source_val > (uint64)std::numeric_limits<int64>::max());
127+
if (should_assert) {
128+
ASSERT(!would_overflow);
129+
}
130+
else if (would_overflow) {
131+
detail_stypecast::LogEventWarnWrapper("[Internal] Narrowing conversion from 64 bits unsigned integer to 64 bits signed integer will overflow.\n");
132+
}
133+
134+
return static_cast<int64>(source_val);
135+
}

0 commit comments

Comments
 (0)