Skip to content

Commit e83c6ca

Browse files
Fix integer clamping by casting to int64_t before std::clamp
Casts input values to int64_t before applying std::clamp to ensure correct clamping behavior for signed integer types. This prevents potential issues with type mismatches and ensures values are properly constrained within the limits of the target type. See Android build in #362
1 parent 4447aec commit e83c6ca

2 files changed

Lines changed: 3 additions & 3 deletions

File tree

src/common/AbstractObject.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,7 @@ namespace {
10641064
if (arrayInput->soap_type() == SOAP_TYPE_gsoap_resqml2_0_1_resqml20__IntegerHdf5Array) {
10651065
auto const* hdfArray = static_cast<gsoap_resqml2_0_1::resqml20__IntegerHdf5Array const*>(arrayInput);
10661066
if constexpr (std::is_signed_v<T>) {
1067-
nullValue = static_cast<T>(std::clamp(hdfArray->NullValue,
1067+
nullValue = static_cast<T>(std::clamp(static_cast<int64_t>(hdfArray->NullValue),
10681068
static_cast<int64_t>((std::numeric_limits<T>::min)()), static_cast<int64_t>((std::numeric_limits<T>::max)())));
10691069
}
10701070
else {
@@ -1095,7 +1095,7 @@ namespace {
10951095
externalDataArrayParts = static_cast<gsoap_eml2_3::eml23__IntegerExternalArray const*>(arrayInput)->Values->ExternalDataArrayPart;
10961096
const int64_t xmlNullValue = static_cast<gsoap_eml2_3::eml23__IntegerExternalArray const*>(arrayInput)->NullValue;
10971097
if constexpr (std::is_signed_v<T>) {
1098-
nullValue = static_cast<T>(std::clamp(xmlNullValue,
1098+
nullValue = static_cast<T>(std::clamp(static_cast<int64_t>(xmlNullValue),
10991099
static_cast<int64_t>((std::numeric_limits<T>::min)()), static_cast<int64_t>((std::numeric_limits<T>::max)())));
11001100
}
11011101
else {

src/common/AbstractObject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ namespace COMMON_NS
842842
gsoap_resqml2_0_1::resqml20__IntegerConstantArray const* constantArray = static_cast<gsoap_resqml2_0_1::resqml20__IntegerConstantArray const*>(arrayInput);
843843
T value;
844844
if constexpr (std::is_signed_v<T>) {
845-
value = static_cast<T>(std::clamp(constantArray->Value,
845+
value = static_cast<T>(std::clamp(static_cast<int64_t>(constantArray->Value),
846846
static_cast<int64_t>((std::numeric_limits<T>::min)()), static_cast<int64_t>((std::numeric_limits<T>::max)())));
847847
}
848848
else {

0 commit comments

Comments
 (0)