[erlang-bugs] erl nif utf8 bug

Paul Davis <>
Sun Feb 26 00:17:50 CET 2012


Couple things wrong here. Firstly, enif_inspect_iolist_as_binary is
akin to iolist_to_binary. Specifically, it doesn't do any conversions
from Unicode code points to binary. To see that in action:

Eshell V5.8.5  (abort with ^G)
1> iolist_to_binary([910275]).
** exception error: bad argument
     in function  iolist_to_binary/1
        called as iolist_to_binary([910275])

The actual error here is that you're not checking return codes.
enif_inspect_iolist_as_binary is returning an error that you're
ignoring. Quick diff is:

diff --git a/c_src/mymodule.c b/c_src/mymodule.c
index af311d2..23c9bfc 100644
--- a/c_src/mymodule.c
+++ b/c_src/mymodule.c
@@ -33,7 +33,9 @@ static ERL_NIF_TERM mymodule_nif_bin_size(ErlNifEnv*
env, int                                            const ERL_NIF_TERM
argv[])
 {
   ErlNifBinary bin;
-  enif_inspect_iolist_as_binary(env, argv[1], &bin);
+  if(!enif_inspect_iolist_as_binary(env, argv[1], &bin)) {
+    return enif_make_badarg(env);
+  }

   return enif_make_int(env, bin.size);
 }

On Sat, Feb 25, 2012 at 4:57 PM, Heinz N. Gies <> wrote:
> I noticed that the nif functions in erlang don't handle utf8/16 correctly
>
> https://github.com/Licenser/erlniftest is an example of the problem.
>
> Regards,
> Heinz
> --
> Heinz N. Gies
> 
> http://licenser.net
>
> _______________________________________________
> erlang-bugs mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-bugs


More information about the erlang-bugs mailing list