Skip to content

Commit 3274132

Browse files
committed
Check for invalid bytes in issue message
Refs #1120
1 parent d484d0a commit 3274132

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

lib/credo/check.ex

+15-1
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ defmodule Credo.Check do
705705
column = opts[:column]
706706
severity = opts[:severity] || Severity.default_value()
707707
trigger = opts[:trigger]
708+
message = to_string(opts[:message])
708709

709710
trigger =
710711
if trigger == Issue.no_trigger() do
@@ -713,12 +714,23 @@ defmodule Credo.Check do
713714
to_string(trigger)
714715
end
715716

717+
message =
718+
if String.valid?(message) do
719+
message
720+
else
721+
IO.warn(
722+
"#{check_name(check)} creates an Issue with a `:message` containing invalid bytes: #{inspect(message)}"
723+
)
724+
725+
"(see warning) #{inspect(message)}"
726+
end
727+
716728
%Issue{
717729
check: check,
718730
category: issue_category,
719731
priority: priority,
720732
filename: source_file.filename,
721-
message: opts[:message],
733+
message: message,
722734
trigger: trigger,
723735
line_no: line_no,
724736
column: column,
@@ -764,6 +776,8 @@ defmodule Credo.Check do
764776
end
765777
end
766778

779+
defp check_name(module), do: Credo.Code.Name.full(module)
780+
767781
# Returns the scope for the given line as a tuple consisting of the call to
768782
# define the scope (`:defmodule`, `:def`, `:defp` or `:defmacro`) and the
769783
# name of the scope.

lib/credo/plugin.ex

+3-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,9 @@ defmodule Credo.Plugin do
344344
import Credo.Plugin
345345
346346
def init(exec) do
347-
register_cli_switch(exec, :kastle, :string, :X, fn(switch_value) ->
347+
exec
348+
|> register_command("demo", CredoDemoPlugin.DemoCommand)
349+
|> register_cli_switch(:kastle, :string, :X, fn(switch_value) ->
348350
{:castle, String.upcase(switch_value)}
349351
end)
350352
end

test/credo/check/readability/string_sigils_test.exs

+2-2
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ defmodule Credo.Check.Readability.StringSigilsTest do
170170
end
171171

172172
test "doesn't crash on #729" do
173-
log =
173+
stderr_output =
174174
capture_io(:stderr, fn ->
175175
~S"""
176176
defmodule CredoInterpolationError do
@@ -187,6 +187,6 @@ defmodule Credo.Check.Readability.StringSigilsTest do
187187
|> refute_issues()
188188
end)
189189

190-
assert log == ""
190+
assert stderr_output == ""
191191
end
192192
end

test/credo/check_test.exs

+25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
defmodule Credo.CheckTest do
22
use Credo.Test.Case
33

4+
import ExUnit.CaptureIO
5+
46
alias Credo.Check
57

68
@generated_lines 1000
@@ -91,4 +93,27 @@ defmodule Credo.CheckTest do
9193
assert issue.severity == 11
9294
end)
9395
end
96+
97+
defmodule IssueInvalidMessageTestCheck do
98+
use Credo.Check
99+
100+
@message <<70, 111, 117, 110, 100, 32, 109, 105, 115, 115, 112, 101, 108, 108, 101, 100, 32,
101+
119, 111, 114, 100, 32, 96, 103, 97, 114, 114, 121, 226, 96, 46>>
102+
103+
def run(%SourceFile{} = source_file, params \\ []) do
104+
IssueMeta.for(source_file, params) |> format_issue(message: @message) |> List.wrap()
105+
end
106+
end
107+
108+
test "it should handle an invalid message" do
109+
stderr_output =
110+
capture_io(:stderr, fn ->
111+
"# we do not need code, as the check is creating an issue in any case"
112+
|> to_source_file
113+
|> run_check(IssueInvalidMessageTestCheck)
114+
end)
115+
116+
assert stderr_output != ""
117+
assert stderr_output =~ "containing invalid bytes"
118+
end
94119
end

0 commit comments

Comments
 (0)