Opened 6 years ago

#12893 new Feature Requests

Add debug check for boost::container::string::c_str method

Reported by: Lev Sch <zorechfan@…> Owned by: Ion Gaztañaga
Milestone: To Be Determined Component: container
Version: Boost 1.63.0 Severity: Problem
Keywords: Cc:

Description

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;
}


Change History (0)

Note: See TracTickets for help on using tickets.