Skip to content
Merged
4 changes: 2 additions & 2 deletions core/base/inc/TString.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,9 @@ friend std::strong_ordering operator<=>(const TString &s1, const TString &s2) {
char *GetPointer() { return IsLong() ? GetLongPointer() : GetShortPointer(); }
const char *GetPointer() const { return IsLong() ? GetLongPointer() : GetShortPointer(); }
#ifdef R__BYTESWAP
static Ssiz_t MaxSize() { return kMaxInt - 1; }
static Ssiz_t MaxSize() { return kMaxInt - 1 - (kAlignment - 1); }
#else
static Ssiz_t MaxSize() { return (kMaxInt >> 1) - 1; }
static Ssiz_t MaxSize() { return (kMaxInt >> 1) - 1 - (kAlignment - 1); }
#endif
void UnLink() const { if (IsLong()) delete [] fRep.fLong.fData; }
void Zero() {
Expand Down
12 changes: 10 additions & 2 deletions core/base/src/TString.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ strings (<15 on 64-bit and <11 on 32-bit) are contained in the
TString internal data structure without the need for mallocing the
required space.

\note TString can store a maximum of MaxSize()=2147483631 characters.
Trying to allocate larger buffers might throw std::bad_alloc or raise
Fatal errors or lead to undefined behavior. Likewise, there is no safety
check if you pass a Long64_t to the class functions, they will be silently
rounded to Int_t and lead to an integer overflow (negative value).
For future designs, consider using std::string instead, which has a larger
maximum size.

Substring operations are provided by the TSubString class, which
holds a reference to the original string and its data, along with
the offset and length of the substring. To retrieve the substring
Expand Down Expand Up @@ -1220,11 +1228,11 @@ void TString::AssertElement(Ssiz_t i) const
Ssiz_t TString::AdjustCapacity(Ssiz_t oldCap, Ssiz_t newCap)
{
Ssiz_t ms = MaxSize();
if (newCap > ms - 1) {
if (newCap > ms) {
Fatal("TString::AdjustCapacity", "capacity too large (%d, max = %d)",
newCap, ms);
}
Ssiz_t cap = oldCap < ms / 2 - kAlignment ?
Ssiz_t cap = oldCap < ms / 2 ?
Recommend(std::max(newCap, 2 * oldCap)) : ms - 1;
return cap;
}
Expand Down
Loading