spacetelescope/PyFITS — pull request #72 by mdboom (2014-06-26 00:58:33+00:00)
Title: Fix memory-access-after-free bug.

In the process of running valgrind over the test suite to see if it
would find anything related to the random crashes, I came across this.
In get_header_string, a header value is retrieved from the Python side,
and a `char *` to the character data is obtained and returned.
Unfortunately, when the Python string that owns that character data
is dereferenced at the end of that function, Python is free to
deallocate or move that character data around. When the `char *` is
later accessed by the calling function, it, in some circumstances, is
no longer valid.

The change here is to allocate character buffers on the stack of the
calling function and copy the character data into that buffer
immediately, so that Python can deallocate its original character buffer
with no problems.

Note: This is a backport of astropy/astropy#2663.