Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

builtin: add runtime checks for []u8{len: negative} #23294

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions vlib/builtin/array.v
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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)
cap_ := if cap < mylen { mylen } else { cap }
arr := array{
element_size: elm_size
Expand All @@ -41,6 +42,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)
cap_ := if cap < mylen { mylen } else { cap }
mut arr := array{
element_size: elm_size
Expand Down Expand Up @@ -79,6 +81,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)
cap_ := if cap < mylen { mylen } else { cap }
mut arr := array{
element_size: elm_size
Expand Down Expand Up @@ -106,6 +109,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)
cap_ := if cap < mylen { mylen } else { cap }
mut arr := array{
element_size: elm_size
Expand All @@ -127,6 +131,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)
cap_ := if cap < mylen { mylen } else { cap }
mut arr := array{
element_size: elm_size
Expand All @@ -149,6 +154,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)
cap_ := if cap < len { len } else { cap }
arr := array{
element_size: elm_size
Expand All @@ -163,6 +169,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)
arr := array{
element_size: elm_size
data: c_array
Expand Down Expand Up @@ -1035,3 +1042,10 @@ pub fn (data voidptr) vbytes(len int) []u8 {
pub fn (data &u8) vbytes(len int) []u8 {
return unsafe { voidptr(data).vbytes(len) }
}

@[if !no_bounds_checking ?; inline]
fn panic_on_negative_len(len int) {
if len < 0 {
panic('negative .len')
}
}
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 @@ -6,6 +6,7 @@
module builtin

fn __new_array_noscan(mylen int, cap int, elm_size int) array {
panic_on_negative_len(mylen)
cap_ := if cap < mylen { mylen } else { cap }
arr := array{
element_size: elm_size
Expand All @@ -17,6 +18,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)
cap_ := if cap < mylen { mylen } else { cap }
mut arr := array{
element_size: elm_size
Expand All @@ -43,6 +45,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)
cap_ := if cap < mylen { mylen } else { cap }
mut arr := array{
element_size: elm_size
Expand All @@ -59,6 +62,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)
cap_ := if cap < mylen { mylen } else { cap }
mut arr := array{
element_size: elm_size
Expand All @@ -75,6 +79,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)
cap_ := if cap < len { len } else { cap }
arr := array{
element_size: elm_size
Expand Down
Loading