From 99b93d96695d7819b9d1264da5ecd91588ddc81f Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Mon, 30 Dec 2024 13:16:17 +0530 Subject: [PATCH] builtin: add runtime check for `[]Type{cap: negative}` (#23307) --- vlib/arrays/arrays.v | 3 +++ vlib/builtin/array.v | 14 ++++++++++++++ vlib/builtin/array_d_gcboehm_opt.v | 5 +++++ 3 files changed, 22 insertions(+) diff --git a/vlib/arrays/arrays.v b/vlib/arrays/arrays.v index 3e058ff152abe0..a4da4dcbaab6c0 100644 --- a/vlib/arrays/arrays.v +++ b/vlib/arrays/arrays.v @@ -225,6 +225,9 @@ pub: // Example: arrays.window([1, 2, 3, 4], size: 2) // => [[1, 2], [2, 3], [3, 4]] // Example: arrays.window([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], size: 3, step: 2) // => [[1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9]] pub fn window[T](array []T, attr WindowAttribute) [][]T { + if array.len == 0 { + return [][]T{} + } // allocate snapshot array mut windows := [][]T{cap: array.len - attr.size + 1} diff --git a/vlib/builtin/array.v b/vlib/builtin/array.v index 0770aae9f2622d..9973b904a559cf 100644 --- a/vlib/builtin/array.v +++ b/vlib/builtin/array.v @@ -31,6 +31,7 @@ pub enum ArrayFlags { // Internal function, used by V (`nums := []int`) fn __new_array(mylen int, cap int, elm_size int) array { panic_on_negative_len(mylen) + panic_on_negative_cap(cap) cap_ := if cap < mylen { mylen } else { cap } arr := array{ element_size: elm_size @@ -43,6 +44,7 @@ fn __new_array(mylen int, cap int, elm_size int) array { fn __new_array_with_default(mylen int, cap int, elm_size int, val voidptr) array { panic_on_negative_len(mylen) + panic_on_negative_cap(cap) cap_ := if cap < mylen { mylen } else { cap } mut arr := array{ element_size: elm_size @@ -82,6 +84,7 @@ fn __new_array_with_default(mylen int, cap int, elm_size int, val voidptr) array fn __new_array_with_multi_default(mylen int, cap int, elm_size int, val voidptr) array { panic_on_negative_len(mylen) + panic_on_negative_cap(cap) cap_ := if cap < mylen { mylen } else { cap } mut arr := array{ element_size: elm_size @@ -110,6 +113,7 @@ fn __new_array_with_multi_default(mylen int, cap int, elm_size int, val voidptr) fn __new_array_with_array_default(mylen int, cap int, elm_size int, val array, depth int) array { panic_on_negative_len(mylen) + panic_on_negative_cap(cap) cap_ := if cap < mylen { mylen } else { cap } mut arr := array{ element_size: elm_size @@ -132,6 +136,7 @@ fn __new_array_with_array_default(mylen int, cap int, elm_size int, val array, d fn __new_array_with_map_default(mylen int, cap int, elm_size int, val map) array { panic_on_negative_len(mylen) + panic_on_negative_cap(cap) cap_ := if cap < mylen { mylen } else { cap } mut arr := array{ element_size: elm_size @@ -155,6 +160,7 @@ fn __new_array_with_map_default(mylen int, cap int, elm_size int, val map) array // Private function, used by V (`nums := [1, 2, 3]`) fn new_array_from_c_array(len int, cap int, elm_size int, c_array voidptr) array { panic_on_negative_len(len) + panic_on_negative_cap(cap) cap_ := if cap < len { len } else { cap } arr := array{ element_size: elm_size @@ -170,6 +176,7 @@ fn new_array_from_c_array(len int, cap int, elm_size int, c_array voidptr) array // Private function, used by V (`nums := [1, 2, 3] !`) fn new_array_from_c_array_no_alloc(len int, cap int, elm_size int, c_array voidptr) array { panic_on_negative_len(len) + panic_on_negative_cap(cap) arr := array{ element_size: elm_size data: c_array @@ -1049,3 +1056,10 @@ fn panic_on_negative_len(len int) { panic('negative .len') } } + +@[if !no_bounds_checking ?; inline] +fn panic_on_negative_cap(cap int) { + if cap < 0 { + panic('negative .cap') + } +} diff --git a/vlib/builtin/array_d_gcboehm_opt.v b/vlib/builtin/array_d_gcboehm_opt.v index dffa424f64eb70..05c62774dc62c1 100644 --- a/vlib/builtin/array_d_gcboehm_opt.v +++ b/vlib/builtin/array_d_gcboehm_opt.v @@ -7,6 +7,7 @@ module builtin fn __new_array_noscan(mylen int, cap int, elm_size int) array { panic_on_negative_len(mylen) + panic_on_negative_cap(cap) cap_ := if cap < mylen { mylen } else { cap } arr := array{ element_size: elm_size @@ -19,6 +20,7 @@ fn __new_array_noscan(mylen int, cap int, elm_size int) array { fn __new_array_with_default_noscan(mylen int, cap int, elm_size int, val voidptr) array { panic_on_negative_len(mylen) + panic_on_negative_cap(cap) cap_ := if cap < mylen { mylen } else { cap } mut arr := array{ element_size: elm_size @@ -46,6 +48,7 @@ fn __new_array_with_default_noscan(mylen int, cap int, elm_size int, val voidptr fn __new_array_with_multi_default_noscan(mylen int, cap int, elm_size int, val voidptr) array { panic_on_negative_len(mylen) + panic_on_negative_cap(cap) cap_ := if cap < mylen { mylen } else { cap } mut arr := array{ element_size: elm_size @@ -63,6 +66,7 @@ fn __new_array_with_multi_default_noscan(mylen int, cap int, elm_size int, val v fn __new_array_with_array_default_noscan(mylen int, cap int, elm_size int, val array) array { panic_on_negative_len(mylen) + panic_on_negative_cap(cap) cap_ := if cap < mylen { mylen } else { cap } mut arr := array{ element_size: elm_size @@ -80,6 +84,7 @@ fn __new_array_with_array_default_noscan(mylen int, cap int, elm_size int, val a // Private function, used by V (`nums := [1, 2, 3]`) fn new_array_from_c_array_noscan(len int, cap int, elm_size int, c_array voidptr) array { panic_on_negative_len(len) + panic_on_negative_cap(cap) cap_ := if cap < len { len } else { cap } arr := array{ element_size: elm_size