diff options
author | David Crocker <dcrocker@eschertech.com> | 2022-07-04 11:14:49 +0300 |
---|---|---|
committer | David Crocker <dcrocker@eschertech.com> | 2022-07-04 11:14:49 +0300 |
commit | 4aaab38a855c0ec32d4cb8d701a61aaa596c59dc (patch) | |
tree | b6807992f7451ee070fc2b469eee6a0f74039a9f | |
parent | 4f6a54a735a993c450656a1180d27323668186cf (diff) |
Improved memory safety of ExpressionValue by removing SetType function
-rw-r--r-- | src/GCodes/GCodeBuffer/ExpressionParser.cpp | 9 | ||||
-rw-r--r-- | src/ObjectModel/ObjectModel.cpp | 28 | ||||
-rw-r--r-- | src/ObjectModel/ObjectModel.h | 24 |
3 files changed, 37 insertions, 24 deletions
diff --git a/src/GCodes/GCodeBuffer/ExpressionParser.cpp b/src/GCodes/GCodeBuffer/ExpressionParser.cpp index 25ef1a24..f6b882b6 100644 --- a/src/GCodes/GCodeBuffer/ExpressionParser.cpp +++ b/src/GCodes/GCodeBuffer/ExpressionParser.cpp @@ -113,8 +113,7 @@ void ExpressionParser::ParseInternal(ExpressionValue& val, bool evaluate, uint8_ { case TypeCode::Uint32: // Convert enumeration to integer - val.iVal = (int32_t)val.uVal; - val.SetType(TypeCode::Int32); + val.SetInt((int32_t)val.uVal); break; case TypeCode::Int32: @@ -122,8 +121,7 @@ void ExpressionParser::ParseInternal(ExpressionValue& val, bool evaluate, uint8_ break; case TypeCode::DateTime_tc: // unary + converts a DateTime to a seconds count - val.iVal = (uint32_t)val.Get56BitValue(); - val.SetType(TypeCode::Int32); + val.SetInt((uint32_t)val.Get56BitValue()); break; default: @@ -336,8 +334,7 @@ void ExpressionParser::ParseInternal(ExpressionValue& val, bool evaluate, uint8_ if (val2.GetType() == TypeCode::DateTime_tc) { // Difference of two data/times - val.SetType(TypeCode::Int32); - val.iVal = (int32_t)(val.Get56BitValue() - val2.Get56BitValue()); + val.SetInt((int32_t)(val.Get56BitValue() - val2.Get56BitValue())); } else if (val2.GetType() == TypeCode::Uint32) { diff --git a/src/ObjectModel/ObjectModel.cpp b/src/ObjectModel/ObjectModel.cpp index 5bffcd3c..e7f86716 100644 --- a/src/ObjectModel/ObjectModel.cpp +++ b/src/ObjectModel/ObjectModel.cpp @@ -193,6 +193,34 @@ void ExpressionValue::Release() noexcept } } +void ExpressionValue::SetBool(bool b) noexcept +{ + Release(); + type = (uint32_t)TypeCode::Bool; + bVal = b; +} + +void ExpressionValue::SetInt(int32_t i) noexcept +{ + Release(); + type = (uint32_t)TypeCode::Int32; + iVal = i; +} + +void ExpressionValue::SetFloat(float f, uint32_t digits) noexcept { Release(); type = (uint32_t)TypeCode::Float; fVal = f; param = digits; } + +void ExpressionValue::SetDriverId(DriverId did) noexcept +{ + Release(); + type = (uint32_t)TypeCode::DriverId_tc; +#if SUPPORT_CAN_EXPANSION + param = did.boardAddress; +#else + param = 0; +#endif + uVal = did.localDriver; +} + #if SUPPORT_CAN_EXPANSION // Given that this is a CanExpansionBoardDetails value, extract the part requested according to the parameter and append it to the string diff --git a/src/ObjectModel/ObjectModel.h b/src/ObjectModel/ObjectModel.h index 146e8f49..3d05ac8c 100644 --- a/src/ObjectModel/ObjectModel.h +++ b/src/ObjectModel/ObjectModel.h @@ -153,33 +153,21 @@ struct ExpressionValue void Release() noexcept; // release any associated storage TypeCode GetType() const noexcept { return (TypeCode)type; } - void SetType(TypeCode t) noexcept { type = (uint32_t)t; } bool IsStringType() const noexcept { return type == (uint32_t)TypeCode::CString || type == (uint32_t)TypeCode::HeapString; } - void SetBool(bool b) noexcept { Release(); type = (uint32_t)TypeCode::Bool; bVal = b; } - void SetChar(char c) noexcept { Release(); type = (uint32_t)TypeCode::Char; cVal = c; } - void SetInt(int32_t i) noexcept { Release(); type = (uint32_t)TypeCode::Int32; iVal = i; } - void SetFloat(float f) noexcept { Release(); type = (uint32_t)TypeCode::Float; fVal = f; param = MaxFloatDigitsDisplayedAfterPoint; } - void SetFloat(float f, uint32_t digits) noexcept { Release(); type = (uint32_t)TypeCode::Float; fVal = f; param = digits; } + void SetBool(bool b) noexcept; + void SetInt(int32_t i) noexcept; + void SetFloat(float f, uint32_t digits) noexcept; + void SetFloat(float f) noexcept { SetFloat(f, MaxFloatDigitsDisplayedAfterPoint); } void SetCString(const char *_ecv_array s) noexcept { Release(); type = (uint32_t)TypeCode::CString; sVal = s; } - void SetDriverId(DriverId did) noexcept - { - Release(); - type = (uint32_t)TypeCode::DriverId_tc; -#if SUPPORT_CAN_EXPANSION - param = did.boardAddress; -#else - param = 0; -#endif - uVal = did.localDriver; - } + void SetDriverId(DriverId did) noexcept; void SetStringHandle(StringHandle sh) noexcept { Release(); type = (uint32_t)TypeCode::HeapString; shVal = sh; } void SetNull(std::nullptr_t dummy) noexcept { Release(); type = (uint32_t)TypeCode::None; } void SetDateTime(time_t t) noexcept { Release(); type = (uint32_t)TypeCode::DateTime_tc; Set56BitValue(t); } // Store a 56-bit value - void Set56BitValue(uint64_t v) { Release(); param = (uint32_t)(v >> 32) & 0x00FFFFFFu; uVal = (uint32_t)v; } + void Set56BitValue(uint64_t v) { param = (uint32_t)(v >> 32) & 0x00FFFFFFu; uVal = (uint32_t)v; } // Extract a 56-bit value that we have stored. Used to retrieve date/times and large bitmaps. uint64_t Get56BitValue() const noexcept { return ((uint64_t)param << 32) | uVal; } |