caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Martin Jambon <martin.jambon@ens-lyon.org>
To: Stas Miasnikou <stas.miasnikou@gmail.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Buffer.add_channel hangs
Date: Wed, 17 Mar 2010 16:20:12 -0700	[thread overview]
Message-ID: <4BA163AC.3030405@ens-lyon.org> (raw)
In-Reply-To: <36ae71be1003171327h3dfe72f5t74d43ebc88035e3d@mail.gmail.com>

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/


  reply	other threads:[~2010-03-17 23:20 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-17 20:27 Stas Miasnikou
2010-03-17 23:20 ` Martin Jambon [this message]
2010-03-18  6:47   ` [Caml-list] " Stas Miasnikou
2010-03-18  9:09     ` Fabrice Le Fessant
2010-03-18  2:53 ` Goswin von Brederlow
2010-03-18 17:52 ` Stas Miasnikou
2010-03-20  8:29   ` Stas Miasnikou

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4BA163AC.3030405@ens-lyon.org \
    --to=martin.jambon@ens-lyon.org \
    --cc=caml-list@yquem.inria.fr \
    --cc=stas.miasnikou@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).