Bug 23698

Summary: STL map<string, string> destructor causes problem
Product: [Retired] Red Hat Linux Reporter: Need Real Name <slav>
Component: libstdc++Assignee: Jakub Jelinek <jakub>
Status: CLOSED NOTABUG QA Contact:
Severity: medium Docs Contact:
Priority: high    
Version: 7.0   
Target Milestone: ---   
Target Release: ---   
Hardware: i386   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2004-10-04 16:17:47 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Need Real Name 2001-01-10 09:10:13 UTC
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-12 00:26:28 UTC
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 15:42:33 UTC
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 17:42:24 UTC
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 16:17:47 UTC
> 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