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

memory bug passing arrays to C #27

Closed
JeffBezanson opened this issue May 4, 2015 · 1 comment
Closed

memory bug passing arrays to C #27

JeffBezanson opened this issue May 4, 2015 · 1 comment

Comments

@JeffBezanson
Copy link
Collaborator

For example in _7_16_2_Relative_Exponential_Functions.jl:

    result = convert(Ptr{gsl_sf_result}, Array(gsl_sf_result, 1))

This creates an array and immediately drops any references to it, leading to a serious memory bug (use after free). Fortunately direct conversion to Ptr has been disallowed in Julia 0.4, so this line raises
an error now.

There is seldom any need to manually convert to Ptr. ccall handles this for you; just pass the array and index it to read the result. Actually now a Ref should be used instead of an array, but an array still works.

jiahao added a commit that referenced this issue Oct 3, 2015
Ref: #27

The substitutions applied are:

    sed -i bak 's/result = convert(Ptr{gsl_sf_result}, Array(gsl_sf_result, 1))/result = Ref{gsl_sf_result}()/g' *.jl
    sed -i bak 's/return unsafe_load(result)/return result[]/g' *.jl
    sed -i bak 's/result_re = convert(Ptr{gsl_sf_result}, Array(gsl_sf_result, 1))/result_re = Ref{gsl_sf_result}()/g' *.jl
    sed -i bak 's/result_im = convert(Ptr{gsl_sf_result}, Array(gsl_sf_result, 1))/result_im = Ref{gsl_sf_result}()/g' *.jl
    sed -i bak 's/return unsafe_load(result_re), unsafe_load(result_im)/return result_re[], result_im/g' *.jl
jiahao added a commit that referenced this issue Oct 3, 2015
jiahao added a commit that referenced this issue Oct 3, 2015
- bytestring no longer requires explicit Ptr conversion
- ccall now understands cstring

Ref #27
jiahao added a commit that referenced this issue Oct 3, 2015
@jiahao
Copy link
Collaborator

jiahao commented Oct 3, 2015

I've done what I can to fix this issue on master. Will tag a new release.

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

No branches or pull requests

2 participants