From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p7G8Q7Xu007738 for ; Tue, 16 Aug 2011 10:26:07 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: At8CAE0pSk7RVdg2kGdsb2JhbABBqCIIFAEBAQEJCQ0HFAQhgUABAQEBAwwGAiwBGx4DDAYFCw0uIQEBEQEFARwGEyKjcgqMN4JVhTA7iG0CAwaGQQSHWYs5iXaCYzyDZQ X-IronPort-AV: E=Sophos;i="4.67,379,1309730400"; d="scan'208";a="105564128" Received: from mail-qw0-f54.google.com ([209.85.216.54]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 16 Aug 2011 10:25:40 +0200 Received: by qwc9 with SMTP id 9so5006057qwc.27 for ; Tue, 16 Aug 2011 01:25:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=CEAUZqFmgEBM0kmY3BmNKIQtAG87wS2s/g1AzzVk3EY=; b=VldfbphtchKShTGLj/pnKsUUUwy/iX9V8TBr5IruwxAQQHLr9pH2GwYKQX4xeyl8fZ h0phyiU7ojNyhD5tMmsXxAq+NDYGBcB7GGcZVdrwDXooWMsGmymaglKUHpWloXwkRR5v 0KxKYPMydvDZCW5+BJqAOeqhdODRwsW5t4ZcA= MIME-Version: 1.0 Received: by 10.229.62.194 with SMTP id y2mr3272382qch.4.1313483139560; Tue, 16 Aug 2011 01:25:39 -0700 (PDT) Received: by 10.229.76.229 with HTTP; Tue, 16 Aug 2011 01:25:39 -0700 (PDT) In-Reply-To: <4E4A2488.4050706@gmail.com> References: <4E4A2488.4050706@gmail.com> Date: Tue, 16 Aug 2011 12:25:39 +0400 Message-ID: From: Dmitry Bely To: Caml List Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by walapai.inria.fr id p7G8Q7Xu007738 Subject: Re: [Caml-list] Interfacing with C: bad practice 2011/8/16 Török Edwin : > 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? Unfortunately not because wrp_ml_cons() second parameter is not registered. So wrp_ml_cons() gets an invalid value. - Dmitry Bely