From eb5e5c10d3209e4a6c8af46733e66b3c83948f2a Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Thu, 31 Aug 2017 11:20:50 +0000 Subject: [PATCH] checked the no-buffer condition in some string functions --- qse/include/qse/cmn/StrBase.hpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/qse/include/qse/cmn/StrBase.hpp b/qse/include/qse/cmn/StrBase.hpp index 3128b77e..2916c823 100644 --- a/qse/include/qse/cmn/StrBase.hpp +++ b/qse/include/qse/cmn/StrBase.hpp @@ -186,9 +186,11 @@ public: if (capacity <= -1) { // this is a special constructor to instanatiate a string with no buffer. - // it is intended to be followed by truncate() for actual buffer allocation. - // all other functions except the destructor and the truncation() function - // are not aware of this special condition. + // it is designed to be followed by truncate() immediately for actual buffer + // allocation. all other functions except the copy/move constructors, some + // assignment operators, the destructor and the truncation() function are + // not aware of this special condition. if you call other functions without + // truncate(), your program will end with a crash. // // String x(0); // try { x.truncate(10); } catch (...) { return -1; } @@ -270,7 +272,9 @@ public: StrBase (const SelfType& str): Mmged(str) { this->_item = str._item; - this->ref_item (); + + // check if this->_item is null or not to cater for a no buffer string + if (this->_item) this->ref_item (); } #if defined(QSE_CPP_ENABLE_CPP11_MOVE) @@ -291,7 +295,8 @@ public: { if (this->_item != str._item) { - this->deref_item (); + // check if this->_item is null or not to cater for a no buffer string + if (this->_item) this->deref_item (); // the data to be reference could be allocated using the // memory manager of str. and it may be freed or resized by @@ -309,7 +314,8 @@ public: { if (this->_item != str._item) { - this->deref_item (); + // check if this->_item is null or not to cater for a no buffer string + if (this->_item) this->deref_item (); // the data to be reference could be allocated using the // memory manager of str. and it may be freed or resized by