caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Interfacing with C: bad practice
@ 2011-08-16  7:37 Dmitry Bely
  2011-08-16  8:04 ` Török Edwin
                   ` (3 more replies)
  0 siblings, 4 replies; 31+ messages in thread
From: Dmitry Bely @ 2011-08-16  7:37 UTC (permalink / raw)
  To: Caml List

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". Of course, the
stack copy of "list" is not registered as a global root so wrp_ml_cons
gets an invalid value.

Maybe Ocaml docs should be updated to warn about pitfalls like that?

- Dmitry Bely

^ permalink raw reply	[flat|nested] 31+ messages in thread

end of thread, other threads:[~2011-08-16 20:18 UTC | newest]

Thread overview: 31+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-16  7:37 [Caml-list] Interfacing with C: bad practice Dmitry Bely
2011-08-16  8:04 ` Török Edwin
2011-08-16  8:25   ` Dmitry Bely
2011-08-16  8:43     ` Török Edwin
2011-08-16  9:46       ` rixed
2011-08-16  9:53         ` Dmitry Bely
2011-08-16 10:17           ` Török Edwin
2011-08-16 11:04             ` rixed
     [not found] ` <20110816.105738.246515733851238101.Christophe.Troestler@umons.ac.be>
2011-08-16  9:21   ` Dmitry Bely
2011-08-16 10:39     ` Mauricio Fernandez
2011-08-16 14:27       ` John Carr
2011-08-16 12:28 ` [Caml-list] " Dmitry Bely
2011-08-16 15:25 ` [Caml-list] " Richard W.M. Jones
2011-08-16 15:51   ` rixed
2011-08-16 16:00     ` Will M. Farr
2011-08-16 16:10     ` Richard W.M. Jones
2011-08-16 16:17       ` Richard W.M. Jones
2011-08-16 16:18       ` Dmitry Bely
2011-08-16 16:22         ` Richard W.M. Jones
2011-08-16 16:27           ` Richard W.M. Jones
2011-08-16 16:30             ` malc
2011-08-16 16:34             ` Török Edwin
2011-08-16 16:47               ` Richard W.M. Jones
2011-08-16 16:55               ` [Caml-list] " Jeffrey Scofield
2011-08-16 17:08                 ` Will M. Farr
2011-08-16 19:46                 ` Gerd Stolpmann
2011-08-16 20:18                   ` Jeffrey Scofield
2011-08-16 17:08       ` [Caml-list] " rixed
2011-08-16 16:06   ` John Carr
2011-08-16 16:14     ` Wojciech Meyer
2011-08-16 16:13   ` Dmitry Bely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).