Bug 23698 - STL map<string, string> destructor causes problem
Summary: STL map<string, string> destructor causes problem
Status: CLOSED NOTABUG
Alias: None
Product: Red Hat Linux
Classification: Retired
Component: libstdc++   
(Show other bugs)
Version: 7.0
Hardware: i386 Linux
high
medium
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact:
URL:
Whiteboard:
Keywords:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2001-01-10 09:10 UTC by Need Real Name
Modified: 2007-03-27 03:39 UTC (History)
0 users

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: ---


Attachments (Terms of Use)

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


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