Badarg raised in my NIF

Hello,

I’m writing a NIF to process a large list of 3-element tuples. When I pass a list of length of at most 256 elements, it works fine. However when my list length is 257, I’m getting a badarg error. That’s strange because I don’t use enif_make_badarg, I prefer to report {:error, msg} tuples to help in debugging (as the code is bit long with parsing all the terms).

Any ideas what can be wrong here?

1 Like

Can you share a snippet of code (nif code) or explain how you’re trying to work with the provided the list?

3 Likes

Hello,

Sorry for the delay, I was on vacation.

I need to create a grid of interpolated values from a large array of points. I want to do this in NIF as it seems it will be much faster. I chose Zig as the language.

My code looks more or less like this:

https://git.sr.ht/~cgenie/interpolate_nif/tree/master/item/zig_src/interpolate_nif.zig

It seems that when my input array is large, it does go as far as

https://git.sr.ht/~cgenie/interpolate_nif/tree/master/item/zig_src/interpolate_nif.zig#L96

(I added debug logs). However, it still throws a bad argument. If I run this simpler example however

https://git.sr.ht/~cgenie/interpolate_nif/tree/master/item/test.erl#L5

things work fine. The run_json function however does not (where the array is large).

1 Like

Tip: enif_make_double returns “badarg” exception if the argument is not a finite value.
Use enif_is_exception on the returned term to check.

1 Like

OK thanks, I added exception checking for enif_make_double but am still getting the error.

1 Like

The only enif_* functions raising badarg exception are

enif_make_badarg     // of course
enif_raise_exception // if passed the atom 'badarg'
enif_make_double     // for non finite values
enif_make_atom       // for too long atoms
enif_make_atom_len
enif_cpu_time        // if not supported
enif_schedule_nif    // various error cases

You can also use enif_has_pending_exception to find where the exception is generated.

2 Likes

OK, indeed, it’s the double values that were inf for some reason :slight_smile: Thank you.

2 Likes