aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThierry Crozat2010-06-19 15:35:21 +0000
committerThierry Crozat2010-06-19 15:35:21 +0000
commit721db3eccb82e27d1169a46b282975534ce106fe (patch)
tree80a0d1508f00e2bb35382274369f5d8716ee9369
parent32668b553fe69fc8cea5afc60d5f82abdc9a4fb8 (diff)
downloadscummvm-rg350-721db3eccb82e27d1169a46b282975534ce106fe.tar.gz
scummvm-rg350-721db3eccb82e27d1169a46b282975534ce106fe.tar.bz2
scummvm-rg350-721db3eccb82e27d1169a46b282975534ce106fe.zip
Fix an issue in String::ensureCapacity() when the string is shared. It could allocate two much memory as it was at least doubling the current capacity even when this one was sufficient.
It fixes a crash in GUI::Widget::cleanupHotkey() as the capacity of the string was doubled at each iteration once it was too long for the internal storage (only to add one character to the string). This ended up in a bad_alloc exception after a few iterations. svn-id: r50050
-rw-r--r--common/str.cpp6
1 files changed, 5 insertions, 1 deletions
diff --git a/common/str.cpp b/common/str.cpp
index 5e771c8b4d..50b9bdb879 100644
--- a/common/str.cpp
+++ b/common/str.cpp
@@ -151,7 +151,11 @@ 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+1));
+ // If the current capacity is sufficient we use the same capacity
+ if (new_size < curCapacity)
+ newCapacity = curCapacity;
+ else
+ newCapacity = MAX(curCapacity * 2, computeCapacity(new_size+1));
// Allocate new storage
newStorage = new char[newCapacity];