Skip to content

Commit

Permalink
Check for invalid bytes in issue message
Browse files Browse the repository at this point in the history
Refs #1120
  • Loading branch information
rrrene committed Dec 24, 2024
1 parent d484d0a commit 3274132
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
16 changes: 15 additions & 1 deletion lib/credo/check.ex
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ defmodule Credo.Check do
column = opts[:column]
severity = opts[:severity] || Severity.default_value()
trigger = opts[:trigger]
message = to_string(opts[:message])

trigger =
if trigger == Issue.no_trigger() do
Expand All @@ -713,12 +714,23 @@ defmodule Credo.Check do
to_string(trigger)
end

message =
if String.valid?(message) do
message
else
IO.warn(
"#{check_name(check)} creates an Issue with a `:message` containing invalid bytes: #{inspect(message)}"
)

"(see warning) #{inspect(message)}"
end

%Issue{
check: check,
category: issue_category,
priority: priority,
filename: source_file.filename,
message: opts[:message],
message: message,
trigger: trigger,
line_no: line_no,
column: column,
Expand Down Expand Up @@ -764,6 +776,8 @@ defmodule Credo.Check do
end
end

defp check_name(module), do: Credo.Code.Name.full(module)

# Returns the scope for the given line as a tuple consisting of the call to
# define the scope (`:defmodule`, `:def`, `:defp` or `:defmacro`) and the
# name of the scope.
Expand Down
4 changes: 3 additions & 1 deletion lib/credo/plugin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,9 @@ defmodule Credo.Plugin do
import Credo.Plugin
def init(exec) do
register_cli_switch(exec, :kastle, :string, :X, fn(switch_value) ->
exec
|> register_command("demo", CredoDemoPlugin.DemoCommand)
|> register_cli_switch(:kastle, :string, :X, fn(switch_value) ->
{:castle, String.upcase(switch_value)}
end)
end
Expand Down
4 changes: 2 additions & 2 deletions test/credo/check/readability/string_sigils_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ defmodule Credo.Check.Readability.StringSigilsTest do
end

test "doesn't crash on #729" do
log =
stderr_output =
capture_io(:stderr, fn ->
~S"""
defmodule CredoInterpolationError do
Expand All @@ -187,6 +187,6 @@ defmodule Credo.Check.Readability.StringSigilsTest do
|> refute_issues()
end)

assert log == ""
assert stderr_output == ""
end
end
25 changes: 25 additions & 0 deletions test/credo/check_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule Credo.CheckTest do
use Credo.Test.Case

import ExUnit.CaptureIO

alias Credo.Check

@generated_lines 1000
Expand Down Expand Up @@ -91,4 +93,27 @@ defmodule Credo.CheckTest do
assert issue.severity == 11
end)
end

defmodule IssueInvalidMessageTestCheck do
use Credo.Check

@message <<70, 111, 117, 110, 100, 32, 109, 105, 115, 115, 112, 101, 108, 108, 101, 100, 32,
119, 111, 114, 100, 32, 96, 103, 97, 114, 114, 121, 226, 96, 46>>

def run(%SourceFile{} = source_file, params \\ []) do
IssueMeta.for(source_file, params) |> format_issue(message: @message) |> List.wrap()
end
end

test "it should handle an invalid message" do
stderr_output =
capture_io(:stderr, fn ->
"# we do not need code, as the check is creating an issue in any case"
|> to_source_file
|> run_check(IssueInvalidMessageTestCheck)
end)

assert stderr_output != ""
assert stderr_output =~ "containing invalid bytes"
end
end

0 comments on commit 3274132

Please sign in to comment.