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 mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id DFEAABC57 for ; Thu, 18 Mar 2010 10:05:17 +0100 (CET) X-IronPort-AV: E=Sophos;i="4.51,265,1267398000"; d="scan'208";a="55493213" Received: from peermanence.saclay.inria.fr (HELO [195.83.212.205]) ([195.83.212.205]) by mail1-relais-roc.national.inria.fr with ESMTP; 18 Mar 2010 10:05:17 +0100 Message-ID: <4BA1EDDB.8030702@inria.fr> Date: Thu, 18 Mar 2010 10:09:47 +0100 From: Fabrice Le Fessant User-Agent: Thunderbird 2.0.0.23 (X11/20090817) MIME-Version: 1.0 To: Stas Miasnikou Cc: Martin Jambon , caml-list@yquem.inria.fr Subject: Re: [Caml-list] Buffer.add_channel hangs References: <36ae71be1003171327h3dfe72f5t74d43ebc88035e3d@mail.gmail.com> <4BA163AC.3030405@ens-lyon.org> <36ae71be1003172347x469625a5o8de8c5637d5727ff@mail.gmail.com> In-Reply-To: <36ae71be1003172347x469625a5o8de8c5637d5727ff@mail.gmail.com> X-Enigmail-Version: 0.95.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam: no; 0.00; buffer:01 buffer:01 ens-lyon:01 ocaml:01 ocaml:01 bug:01 bigarray:01 beginner's:01 bug:01 scalable:01 orsay:01 orsay:01 10,:98 char:01 beginners:01 Hi, Maybe you should just use (in_channel_length ic) to get the size of the file before hand, so that you can directly create a string with that size instead of a Buffer.t ? Regards, --Fabrice Stas Miasnikou wrote, On 03/18/2010 07:47 AM: > On 3/18/10, Martin Jambon wrote: >> Stas Miasnikou wrote: >>> 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"; >> ... > > Oh, never thought OCaml has bugs! ;-) > >> Since you uncovered this problem, please kindly submit a proper bug report >> at >> http://caml.inria.fr/mantis > > I've submitted it. > >> (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. > > Erm... yes. I think I follow the other advice and will use Bigarray. > > Stas > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > -- Fabrice LE FESSANT Chercheur, Equipe ASAP (As Scalable As Possible) http://www.lefessant.net/ INRIA-Futurs, Bat P - 112 Parc Orsay Université 2-4, rue Jacques Monod F-91893 Orsay Cedex, FRANCE