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 issue when using intermediate string returns #2048

Merged
merged 1 commit into from
Apr 24, 2016

Conversation

mgreter
Copy link
Contributor

@mgreter mgreter commented Apr 24, 2016

Fixes #2046

In MSVC the following can lead to segfault:
sass_copy_c_string(stream.str().c_str());
Reason is that the string returned by str() is disposed before
sass_copy_c_string is invoked. The string is actually a stack
object, so indeed nobody is holding on to it. So it seems
perfectly fair to release it right away. So the const char*
by c_str will point to invalid memory. I'm not sure if this is
the behavior for all compiler, but I'm pretty sure we would
have gotten more issues reported if that would be the case.
Wrapping it in a function seems the cleanest approach as the
function must hold on to the stack variable until it's done.

Note. did a quick search for str().c_str() and sanitized one other case.
Seems we do not use this to often (good!). But worth to keep on our mind!
My gut feeling tells me that this is basically another incarnation of #1462

In MSVC the following can lead to segfault:
sass_copy_c_string(stream.str().c_str());
Reason is that the string returned by str() is disposed before
sass_copy_c_string is invoked. The string is actually a stack
object, so indeed nobody is holding on to it. So it seems
perfectly fair to release it right away. So the const char*
by c_str will point to invalid memory. I'm not sure if this is
the behavior for all compiler, but I'm pretty sure we would
have gotten more issues reported if that would be the case.
Wrapping it in a functions seems the cleanest approach as the
function must hold on to the stack variable until it's done.
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.3%) to 78.943% when pulling 798aff1 on mgreter:bugfix/issue_2046 into 8160ba9 on sass:master.

@mgreter mgreter merged commit fb3e6ef into sass:master Apr 24, 2016
@xzyfer xzyfer modified the milestones: 3.3.7, 3.4 Oct 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants