exercism / elixir-analyzer

GNU Affero General Public License v3.0
30 stars 32 forks source link

Analyzer for the FileSniffer exercise return improper feedback #244

Closed ryanzidago closed 2 years ago

ryanzidago commented 2 years ago

The following solution:

defmodule FileSniffer do
  @exe_signature <<0x7F, 0x45, 0x4C, 0x46>>
  @bmp_signature <<0x42, 0x4D>>
  @png_signature <<0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A>>
  @jpg_signature <<0xFF, 0xD8, 0xFF>>
  @gif_signature <<0x47, 0x49, 0x46>>

  def type_from_extension("exe"), do: "application/octet-stream"
  def type_from_extension("bmp"), do: "image/bmp"
  def type_from_extension("png"), do: "image/png"
  def type_from_extension("jpg"), do: "image/jpg"
  def type_from_extension("gif"), do: "image/gif"
  def type_from_extension(_), do: :unknown

  def type_from_binary(<<@exe_signature, _rest::binary>>), do: type_from_extension("exe")
  def type_from_binary(<<@bmp_signature, _rest::binary>>), do: type_from_extension("bmp")
  def type_from_binary(<<@png_signature, _rest::binary>>), do: type_from_extension("png")
  def type_from_binary(<<@jpg_signature, _rest::binary>>), do: type_from_extension("jpg")
  def type_from_binary(<<@gif_signature, _rest::binary>>), do: type_from_extension("gif")
  def type_from_binary(_), do: :unknown

  def verify(file_binary, extension) do
    with type <- type_from_extension(extension),
         true <- type_from_binary(file_binary) == type do
      {:ok, type}
    else
      _error -> {:error, "Warning, file format and file extension do not match."}
    end
  end
end

triggers the following error:

Even though there could be other valid ways to solve this exercise, for the sake of mastering bitstrings please match the first few bytes of the argument of type_from_binary/1 with the special form <<>>.

Obviously I'm using <<>>, but via a module attribute.

angelikatyborska commented 2 years ago

Let me add to the list of problems with the FileSniffer analyzer @MeerKatDev's solution that is correct, yet triggers a comment too:

Screenshot 2021-12-17 at 10 50 37
jiegillet commented 2 years ago

Thank you very much for opening this issue. Two independent FileSniffer problems within a day, that's interesting :)

I've opened a PR that should fix both cases.