Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
Fix off-by-one errors in Weak.get_copy and Weak.blit #1835
This PR fixes off-by-one errors in
Before this PR the following programs were accepted:
$ ocaml # let w = Weak.create 0;; val w : '_weak1 Weak.t = <abstr> # Weak.get_copy w (-1);; - : '_weak2 option = None # let w1, w2 = Weak.create 16, Weak.create 16;; val w1 : '_weak3 Weak.t = <abstr> val w2 : '_weak4 Weak.t = <abstr> # Weak.blit w1 (-1) w2 (-1) 8;; - : unit = ()
Since an offset of
$ cat segfault.ml let segfault = let e : (int ref, int) Ephemeron.Kn.t = Ephemeron.Kn.create 0 in Ephemeron.Kn.set_data e 0; match Ephemeron.Kn.get_key_copy e (-1) with | Some v -> print_int !v (* segfault here *) | None -> () $ ocaml segfault.ml Segmentation fault (core dumped)
With this PR, the compiler correctly raises
$ ocaml # let w1, w2 = Weak.create 16, Weak.create 16;; val w1 : '_weak1 Weak.t = <abstr> val w2 : '_weak2 Weak.t = <abstr> # Weak.blit w1 (-1) w2 (-1) 8;; Exception: Invalid_argument "Weak.blit". # let w = Weak.create 0;; val w : '_weak3 Weak.t = <abstr> # Weak.get_copy w (-1);; Exception: Invalid_argument "Weak.get_copy".
bobot left a comment
It indeed fix some off-by-one errors, so I approve it. But I would prefer to test the argument before doing any calculation, and I think the test in blit
For #676, I'm willing to cut it in pieces so that it could be more easily accepted. I just don't know where to cut and if it would be effective.