@@ -98,7 +98,7 @@ inline string<Capacity>::string(const char (&other)[N]) noexcept
98
98
99
99
template <uint64_t Capacity>
100
100
inline string<Capacity>::string(TruncateToCapacity_t, const char * const other) noexcept
101
- : string(TruncateToCapacity, other, strnlen(other, Capacity + 1U ))
101
+ : string(TruncateToCapacity, other, strnlen(other, Capacity))
102
102
{
103
103
}
104
104
@@ -144,7 +144,7 @@ inline string<Capacity>& string<Capacity>::operator=(const char (&rhs)[N]) noexc
144
144
return *this ;
145
145
}
146
146
147
- m_rawstringSize = strnlen (rhs, Capacity );
147
+ m_rawstringSize = std::min (Capacity, static_cast < uint64_t >( strnlen (rhs, N)) );
148
148
std::memcpy (&(m_rawstring[0 ]), rhs, m_rawstringSize);
149
149
m_rawstring[m_rawstringSize] = ' \0 ' ;
150
150
@@ -466,14 +466,18 @@ inline typename std::enable_if<internal::IsCharArray<T>::value || internal::IsCx
466
466
string<Capacity>::unsafe_append(const T& t) noexcept
467
467
{
468
468
uint64_t tSize = internal::GetSize<T>::call (t);
469
- if (Capacity < (m_rawstringSize + tSize))
469
+ const char * tData = internal::GetData<T>::call (t);
470
+ uint64_t clampedTSize = std::min (Capacity - m_rawstringSize, tSize);
471
+
472
+ if (tSize > clampedTSize)
470
473
{
471
474
std::cerr << " Appending failed because the sum of sizes exceeds this' capacity." << std::endl;
472
475
return false ;
473
476
}
474
- std::memcpy (&(m_rawstring[0 ]) + m_rawstringSize, internal::GetData<T>::call (t), tSize);
475
- m_rawstring[m_rawstringSize + tSize] = ' \0 ' ;
476
- m_rawstringSize += tSize;
477
+
478
+ std::memcpy (&(m_rawstring[m_rawstringSize]), tData, clampedTSize);
479
+ m_rawstringSize += clampedTSize;
480
+ m_rawstring[m_rawstringSize] = ' \0 ' ;
477
481
return true ;
478
482
}
479
483
@@ -485,20 +489,17 @@ inline
485
489
{
486
490
uint64_t tSize = internal::GetSize<T>::call (t);
487
491
const char * tData = internal::GetData<T>::call (t);
488
- if (Capacity < (m_rawstringSize + tSize))
492
+ uint64_t clampedTSize = std::min (Capacity - m_rawstringSize, tSize);
493
+
494
+ std::memcpy (&(m_rawstring[m_rawstringSize]), tData, clampedTSize);
495
+ if (tSize > clampedTSize)
489
496
{
490
497
std::cerr << " The last " << tSize - Capacity + m_rawstringSize << " characters of " << tData
491
498
<< " are truncated, because the length is larger than the capacity." << std::endl;
492
- std::memcpy (&(m_rawstring[0 ]) + m_rawstringSize, tData, Capacity - m_rawstringSize);
493
- m_rawstring[Capacity] = ' \0 ' ;
494
- m_rawstringSize = Capacity;
495
- }
496
- else
497
- {
498
- std::memcpy (&(m_rawstring[0 ]) + m_rawstringSize, tData, tSize);
499
- m_rawstring[m_rawstringSize + tSize] = ' \0 ' ;
500
- m_rawstringSize += tSize;
501
499
}
500
+
501
+ m_rawstringSize += clampedTSize;
502
+ m_rawstring[m_rawstringSize] = ' \0 ' ;
502
503
return *this ;
503
504
}
504
505
0 commit comments