nhpip

nhpip

Identifying function calls in AST

Hi,

As the title states, I want to reject an AST with function calls. This seems to work, is there a better way?

  @invalid_ast_ops [:., :import, :apply, :__block__]

  defp sanitize_args(args) do
    Macro.prewalk(args, fn {call, _, _} when call in @invalid_ast_ops ->
                              raise "Invalid arguments"

                           {call, _, args} when is_atom(call) and is_list(args) ->
                              Macro.special_form?(call, length(args)) || raise "Invalid arguments"

                           v -> v
    end)

Thanks

Most Liked

fuelen

fuelen

A couple of years ago, I wrote this code that works a bit differently – it allows only a whitelist and collects all invalid nodes for error reporting:

def ensure_ast_safe(ast) do
  Macro.prewalk(
    ast,
    [],
    fn
      {fn_name, _, _} = node, acc
      when fn_name in [:%{}, :sigil_D, :=, :sigil_U, :<<>>, :{}, :<>] ->
        {node, acc}

      {atom, _} = node, acc when is_atom(atom) ->
        {node, acc}

      node, acc
      when is_atom(node) or is_binary(node) or is_integer(node) or is_list(node) or
             is_float(node) ->
        {node, acc}

      node, acc ->
        {nil, [node | acc]}
    end
  )
  |> elem(1)
  |> case do
    [] ->
      :ok

    expressions ->
      {:error, {:unsupported_expressions, expressions}}
  end
end

Hope it helps

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement