Skip to content

Commit

Permalink
Fix column length for erlang stdlib functions
Browse files Browse the repository at this point in the history
  • Loading branch information
NJichev committed May 1, 2024
1 parent 47b2000 commit 346953b
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 10 deletions.
9 changes: 5 additions & 4 deletions lib/credo/check/warning/lazy_logging.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ defmodule Credo.Check.Warning.LazyLogging do
issue_meta
)
when fun_name in @logger_functions do
issue = find_issue(fun_name, arguments, meta, issue_meta)
trigger = "Logger.#{fun_name}"
issue = find_issue(fun_name, arguments, meta, issue_meta, trigger)

{ast, add_issue_to_state(state, issue)}
end
Expand All @@ -62,7 +63,7 @@ defmodule Credo.Check.Warning.LazyLogging do
issue_meta
)
when fun_name in @logger_functions do
issue = find_issue(fun_name, arguments, meta, issue_meta)
issue = find_issue(fun_name, arguments, meta, issue_meta, fun_name)

{ast, add_issue_to_state(state, issue)}
end
Expand All @@ -89,12 +90,12 @@ defmodule Credo.Check.Warning.LazyLogging do
{module_contains_import?, [issue | issues]}
end

defp find_issue(fun_name, arguments, meta, issue_meta) do
defp find_issue(fun_name, arguments, meta, issue_meta, trigger) do
params = IssueMeta.params(issue_meta)
ignored_functions = Params.get(params, :ignore, __MODULE__)

unless Enum.member?(ignored_functions, fun_name) do
issue_for_call(arguments, meta, fun_name, issue_meta)
issue_for_call(arguments, meta, trigger, issue_meta)
end
end

Expand Down
6 changes: 6 additions & 0 deletions lib/credo/check/warning/leaky_environment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ defmodule Credo.Check.Warning.LeakyEnvironment do
Credo.Code.prewalk(source_file, &traverse(&1, &2, issue_meta))
end

@offset 2
defp traverse({{:., _, call}, meta, args} = ast, issues, issue_meta) do
case get_forbidden_call(call, args) do
nil ->
Expand All @@ -36,6 +37,11 @@ defmodule Credo.Check.Warning.LeakyEnvironment do
{ast, [issue_for(issue_meta, meta, trigger) | issues]}

trigger ->
[module, _function] = call
len = module |> Atom.to_string() |> String.length()
column = meta[:column] - len - @offset
meta = Keyword.put(meta, :column, column)

{ast, [issue_for(issue_meta, meta, trigger) | issues]}
end
end
Expand Down
12 changes: 9 additions & 3 deletions lib/credo/check/warning/unsafe_exec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ defmodule Credo.Check.Warning.UnsafeExec do
defp traverse({{:., _loc, call}, meta, args} = ast, issues, issue_meta) do
case get_forbidden_call(call, args) do
{bad, suggestion, trigger} ->
{ast, [issue_for(bad, suggestion, trigger, meta, issue_meta) | issues]}
[module, _function] = call
{ast, [issue_for(bad, suggestion, trigger, meta, module, issue_meta) | issues]}

nil ->
{ast, issues}
Expand Down Expand Up @@ -65,12 +66,17 @@ defmodule Credo.Check.Warning.UnsafeExec do
nil
end

defp issue_for(call, suggestion, trigger, meta, issue_meta) do
# offset 2 characters for the dot call and the atom syntax
@offset 2
defp issue_for(call, suggestion, trigger, meta, module, issue_meta) do
len = module |> Atom.to_string() |> String.length()
column = meta[:column] - len - @offset

format_issue(issue_meta,
message: "Prefer #{suggestion} over #{call} to prevent command injection.",
trigger: trigger,
line_no: meta[:line],
column: meta[:column]
column: column
)
end
end
3 changes: 3 additions & 0 deletions test/credo/check/warning/lazy_logging_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,15 @@ defmodule Credo.Check.Warning.LazyLoggingTest do
|> to_source_file
|> run_check(@described_check)
|> assert_issues(fn [three, two, one] ->
assert one.trigger == "Logger.debug"
assert one.line_no == 5
assert one.column == 5

assert two.trigger == "Logger.debug"
assert two.line_no == 6
assert two.column == 5

assert three.trigger == "Logger.debug"
assert three.line_no == 7
assert three.column == 5
end)
Expand Down
2 changes: 1 addition & 1 deletion test/credo/check/warning/leaky_environment_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ defmodule Credo.Check.Warning.LeakyEnvironmentTest do
|> run_check(@described_check)
|> assert_issue(fn issue ->
assert issue.line_no == 3
assert issue.column == 13
assert issue.column == 5
assert issue.trigger == ":erlang.open_port"
end)
end
Expand Down
5 changes: 3 additions & 2 deletions test/credo/check/warning/unsafe_exec_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ defmodule Credo.Check.Warning.UnsafeExecTest do
|> run_check(@described_check)
|> assert_issue(fn issue ->
assert issue.line_no == 3
assert issue.column == 9
assert issue.column == 5
end)
end

Expand All @@ -60,6 +60,7 @@ defmodule Credo.Check.Warning.UnsafeExecTest do
|> run_check(@described_check)
|> assert_issue(fn issue ->
assert issue.line_no == 3
assert issue.column == 5
assert issue.trigger == ":os.cmd"
end)
end
Expand All @@ -76,7 +77,7 @@ defmodule Credo.Check.Warning.UnsafeExecTest do
|> run_check(@described_check)
|> assert_issue(fn issue ->
assert issue.line_no == 3
assert issue.column == 13
assert issue.column == 5
assert issue.trigger == ":erlang.open_port"
end)
end
Expand Down

0 comments on commit 346953b

Please sign in to comment.