From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: Joel Reymont <joelr1@gmail.com>
Cc: micha <micha-1@fantasymail.de>, caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Wrapping OCaml function returning a variant
Date: Wed, 28 Mar 2007 16:57:50 +0200 [thread overview]
Message-ID: <460A826E.4000105@inria.fr> (raw)
In-Reply-To: <CD3AF249-FA07-4DBD-A360-06155A6DE2BF@gmail.com>
> I came up with this. Is it sound?
Not quite.
> char *morph(char *code, char** error, int *line, int *start, int *end)
> {
> static value *closure = NULL;
> value v;
> char *res;
>
> if (closure == NULL)
> closure = caml_named_value("morph");
>
> v = callback(*closure, caml_copy_string(code));
Here, caml_copy_string could trigger a GC and move the closure pointed
to by closure, so the value of *closure could become invalid.
You need to order the computations explicitly:
value vcode;
if (closure == NULL)
closure = caml_named_value("morph");
vcode = caml_copy_string(code);
v = callback(*closure, vcode);
> switch (Long_val(v)) {
v is not a constant constructor, but a constructor with arguments.
So, its representation is not an integer, but a pointer to a block.
You want to discriminate on the tag of this block.
switch (Tag_val(v)) {
> case 0: /* success */
> res = String_val(Field(v, 0));
> *error = NULL;
> case 1: /* error */
> res = NULL;
> *error = String_val(Field(v, 0));
> *line = Int_val(Field(v, 1));
> *start = Int_val(Field(v, 2));
> *end = Int_val(Field(v, 3));
> }
The next GC can move the strings obtained by String_val, making
invalid the pointer you return or store in *error. Take a copy immediately:
switch (Tag_val(v)) {
case 0: /* success */
res = strdup(String_val(Field(v, 0)));
*error = NULL;
case 1: /* error */
res = NULL;
*error = strdup(String_val(Field(v, 0)));
*line = Int_val(Field(v, 1));
*start = Int_val(Field(v, 2));
*end = Int_val(Field(v, 3));
}
Hope this helps,
- Xavier Leroy
next prev parent reply other threads:[~2007-03-28 14:57 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-03-28 11:33 Joel Reymont
2007-03-28 11:47 ` [Caml-list] " Richard Jones
2007-03-28 12:25 ` micha
2007-03-28 12:51 ` Joel Reymont
2007-03-28 13:28 ` Richard Jones
2007-03-28 14:34 ` micha
2007-03-28 14:46 ` Joel Reymont
2007-03-28 14:56 ` micha
2007-03-28 14:57 ` Xavier Leroy [this message]
2007-03-28 15:07 ` Joel Reymont
2007-03-28 22:47 ` Joel Reymont
2007-03-28 12:55 ` Serge Aleynikov
2007-03-28 12:01 ` Joel Reymont
2007-03-28 13:40 ` Serge Aleynikov
2007-03-28 12:13 ` Joel Reymont
2007-03-28 13:53 ` Serge Aleynikov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=460A826E.4000105@inria.fr \
--to=xavier.leroy@inria.fr \
--cc=caml-list@yquem.inria.fr \
--cc=joelr1@gmail.com \
--cc=micha-1@fantasymail.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox