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

Array.init evaluates callback although it should not do so #6575

Closed
vicuna opened this issue Sep 23, 2014 · 1 comment
Closed

Array.init evaluates callback although it should not do so #6575

vicuna opened this issue Sep 23, 2014 · 1 comment
Assignees
Milestone

Comments

@vicuna
Copy link

vicuna commented Sep 23, 2014

Original bug ID: 6575
Reporter: gerd
Assigned to: @alainfrisch
Status: closed (set by @xavierleroy on 2017-02-16T14:18:27Z)
Resolution: fixed
Priority: normal
Severity: minor
Version: 4.02.0
Target version: 4.02.2+dev / +rc1
Category: standard library
Related to: #4950
Monitored by: @hcarty

Bug description

Look at this (ocaml-4.02):

let n = 0 ;;
ignore (Array.init (n-1) (fun _ -> assert false));;

$ ocamlopt -o m m.ml
$ ./m
Fatal error: exception Assert_failure("m.ml", 2, 35)

Same with ocamlc.

Additional information

This is straight-forward from the definition of Array.init, but nevertheless quite surprising behavior, because all other stdlib functions check the arguments before calling anything back.

@vicuna
Copy link
Author

vicuna commented Mar 11, 2015

Comment author: @alainfrisch

Commit 15898 on 4.02, 15899 on trunk. I've added a check for size < 0 before calling Array.make. Notes:

  • Array.init used to raise (Invalid_argument "Array.make") in that case, not it raises (Invalid_argument "Array.init"). I don't expect this to break any code and is clearly better.

  • Checking for the maximum array size is more difficult, since this size depends on whether we are building an array of floats or a regular array (and we don't know before calling the initializer once). Anyway, it is less clear that when the size is too large, the callback should not be called at all.

@vicuna vicuna closed this as completed Feb 16, 2017
@vicuna vicuna added the stdlib label Mar 14, 2019
@vicuna vicuna added this to the 4.02.2 milestone Mar 14, 2019
@vicuna vicuna added the bug label Mar 20, 2019
avsm pushed a commit to avsm/ocaml that referenced this issue Jun 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants