tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

30 lignes · 838 octetsBrut · Historique
1import sortkit.{type SkVec}
2import std/ffi.{type Resource}
3import std/io
4
5/// A deliberate bug: `sum` reads one element too many. `sk_vec_get` does
6/// not check its index (its header says so), so that read is past the
7/// vector's buffer. AddressSanitizer reports it with a stack that goes
8/// from the C library, through the binding, to the call in `sum` below.
9pub fn main :: -> () / <io, ffi>
10{
11 let v = sortkit.sk_vec_new ()
12 let _ = sortkit.sk_vec_push v 1
13 let _ = sortkit.sk_vec_push v 2
14 let _ = sortkit.sk_vec_push v 3
15 let _ = sortkit.sk_vec_push v 4
16 io.println (show (sum v 0 (sortkit.sk_vec_len v) 0))
17}
18
19fn sum :: v: Resource<SkVec> i: Int length: Int acc: Int -> Int / <ffi>
20{
21 // The bug: `>` should be `>=`.
22 if i > length
23 {
24 acc
25 }
26 else
27 {
28 sum v (i + 1) length (acc + sortkit.sk_vec_get v i)
29 }
30}