[Boost-bugs] [Boost C++ Libraries] #12893: Add debug check for boost::container::string::c_str method

Subject: [Boost-bugs] [Boost C++ Libraries] #12893: Add debug check for boost::container::string::c_str method
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2017-03-10 13:19:51


#12893: Add debug check for boost::container::string::c_str method
-----------------------------------+------------------------
 Reporter: Lev Sch <zorechfan@…> | Owner: igaztanaga
     Type: Feature Requests | Status: new
Milestone: To Be Determined | Component: container
  Version: Boost 1.63.0 | Severity: Problem
 Keywords: |
-----------------------------------+------------------------
 If boost::container::string was destroyed then result of c_str method must
 be invalidated. It will help to catch errors in a application.

 Notes:
 1. std::string on MS Visual Studio 2015 is already does this check.
 2. some patterns are here http://stackoverflow.com/a/127404


 Example:

 {{{
 #include <cassert>
 #include <string>
 #include <boost/container/string.hpp>

 bool isAbcd(const char* ch)
 {
   if (
     ch[0] == 'a' &&
     ch[1] == 'b' &&
     ch[2] == 'c' &&
     ch[3] == 'd' &&
     ch[4] == '\0'
   ) {
     return true;
   }
   return false;
 }

 void exampleNewDelete()
 {
   char* ch = new char[5];
   ch[0] = 'a';
   ch[1] = 'b';
   ch[2] = 'c';
   ch[3] = 'd';
   ch[4] = '\0';

   assert(isAbcd(ch));
   delete[] ch;
   const bool ok = isAbcd(ch); // if app crash then test OK
   if ( ok ) {
     assert(false); // test failed
   }
   // test OK
 }

 void exampleStdString()
 {
   char* ch;
   ch = (char*)std::string("abcd").c_str();
   const bool ok = isAbcd(ch); // if app crash then test OK
   if ( ok ) {
     assert(false); // test failed
   }
   // test OK
 }

 void exampleBoostString()
 {
   char* ch;
   ch = (char*)boost::container::string("abcd").c_str();
   const bool ok = isAbcd(ch); // if app crash then test OK
   if ( ok ) {
     assert(false); // test failed
   }
   // test OK
 }

 int main()
 {
   //exampleNewDelete();
   //exampleStdString();
   //exampleBoostString();
   return 0;
 }


 }}}

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/12893>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-03-10 13:23:45 UTC