Skip to content

Commit

Permalink
builtin: add runtime check for []Type{cap: negative}
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Dec 29, 2024
1 parent 82c3edb commit 8743509
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
14 changes: 14 additions & 0 deletions vlib/builtin/array.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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')
}
}
5 changes: 5 additions & 0 deletions vlib/builtin/array_d_gcboehm_opt.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 8743509

Please sign in to comment.