caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Richard W.M. Jones" <rich@annexia.org>
To: John Whitington <john@coherentgraphics.co.uk>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Matching exceptions in C code
Date: Wed, 21 Jun 2017 17:06:55 +0100	[thread overview]
Message-ID: <20170621160655.GT8764@annexia.org> (raw)
In-Reply-To: <47446BCC-CCDC-4DD0-A07C-349589DD3FE5@coherentgraphics.co.uk>

On Wed, Jun 21, 2017 at 04:50:06PM +0100, John Whitington wrote:
> >>    retv = Extract_exception (retv);
> >>    exn_name = String_val (Field (Field (retv, 0), 0));
> >>    if (strcmp (exn_name, "Unix.Unix_error") == 0) {
> >>      int errcode = code_of_unix_error (Field (retv, 1));
> > 
> > So I found out today that this doesn't work for every exception.
> > 
> > For 'End_of_file' in particular, 'exn_name' ends up pointing to a bit
> > of random memory (unfortunately it's not NULL or otherwise easily
> > detectable), and so the program crashes in the strcmp.
> 
> End_of_file, Invalid_argument, and Failure are not defined in pervasives.ml, they are “internal” in some sense. Perhaps this is the cause?

Actually it's not a random pointer, it's a pointer made of ASCII
characters.  In other words it seems there is one less indirection.
This code works for me:

  if (Tag_val (Field (exn, 0)) == String_tag)
    exn_name = String_val (Field (exn, 0));    // for End_of_file
  else
    exn_name = String_val (Field (Field (exn, 0), 0));  // for others

but I feel like I'm well outside the bounds of valid, supportable use
of the OCaml VM here.

Rich.

      reply	other threads:[~2017-06-21 16:07 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-03  6:44 Richard W.M. Jones
2017-06-21 15:39 ` Richard W.M. Jones
2017-06-21 15:50   ` John Whitington
2017-06-21 16:06     ` Richard W.M. Jones [this message]

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=20170621160655.GT8764@annexia.org \
    --to=rich@annexia.org \
    --cc=caml-list@inria.fr \
    --cc=john@coherentgraphics.co.uk \
    /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;
as well as URLs for NNTP newsgroup(s).