Bug 23698 - STL map<string, string> destructor causes problem
STL map<string, string> destructor causes problem
Status: CLOSED NOTABUG
Product: Red Hat Linux
Classification: Retired
Component: libstdc++ (Show other bugs)
7.0
i386 Linux
high Severity medium
: ---
: ---
Assigned To: Jakub Jelinek
:
Depends On:
Blocks:
  Show dependency treegraph
 
Reported: 2001-01-10 04:10 EST by Need Real Name
Modified: 2007-03-26 23:39 EDT (History)
0 users

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Environment:
Last Closed: 2004-10-04 12:17:47 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---


Attachments (Terms of Use)

  None (edit)
Description Need Real Name 2001-01-10 04:10:13 EST
If standard template library map<string,string> is used and destoyed next
instance will have garbled strings. It seems that map::erase() cause the 
same problem. Same behavior on Linux 6 & 7.
I have example which works on Sun 2.8 and NT 4.
Comment 1 Need Real Name 2001-01-11 19:26:28 EST
string in string object is not terminated so conversion
from STL string to C string should be:

string a;
char  b[100];

strncpy( b, a.data(), a.size() );
b[a.size()] = 0;
Comment 2 Benjamin Kosnik 2004-10-01 11:42:33 EDT
std::string is terminated.

No code to reproduce this error, cannot tell if user error or
implementation error.

Comment 3 Need Real Name 2004-10-01 13:42:24 EDT
C String is converted to the string and stored into map, when 
retrieved value.data() returns unterminated string. See work around 
with strncpy below.


class gwReference
{

public:
	
	gwReference();
	~gwReference();

	char * Get( char * UserOrderId, char * OrderNumber );
	int Put( char * UserOrderId, char * OrderNumber );

private:

	map<string, string>  m_RefTable;
};
int gwReference::Put( char * UserOrderId, char * OrderNumber )
{
	string key;
	string value;

	key   = UserOrderId;
	value = OrderNumber;
	m_RefTable[key] = value;

	return 0;
} 

har * gwReference::Get( char * UserOrderId, char * OrderNumber )
{
	string  key;
	string  value;

	key = UserOrderId;

	map<string, string>::iterator myIterator;
	
	myIterator = m_RefTable.find(key);

	if( myIterator == m_RefTable.end() )
	{
		// not found
		OrderNumber[0] = 0;
	}
	else
	{
		value = (*myIterator).second;
//		strcpy( OrderNumber, value.data());

		int size = value.size();
		strncpy( OrderNumber, value.data(), size );
		OrderNumber[size] = 0;
	}

	return OrderNumber;
}
Comment 4 Benjamin Kosnik 2004-10-04 12:17:47 EDT
> C String is converted to the string and stored into map, when 
> retrieved value.data() returns unterminated string.

If you want to extract a null-terminated char* from std::string, you
have to use std::string.c_str() not std::string.data().

This isn't a bug, it's standard-required behavior.

-benjamin

Note You need to log in before you can comment on or make changes to this bug.