From 721db3eccb82e27d1169a46b282975534ce106fe Mon Sep 17 00:00:00 2001 From: Thierry Crozat Date: Sat, 19 Jun 2010 15:35:21 +0000 Subject: 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 --- common/str.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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]; -- cgit v1.2.3