diff options
author | Max Horn | 2008-09-03 18:38:01 +0000 |
---|---|---|
committer | Max Horn | 2008-09-03 18:38:01 +0000 |
commit | 3cb4cfa06ff10ac1f5b708ae78e624c07b3eaa4e (patch) | |
tree | 1523a35dc34f377942ad40da8a87b880078e088f /common | |
parent | c7a0b52c179f9d0238213e2d5ce5bbda57d0739c (diff) | |
download | scummvm-rg350-3cb4cfa06ff10ac1f5b708ae78e624c07b3eaa4e.tar.gz scummvm-rg350-3cb4cfa06ff10ac1f5b708ae78e624c07b3eaa4e.tar.bz2 scummvm-rg350-3cb4cfa06ff10ac1f5b708ae78e624c07b3eaa4e.zip |
Fix nasty off-by-one errors
svn-id: r34317
Diffstat (limited to 'common')
-rw-r--r-- | common/str.cpp | 11 |
1 files changed, 4 insertions, 7 deletions
diff --git a/common/str.cpp b/common/str.cpp index f24780cd6e..f6a2df5b0e 100644 --- a/common/str.cpp +++ b/common/str.cpp @@ -35,10 +35,7 @@ const char *String::emptyString = ""; #endif static uint32 computeCapacity(uint32 len) { - // By default, for the capacity we use the nearest multiple of 32 - // that leaves at least 16 chars of extra space (in case the string - // grows a bit). - len += 16; + // By default, for the capacity we use the next multiple of 32 return ((len + 32 - 1) & ~0x1F); } @@ -70,7 +67,7 @@ void String::initWithCStr(const char *str, uint32 len) { if (len >= _builtinCapacity) { // Not enough internal storage, so allocate more - _extern._capacity = computeCapacity(len); + _extern._capacity = computeCapacity(len+1); _extern._refCount = 0; _str = (char *)malloc(_extern._capacity); assert(_str != 0); @@ -136,7 +133,7 @@ void String::ensureCapacity(uint32 new_size, bool keep_old) { // Special case: If there is enough space, and we do not share // the storage, then there is nothing to do. - if (!isShared && new_size <= curCapacity) + if (!isShared && new_size < curCapacity) return; if (isShared && new_size < _builtinCapacity) { @@ -147,7 +144,7 @@ void String::ensureCapacity(uint32 new_size, bool keep_old) { // We need to allocate storage on the heap! // Compute a suitable new capacity limit - newCapacity = MAX(curCapacity * 2, computeCapacity(new_size)); + newCapacity = MAX(curCapacity * 2, computeCapacity(new_size+1)); // Allocate new storage newStorage = (char *)malloc(newCapacity); |