Skip to content

Commit 825fb73

Browse files
robi-wanjosevalim
authored andcommitted
Mix.Tasks.Profile.Eprof: Protect against division by zero (#6614)
Tests for this Mix task failed on Windows with `(ArithmeticError) bad argument in arithmetic expression`.
1 parent 9191e15 commit 825fb73

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

lib/mix/lib/mix/tasks/profile.eprof.ex

+6-3
Original file line numberDiff line numberDiff line change
@@ -219,20 +219,23 @@ defmodule Mix.Tasks.Profile.Eprof do
219219

220220
defp format_row({{module, function, arity}, {count, time}}, total_time) do
221221
mfa = Exception.format_mfa(module, function, arity)
222-
time_percentage = :erlang.float_to_binary(100 * time / total_time, [{:decimals, 2}])
223-
time_per_call = :erlang.float_to_binary(time / count, [{:decimals, 2}])
222+
time_percentage = :erlang.float_to_binary(100 * divide(time, total_time), [{:decimals, 2}])
223+
time_per_call = :erlang.float_to_binary(divide(time, count), [{:decimals, 2}])
224224
count = Integer.to_string(count)
225225
time = Integer.to_string(time)
226226

227227
[mfa, count, time_percentage, time, time_per_call]
228228
end
229229

230230
defp format_total(total_time, total_count) do
231-
time_per_call = :erlang.float_to_binary(total_time / total_count, [{:decimals, 2}])
231+
time_per_call = :erlang.float_to_binary(divide(total_time, total_count), [{:decimals, 2}])
232232

233233
["Total", Integer.to_string(total_count), "100.00", Integer.to_string(total_time), time_per_call]
234234
end
235235

236+
defp divide(_, 0), do: 0.0
237+
defp divide(t, n), do: t / n
238+
236239
defp column_lengths(header, rows) do
237240
max_lengths = Enum.map(header, &String.length/1)
238241

0 commit comments

Comments
 (0)