Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buffer.add_channel hangs #5004

Closed
vicuna opened this Issue Mar 18, 2010 · 1 comment

Comments

Projects
None yet
1 participant
@vicuna
Copy link
Collaborator

vicuna commented Mar 18, 2010

Original bug ID: 5004
Reporter: myst
Status: closed (set by @xavierleroy on 2010-03-28T08:17:40Z)
Resolution: fixed
Priority: normal
Severity: minor
Version: 3.11.1
Fixed in version: 3.12.0+dev
Category: ~DO NOT USE (was: OCaml general)

Bug description

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.

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";
...

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented Mar 28, 2010

Comment author: @xavierleroy

Fixed as suggested. Will go in 3.12.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.