Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix use-after-free bugs #219

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Conversation

akalin
Copy link

@akalin akalin commented Aug 24, 2014

The pattern:

const char *foo = function_returning_string().c_str();

is dangerous because the pointer lives only as long as the temporary string returned by the function (i.e., the end of the statement), so using 'foo' leads to a use-after-free.

Fixed that by doing:

const string &foo = function_returning_string();
...
function_taking_pointer(foo.c_str());

instead. This is safe because binding a temporary to a const reference makes that temporary live for the whole block.

Also removed some other unnecessary calls to c_str().

@akalin
Copy link
Author

akalin commented Aug 24, 2014

Ugh, this fix is buggy. Stay tuned...

@akalin akalin force-pushed the fix-string-bug branch 2 times, most recently from 98a60a7 to 58218d5 Compare August 25, 2014 00:04
@akalin
Copy link
Author

akalin commented Aug 25, 2014

Okay, this version should work. That's what I get for not testing before pushing...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant