From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id B6D4580161 for ; Wed, 21 Jun 2017 17:39:04 +0200 (CEST) Authentication-Results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=rich@annexia.org; spf=Pass smtp.mailfrom=rich@annexia.org; spf=Pass smtp.helo=postmaster@annexia.org Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of rich@annexia.org) identity=pra; client-ip=80.68.91.176; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="rich@annexia.org"; x-sender="rich@annexia.org"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of rich@annexia.org designates 80.68.91.176 as permitted sender) identity=mailfrom; client-ip=80.68.91.176; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="rich@annexia.org"; x-sender="rich@annexia.org"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of postmaster@annexia.org designates 80.68.91.176 as permitted sender) identity=helo; client-ip=80.68.91.176; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="rich@annexia.org"; x-sender="postmaster@annexia.org"; x-conformance=sidf_compatible; x-record-type="v=spf1" IronPort-PHdr: =?us-ascii?q?9a23=3AA4gWZBPstvV2hlAfqwwl6mtUPXoX/o7sNwtQ0KIM?= =?us-ascii?q?zox0Lf37rarrMEGX3/hxlliBBdydsKMbzbKO+4nbGkU4qa6bt34DdJEeHzQksu?= =?us-ascii?q?4x2zIaPcieFEfgJ+TrZSFpVO5LVVti4m3peRMNQJW2aFLduGC94iAPERvjKwV1?= =?us-ascii?q?Ov71GonPhMiryuy+4ZPebgFKiTanfb9+MAi9oBnMuMURnYZsMLs6xAHTontPde?= =?us-ascii?q?RWxGdoKkyWkh3h+Mq+/4Nt/jpJtf45+MFOTav1f6IjTbxFFzsmKHw65NfqtRbY?= =?us-ascii?q?UwSC4GYXX3gMnRpJBwjF6wz6Xov0vyDnuOdxxDWWMMvrRr0yRD+s7bpkSAXwhS?= =?us-ascii?q?kJNzA37nzZhM9+jK1UvB2uuh5wzIDPbYGJKPZzZL/Rcc8ASGZfWMtaSixPApm7?= =?us-ascii?q?b4sKF+cNM/pWr4jhqFsOtxS+HxSjD/7vxTNSgXH6x7Ax3eAmEQHAwgMgBNwCu2?= =?us-ascii?q?nTodXvNacdTeC1wbLSwjXFafNZxzj86InSfRw7pvGMR71wfNPXxEIyGQ3FiVCQ?= =?us-ascii?q?ppbkPzOTzukNsm6b7/BhVe21kWInpRtxrz6yzckvkonEnoEYxkze+Sh4w4s5P9?= =?us-ascii?q?m1RFNhbdK5EpZdtieXPJZsTMw4WWFnoiM6x6UGuZGleCgKz4wqyATZa/yBbYeJ?= =?us-ascii?q?7RPjW/yLLTd/nnJld6qzhxe08Ue+1u3xTsq53EpQoidEkdTArHQA2wHJ5sSaSP?= =?us-ascii?q?Zw/1+t2TOV2ADS7uFEL1o0la3eK5M52L4wl4YcsUTYEyDsnkX2irGZd0ol+uey?= =?us-ascii?q?8ejnZa/mppCEO49ulg7+KrgumtC4AekgLgcBRW2b+eCl2L3n/E35W6lKg+Ytkq?= =?us-ascii?q?jZtZDaPd4UqrS4Aw9TyIYj6gywAy2o0NQCzjE7KwdOcReDyozoIE3mIfbiDP75?= =?us-ascii?q?jU7/vi1swqWMBLzqApzXIjD81pKnNZR67UNHgkJn19FU57pdDLcMMPP6HEjru4?= =?us-ascii?q?qLXVcCLwWozrO/W51G3YQEVDfXDw=3D=3D?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0C7AgBYkkpZ/7BbRFBdGwEBAQMBAQEJA?= =?us-ascii?q?QEBFgEBAQMBAQEJAQEBgy1igQ2DIZsMAQEBAQEBBoEomAkuhXYCgnRDFAEBAQE?= =?us-ascii?q?BAQEBAQEBaiiCMyKCQwEBAQECATpECwsYCRMSDwUoilMFDAysGYwGhiY9gWqDJ?= =?us-ascii?q?IU2hScFnmIChzGMIoJrjzCVETYhgQqBAQhJhxA/NgEBiWgBAQE?= X-IPAS-Result: =?us-ascii?q?A0C7AgBYkkpZ/7BbRFBdGwEBAQMBAQEJAQEBFgEBAQMBAQE?= =?us-ascii?q?JAQEBgy1igQ2DIZsMAQEBAQEBBoEomAkuhXYCgnRDFAEBAQEBAQEBAQEBaiiCM?= =?us-ascii?q?yKCQwEBAQECATpECwsYCRMSDwUoilMFDAysGYwGhiY9gWqDJIU2hScFnmIChzG?= =?us-ascii?q?MIoJrjzCVETYhgQqBAQhJhxA/NgEBiWgBAQE?= X-IronPort-AV: E=Sophos;i="5.39,369,1493676000"; d="scan'208";a="229170626" Received: from annexia.org ([80.68.91.176]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/AES128-GCM-SHA256; 21 Jun 2017 17:39:03 +0200 Received: from rich by annexia.org with local (Exim 4.84_2) (envelope-from ) id 1dNhig-000697-Ib for caml-list@inria.fr; Wed, 21 Jun 2017 16:39:02 +0100 Date: Wed, 21 Jun 2017 16:39:02 +0100 From: "Richard W.M. Jones" To: caml-list@inria.fr Message-ID: <20170621153902.GM28111@annexia.org> References: <20170603064431.GL28111@annexia.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20170603064431.GL28111@annexia.org> User-Agent: Mutt/1.5.23 (2014-03-12) Subject: Re: [Caml-list] Matching exceptions in C code On Sat, Jun 03, 2017 at 07:44:31AM +0100, Richard W.M. Jones wrote: > I'm trying to catch a Unix_error exception in C code, and extract the > errno from it. Getting the errno is fine, but the problem is matching > on the Unix.Unix_error exception. > > My original code used the documented method: > > retv = caml_callbackN_exn (*cb, 4, args); > > if (Is_exception_result (retv)) { > retv = Extract_exception (retv); > if (Field (retv, 0) == *caml_named_value ("Unix.Unix_error")) > ... > > However the if statement never matched the exception. (I verified on > the OCaml side that the correct exception is being thrown). > > After reading: > > http://caml.inria.fr/pub/ml-archives/caml-list/2006/05/097f63cfb39a80418f95c70c3c520aa8.en.html > http://caml.inria.fr/pub/ml-archives/caml-list/2009/06/797e2f797f57b8ea2a2c0e431a2df312.en.html > > it seems I'm not the first person to have this problem. It may be > that Unix is linked twice, but I'm not really sure about that. I'm > using -output-obj with unix.cmxa linked into the object file, and also > -lunix in the link step, which is the documented way to do things, but > I don't know if that means the Unix module initializes itself twice. > > Anyway, I modified the code to this, which *works* and is actually > more convenient than the documented method (the real code matches on > several other system exceptions as well): > > ... > 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. I'm still interested in the answers to the questions below ... > The questions are: > > (a) Will this continue to work in future? It seems to be > undocumented. > > (b) Could we have some sort of official function to hide the details? Rich.