Skip to content

Commit

Permalink
fix #581, report dangerous indexing (#582)
Browse files Browse the repository at this point in the history
  • Loading branch information
aviatesk authored Dec 12, 2023
1 parent aec0e21 commit 8a69a9a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/analyzers/jetanalyzer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,14 @@ function report_serious_exception!(analyzer::JETAnalyzer, sv::InferenceState, ar
return true
end
end
elseif widenconst(a) <: ArgumentError
# promote `ArgumentError` thrown by `to_index` method
# so that we get reports from dangerous indexing (aviatesk/JET.jl#581)
def = sv.linfo.def
if isa(def, Method) && def.name === :to_index
add_new_report!(analyzer, sv.result, SeriousExceptionReport(sv, ArgumentError("invalid index"), get_lin((sv, get_currpc(sv)))))
return true
end
end
end
return false
Expand Down
15 changes: 15 additions & 0 deletions test/analyzers/test_jetanalyzer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,21 @@ end
end
end

func_invalid_index(xs, i) = xs[i]
func_invalid_index(xs::Vector{Int}) = xs[findfirst(>(0), xs)]
@testset "report invalid index" begin
let result = report_call(func_invalid_index, (Vector{Int},Nothing))
@test count(get_reports_with_test(result)) do r
r isa SeriousExceptionReport
end == 1
end
let result = report_call(func_invalid_index, (Vector{Int},))
@test count(get_reports_with_test(result)) do r
r isa SeriousExceptionReport
end == 1
end
end

@testset "DivideError" begin
let result = report_call() do
div(1, 0)
Expand Down

0 comments on commit 8a69a9a

Please sign in to comment.