Skip to content

Commit

Permalink
Merge pull request #797 from LSSTDESC/double_free_bug
Browse files Browse the repository at this point in the history
Fix double-free bug
  • Loading branch information
joezuntz committed Jul 6, 2020
2 parents c1464c6 + 0c3d5d4 commit a9c2314
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
14 changes: 9 additions & 5 deletions pyccl/correlation.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,15 @@ def correlation(cosmo, ell, C_ell, theta, type='NN', corr_type=None,
scalar = True
theta = np.array([theta, ])

# Call correlation function
wth, status = lib.correlation_vec(cosmo, ell, C_ell, theta,
correlation_types[type],
correlation_methods[method],
len(theta), status)
if np.all(np.array(C_ell) == 0):
# short-cut and also avoid integration errors
wth = np.zeros_like(theta)
else:
# Call correlation function
wth, status = lib.correlation_vec(cosmo, ell, C_ell, theta,
correlation_types[type],
correlation_methods[method],
len(theta), status)
check(status, cosmo_in)
if scalar:
return wth[0]
Expand Down
24 changes: 24 additions & 0 deletions pyccl/tests/test_correlation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
import pyccl as ccl
import pytest
from timeit import default_timer

COSMO = ccl.Cosmology(
Omega_c=0.27, Omega_b=0.045, h=0.67, sigma8=0.8, n_s=0.96,
Expand Down Expand Up @@ -129,3 +130,26 @@ def test_correlation_raises():
ccl.correlation(COSMO, [1], [1e-3], [1], type='blah')
with pytest.raises(ValueError):
ccl.correlation(COSMO, [1], [1e-3], [1], corr_type='blah')


def test_correlation_zero():
ell = np.arange(2, 100000)
C_ell = np.zeros(ell.size)
theta = np.logspace(0, 2, 10000)
t0 = default_timer()
corr = ccl.correlation(COSMO, ell, C_ell, theta)
t1 = default_timer()
# if the short-cut has worked this should take
# less than 1 second at the absolute outside
assert t1 - t0 < 1.0
assert (corr == np.zeros(theta.size)).all()


def test_correlation_zero_ends():
# This should give an error instead of crashing
ell = np.arange(2, 1001)
C_ell = np.zeros(ell.size)
C_ell[500] = 1.0
theta = np.logspace(0, 2, 20)
with pytest.raises(ccl.CCLError):
ccl.correlation(COSMO, ell, C_ell, theta)
10 changes: 10 additions & 0 deletions src/ccl_correlation.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ static void ccl_tracer_corr_fftlog(ccl_cosmology *cosmo,
ccl_f1d_extrap_const,
ccl_f1d_extrap_logx_logy,
status);
if (*status) {
free(l_arr);
free(cl_arr);
ccl_cosmology_set_status_message(cosmo,
"ccl_correlation.c: ccl_f1d_t_new "
"failed to create spline\n");
if (cl_spl) ccl_f1d_t_free(cl_spl);
return;
}

if(cl_spl==NULL) {
free(l_arr);
free(cl_arr);
Expand Down
8 changes: 8 additions & 0 deletions src/ccl_f1d.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ ccl_f1d_t *ccl_f1d_t_new(int n,double *x,double *y,double y0,double yf,
}
else // No extrapolation
spl->der_hi = 0;

// If one of the derivatives could not be calculated
// then return NULL.
if (*status){
ccl_f1d_t_free(spl);
spl = NULL;
}

return spl;
}

Expand Down

0 comments on commit a9c2314

Please sign in to comment.