From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p7G8hY9B008509 for ; Tue, 16 Aug 2011 10:43:34 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AjoEAH4tSk7RVdY2imdsb2JhbABBmSCPAggUAQEBCgkNBxIGIYFAAQEBAQIBDAYCHgENARscAgMBCwYFCw0JFg8JAwIBAgEPAhEBBQEcEwYCAh6HTgKcCwqMN4JVhTQ7iG0CAwaGQQSHWYs5hQyBKINCgmM8g2M X-IronPort-AV: E=Sophos;i="4.67,379,1309730400"; d="scan'208";a="115960880" Received: from mail-bw0-f54.google.com ([209.85.214.54]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 16 Aug 2011 10:43:28 +0200 Received: by bkat8 with SMTP id t8so7024846bka.27 for ; Tue, 16 Aug 2011 01:43:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:x-enigmail-version:content-type :content-transfer-encoding; bh=rZCHcxm8GuSidwicAnSgHcZLgILhL52YAfeYU9DIsRA=; b=r3qxzhjq3/2tXY/46rGeIqE/oQG+pfLqHbNJiYj0QZlfUROx0W0SigqKHeQPjQbTCn 4m3stOQphTdhpHqN2EtYoXBdOMopGvaF8fKzplFHIbC4XFFijNvoPrWILnGL7dDcFZNs M3oJE8lVSS5rEeeV/Nj6ih2Yk+hmeCjT45ogo= Received: by 10.205.24.77 with SMTP id rd13mr907764bkb.330.1313484208256; Tue, 16 Aug 2011 01:43:28 -0700 (PDT) Received: from [192.168.1.101] ([79.114.94.185]) by mx.google.com with ESMTPS id b3sm1809600bke.44.2011.08.16.01.43.25 (version=SSLv3 cipher=OTHER); Tue, 16 Aug 2011 01:43:26 -0700 (PDT) Message-ID: <4E4A2DAC.5010207@gmail.com> Date: Tue, 16 Aug 2011 11:43:24 +0300 From: =?ISO-8859-1?Q?T=F6r=F6k_Edwin?= User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110626 Icedove/3.1.11 MIME-Version: 1.0 To: caml-list@inria.fr References: <4E4A2488.4050706@gmail.com> In-Reply-To: X-Enigmail-Version: 1.1.2 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit Subject: Re: [Caml-list] Interfacing with C: bad practice On 08/16/2011 11:25 AM, Dmitry Bely wrote: > 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 is registered there, is that not enough? (the macro registers address of list AFAIK, so even if you change its value, it'll stay registered as a local root) >>> 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. 'list' should be reachable via caml_local_roots, so if it really gets an invalid value it sounds like a bug to me. Best regards, --Edwin