From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id E4B8FBC57 for ; Thu, 18 Mar 2010 00:20:15 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AqwBAH8AoUtCbwQbe2dsb2JhbACDC5gZFQEBFiQEHqg/kDwFgSyCYGqDHg X-IronPort-AV: E=Sophos;i="4.51,261,1267398000"; d="scan'208";a="47350616" Received: from out3.smtp.messagingengine.com ([66.111.4.27]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/ADH-AES256-SHA; 18 Mar 2010 00:20:15 +0100 Received: from compute1.internal (compute1.internal [10.202.2.41]) by gateway1.messagingengine.com (Postfix) with ESMTP id 5F2E2E57A7; Wed, 17 Mar 2010 19:20:14 -0400 (EDT) Received: from heartbeat2.messagingengine.com ([10.202.2.161]) by compute1.internal (MEProxy); Wed, 17 Mar 2010 19:20:14 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=messagingengine.com; h=message-id:date:from:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding; s=smtpout; bh=c13s7SIu6tSA4bG3sT3+CVINvLk=; b=d0ONHh6SNjQ2h9kRQ31q6xdFPfHWy3F09GtUv2Rsgqvzgjr5MNPMIOaWR5wfUDFxallutBqosT+5qd1Z8IVYw+hfgcV8/+qTgPasfa8cpsgeACHvlcJskisxxZKk0rKUbjTruT/16LICvk1FwBCPEaoZboXaNiMtKBZneVzY+UE= X-Sasl-enc: Q5V1nzt242wKSpKkLGA+wfEluzZOoSoGxyOqquCJnTW9 1268868014 Received: from [192.168.1.61] (64-71-1-162.static.wiline.com [64.71.1.162]) by mail.messagingengine.com (Postfix) with ESMTPSA id DCE583E0E2; Wed, 17 Mar 2010 19:20:13 -0400 (EDT) Message-ID: <4BA163AC.3030405@ens-lyon.org> Date: Wed, 17 Mar 2010 16:20:12 -0700 From: Martin Jambon User-Agent: Thunderbird 2.0.0.22 (X11/20090908) MIME-Version: 1.0 To: Stas Miasnikou Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Buffer.add_channel hangs References: <36ae71be1003171327h3dfe72f5t74d43ebc88035e3d@mail.gmail.com> In-Reply-To: <36ae71be1003171327h3dfe72f5t74d43ebc88035e3d@mail.gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; ens-lyon:01 buffer:01 ocaml:01 buffer:01 bug:01 char:01 wrote:01 caml-list:01 int:01 int:01 bin:01 bin:01 jambon:01 jambon:01 hangs:01 Stas Miasnikou wrote: > Hi, > > OCaml 3.11.1, OpenBSD 4.6, i386. > > I am trying to read whole file by doing: > > let read_file_bin name = > let ic = open_in_bin name in > let b = Buffer.create 1024 in > (try Buffer.add_channel b ic max_int with _ -> ()); (* <-- HERE *) > close_in ic; > Array.init (Buffer.length b) (fun i -> int_of_char (Buffer.nth b i)) > > but it hangs on the line marked. Am I doing something wrong? The problem is max_int and the fact that Buffer.add_channel and Buffer.resize do not check for this possibility: let add_channel b ic len = if b.position + len > b.length then resize b len; really_input ic b.buffer b.position len; b.position <- b.position + len Something like the following would be better: let add_channel b ic len = if len < 0 || len > Sys.max_string_length then invalid_arg "Buffer.add_channel"; ... Since you uncovered this problem, please kindly submit a proper bug report at http://caml.inria.fr/mantis (and figure what to do if the file is larger than 16MB on 32-bit systems) Of course, you can see from the implementation of the Buffer module that a string of your maximum length is created no matter what, which you surely want to avoid especially on 64-bit systems where Sys.max_string_length is very large. Martin -- http://mjambon.com/