luerl icon indicating copy to clipboard operation
luerl copied to clipboard

pcall and division by zero

Open iggi42 opened this issue 3 years ago • 6 comments

crash = function()
  return 10/0
end
return pcall(crash)

This still crashes in luerl, ignoring the supposed protection of "pcall".

The official lua returns inf, when divided by zero. Pcall returns true here.

iggi42 avatar Jun 14 '22 20:06 iggi42

guess extending the numeric operators on how to handle "inf" would be the right move.

That is here, right? https://github.com/rvirding/luerl/blob/develop/src/luerl_emul.erl#L1020' sounds like a good first pr.

iggi42 avatar Jun 14 '22 20:06 iggi42

The problem is that Erlang floating point doesn't "inf", if you divide by 0.0 you get a badarith error. There is nothing to do about this. I will check pcall though to see what is going on with it failing.

rvirding avatar Jun 20 '22 12:06 rvirding

The reason why pcall crashes is that by design it only catches Lua errors and and not Erlang errors and the badarith error generated by dividing by 0 is an Erlang error.

rvirding avatar Jun 20 '22 12:06 rvirding

The problem is that Erlang floating point doesn't "inf", if you divide by 0.0 you get a badarith error. There is nothing to do about this.

I mean, we would need to extend what the internal representation of a number can be. like adding the :infinity atom. Lua also has a NaN, (resulting from inf - inf for example). probably want to add both of these in one go.

Alternatively maybe extend division with a clause to make it

op('/', A1, A2, St) ->
    numeric_op('/', A1, A2, St, <<"__div">>, fun (_,0) -> lua_error("dividing by 0 bad", St);
                                                                         (N1,N2) -> N1/N2 end);

Not sure if this would work.

iggi42 avatar Jun 21 '22 04:06 iggi42

like how close to the official lua c implementation is luerl supposed to be?

iggi42 avatar Jun 21 '22 04:06 iggi42

As close as is reasonably possible using standard Erlang and OTP.

rvirding avatar Jul 26 '22 19:07 rvirding