[PATCH] don't store temporary value returned from c_str()

Vladimir Marek Vladimir.Marek at Oracle.COM
Sat Apr 27 08:59:22 PDT 2013


> > const char*
> > string::c_str(void) {
> > 	char buf[100];
> >
> > 	strcpy (buf, this->internal_representation);
> > 	return buf;
> > }
> 
> Isn't that undefined behavior, returning a pointer to a (non-static)
> local variable?

Right, I was trying to bring up an example and this one is not very
good. Maybe something like that:


const char*
c_str() {
	vector<char> X(my_string);
	return &X[0];
};

char buf[];
strcpy(buf, c_str());

X destructor is called after strcpy is done.

char buf[];
char *tmp=c_str();
strcpy(buf, tmp);

X destructor is called before strcpy.

At least this is how I understand it.

-- 
	Vlad


More information about the notmuch mailing list