import sortkit.{type SkVec} import std/ffi.{type Resource} import std/io /// A deliberate bug: `sum` reads one element too many. `sk_vec_get` does /// not check its index (its header says so), so that read is past the /// vector's buffer. AddressSanitizer reports it with a stack that goes /// from the C library, through the binding, to the call in `sum` below. pub fn main :: -> () / { let v = sortkit.sk_vec_new () let _ = sortkit.sk_vec_push v 1 let _ = sortkit.sk_vec_push v 2 let _ = sortkit.sk_vec_push v 3 let _ = sortkit.sk_vec_push v 4 io.println (show (sum v 0 (sortkit.sk_vec_len v) 0)) } fn sum :: v: Resource i: Int length: Int acc: Int -> Int / { // The bug: `>` should be `>=`. if i > length { acc } else { sum v (i + 1) length (acc + sortkit.sk_vec_get v i) } }