Browse thread
Buffer.add_channel hangs
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Stas Miasnikou <stas.miasnikou@g...> |
| Subject: | Re: [Caml-list] Buffer.add_channel hangs |
On 3/18/10, Martin Jambon <martin.jambon@ens-lyon.org> 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