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 8B55CBC57 for ; Thu, 18 Mar 2010 07:47:18 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AhgCALppoUvRVdvckGdsb2JhbACDC44KigsIFQEBAQEJCQwHEwMfpwE3gWOFJS6ISwEBAwWBJ4JiagQ X-IronPort-AV: E=Sophos;i="4.51,264,1267398000"; d="scan'208";a="55485479" Received: from mail-ew0-f220.google.com ([209.85.219.220]) by mail1-smtp-roc.national.inria.fr with ESMTP; 18 Mar 2010 07:47:18 +0100 Received: by ewy20 with SMTP id 20so204211ewy.1 for ; Wed, 17 Mar 2010 23:47:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type; bh=Vb+j2qh+UBbkhOZ5MOfPllYTY2jNfvNUASfQQV1v6U4=; b=aDOu+uFvNhGuSXU6g5CyXuDsMBwTgU1YKm8EphBY1uGnmMVkDRn0V/NqoCizajRcte XCXN7nPm45mQoJP3ARpAoDq3yEsGwn/jiz5W+oIidkcSxlYiYg4IV0eNWArcT/Kmv8Lc 3wonZHmpVYgK5ArPjSW03mvxRH1uVJLWc1+oA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=xCQLr5B10v/PWC+fh83Bfe9A3RBJ8ZpieOtZJHjvcKfH2cJUPgE8bmBGxR3koNQP6M fTreJCvj3ksigPlcyRztvAX6MSP0aC26AV8h6RKskAAkDb2eTwQrKNl7JviflsweSG7K hwfL9PfRG3jRKs9QjMJdJdNVzVRKThDkWOSQk= MIME-Version: 1.0 Received: by 10.213.110.135 with SMTP id n7mr1265300ebp.58.1268894834822; Wed, 17 Mar 2010 23:47:14 -0700 (PDT) In-Reply-To: <4BA163AC.3030405@ens-lyon.org> References: <36ae71be1003171327h3dfe72f5t74d43ebc88035e3d@mail.gmail.com> <4BA163AC.3030405@ens-lyon.org> Date: Thu, 18 Mar 2010 08:47:14 +0200 Message-ID: <36ae71be1003172347x469625a5o8de8c5637d5727ff@mail.gmail.com> Subject: Re: [Caml-list] Buffer.add_channel hangs From: Stas Miasnikou To: Martin Jambon Cc: caml-list@yquem.inria.fr Content-Type: text/plain; charset=UTF-8 X-Spam: no; 0.00; buffer:01 ens-lyon:01 ocaml:01 buffer:01 ocaml:01 bug:01 bigarray:01 10,:98 char:01 wrote:01 wrote:01 caml-list:01 int:01 int:01 bin:01 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