Replace String.to_integer with safe Integer.parse
Use Integer.parse/1 instead of String.to_integer/1 on user-supplied and external API input to prevent ArgumentError crashes on non-numeric values. Adds fallback defaults or nil returns at each call site.
This commit is contained in:
@@ -28,15 +28,8 @@ defmodule MusicLibraryWeb.CollectionController do
|
||||
end
|
||||
|
||||
def index(conn, params) do
|
||||
limit =
|
||||
params
|
||||
|> Map.get("limit", "20")
|
||||
|> String.to_integer()
|
||||
|
||||
offset =
|
||||
params
|
||||
|> Map.get("offset", "0")
|
||||
|> String.to_integer()
|
||||
limit = parse_int(params["limit"], 20)
|
||||
offset = parse_int(params["offset"], 0)
|
||||
|
||||
total = Collection.search_records_count("")
|
||||
|
||||
@@ -44,4 +37,15 @@ defmodule MusicLibraryWeb.CollectionController do
|
||||
|
||||
render(conn, :index, total: total, limit: limit, offset: offset, records: records)
|
||||
end
|
||||
|
||||
defp parse_int(nil, default), do: default
|
||||
|
||||
defp parse_int(value, default) when is_binary(value) do
|
||||
case Integer.parse(value) do
|
||||
{int, ""} -> int
|
||||
_ -> default
|
||||
end
|
||||
end
|
||||
|
||||
defp parse_int(_, default), do: default
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user