Skip to content

Commit 2de0c29

Browse files
Use zero instead of similar to avoid UB
wip
1 parent c7faccb commit 2de0c29

File tree

6 files changed

+16
-16
lines changed

6 files changed

+16
-16
lines changed

src/nl_solvers/newtons_method.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ Base.@kwdef struct ForwardDiffJVP{S <: ForwardDiffStepSize, T} <: JacobianFreeJV
149149
step_adjustment::T = 1
150150
end
151151

152-
allocate_cache(::ForwardDiffJVP, x_prototype) = (; x2 = similar(x_prototype), f2 = similar(x_prototype))
152+
allocate_cache(::ForwardDiffJVP, x_prototype) = (; x2 = zero(x_prototype), f2 = zero(x_prototype))
153153

154154
function jvp!(alg::ForwardDiffJVP, cache, jΔx, Δx, x, f!, f, post_implicit!)
155155
(; default_step, step_adjustment) = alg
@@ -562,8 +562,8 @@ function allocate_cache(alg::NewtonsMethod, x_prototype, j_prototype = nothing)
562562
convergence_checker_cache = isnothing(convergence_checker) ? nothing :
563563
allocate_cache(convergence_checker, x_prototype),
564564
Δx = similar(x_prototype),
565-
f = similar(x_prototype),
566-
j = isnothing(j_prototype) ? nothing : similar(j_prototype),
565+
f = zero(x_prototype),
566+
j = isnothing(j_prototype) ? nothing : zero(j_prototype),
567567
)
568568
end
569569

src/solvers/imex_ssprk.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ function init_cache(prob::DiffEqBase.AbstractODEProblem, alg::IMEXAlgorithm{SSP}
1919
s = length(b_exp)
2020
inds = ntuple(i -> i, s)
2121
inds_T_imp = filter(i -> !all(iszero, a_imp[:, i]) || !iszero(b_imp[i]), inds)
22-
U = similar(u0)
23-
U_exp = similar(u0)
24-
T_lim = similar(u0)
25-
T_exp = similar(u0)
26-
U_lim = similar(u0)
27-
T_imp = SparseContainer(map(i -> similar(u0), collect(1:length(inds_T_imp))), inds_T_imp)
28-
temp = similar(u0)
22+
U = zero(u0)
23+
U_exp = zero(u0)
24+
T_lim = zero(u0)
25+
T_exp = zero(u0)
26+
U_lim = zero(u0)
27+
T_imp = SparseContainer(map(i -> zero(u0), collect(1:length(inds_T_imp))), inds_T_imp)
28+
temp = zero(u0)
2929
â_exp = SparseCoeffs(vcat(a_exp.coeffs, b_exp.coeffs'))
3030
β = SparseCoeffs(diag(â_exp, -1))
3131
for i in 1:length(β)

src/solvers/mis.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ function init_cache(
6363

6464
tab = tableau(alg, eltype(prob.u0))
6565
N = length(tab.d)
66-
ΔU = ntuple(n -> similar(prob.u0), N)
66+
ΔU = ntuple(n -> zero(prob.u0), N)
6767
# at time i, contains
6868
# ΔU[j] = U[j] - u j < i
6969
# ΔU[i] = U[i]
7070
# ΔU[N] = offset vec
71-
F = ntuple(n -> similar(prob.u0), N)
71+
F = ntuple(n -> zero(prob.u0), N)
7272
return MultirateInfinitesimalStepCache(ΔU, F, tab)
7373
end
7474

src/solvers/rosenbrock.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ function init_cache(prob::DiffEqBase.AbstractODEProblem, alg::RosenbrockAlgorith
9494
fU_exp = zero(prob.u0)
9595
fU_lim = zero(prob.u0)
9696
∂Y∂t = zero(prob.u0)
97-
k = ntuple(n -> similar(prob.u0), Nstages)
97+
k = ntuple(n -> zero(prob.u0), Nstages)
9898
if !isnothing(prob.f.T_imp!)
9999
W = prob.f.T_imp!.jac_prototype
100100
else

src/solvers/wickerskamarock.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ struct WickerSkamarockRungeKuttaCache{T <: WickerSkamarockRungeKuttaTableau, A}
2626
F::A
2727
end
2828
function init_cache(prob::DiffEqBase.ODEProblem, alg::WickerSkamarockRungeKutta; kwargs...)
29-
U = similar(prob.u0)
30-
F = similar(prob.u0)
29+
U = zero(prob.u0)
30+
F = zero(prob.u0)
3131
return WickerSkamarockRungeKuttaCache(tableau(alg, eltype(F)), U, F)
3232
end
3333

test/convergence_utils.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ DirectSolver(args...) = DirectSolver()
1212

1313
function (::DirectSolver)(x, A, b, matrix_updated; kwargs...)
1414
n = length(x)
15-
M = mapslices(y -> mul!(similar(y), A, y), Matrix{eltype(x)}(I, n, n), dims = 1)
15+
M = mapslices(y -> mul!(zero(y), A, y), Matrix{eltype(x)}(I, n, n), dims = 1)
1616
x .= M \ b
1717
end
1818

0 commit comments

Comments
 (0)