On 08/16/2011 10:37 AM, Dmitry Bely wrote:
> I would like to share my experience of writing bad C bindings. The
> following code is wrong, although no "living in harmony with the
> garbage collector" rule seems to be violated:
>
> value wrp_ml_cons (value v, value l)
> {
> CAMLparam2(v, l);
> CAMLlocal1(cell);
> cell = caml_alloc_small(2, Tag_cons);
> Field(cell, 0) = v;
> Field(cell, 1) = l;
> CAMLreturn(cell);
> }
>
> value string_list(const char ** s)
> {
> CAMLparam0();
> CAMLlocal1(list);
> list = Val_emptylist;
> while (*s != NULL) {
> list = wrp_ml_cons(caml_copy_string(*s), list); /* bug! */
> }
> CAMLreturn(list);
> }
>
> In the line
>
> list = wrp_ml_cons(caml_copy_string(*s), list); /* bug! */
>
> C compiler first puts "list" pointer on stack and then calls
> caml_copy_string(*s), potentially invalidating "list".
list is a local root though, shouldn't that prevent the invalidation?
Best regards,
--Edwin
--
Caml-list mailing list. Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs