mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
builtin: new_array: cap should not be less than len
This commit is contained in:
parent
673acdbd00
commit
722a2c71c3
@ -17,7 +17,7 @@ pub mut:
|
||||
|
||||
// Internal function, used by V (`nums := []int`)
|
||||
fn __new_array(mylen int, cap int, elm_size int) array {
|
||||
cap_ := if cap == 0 { 1 } else { cap }
|
||||
cap_ := if cap < mylen { mylen } else { cap }
|
||||
arr := array{
|
||||
len: mylen
|
||||
cap: cap_
|
||||
@ -29,11 +29,11 @@ fn __new_array(mylen int, cap int, elm_size int) array {
|
||||
|
||||
// Private function, used by V (`nums := [1, 2, 3]`)
|
||||
fn new_array_from_c_array(len, cap, elm_size int, c_array voidptr) array {
|
||||
cap_ := if cap == 0 { 1 } else { cap }
|
||||
cap_ := if cap < len { len } else { cap }
|
||||
|
||||
arr := array{
|
||||
len: len
|
||||
cap: cap
|
||||
cap: cap_
|
||||
element_size: elm_size
|
||||
data: vcalloc(cap_ * elm_size)
|
||||
}
|
||||
|
@ -142,10 +142,13 @@ pub fn v_calloc(n int) byteptr {
|
||||
}
|
||||
|
||||
pub fn vcalloc(n int) byteptr {
|
||||
if n <= 0 {
|
||||
if n < 0 {
|
||||
panic('calloc(<=0)')
|
||||
} else if n == 0 {
|
||||
return byteptr(0)
|
||||
} else {
|
||||
return C.calloc(n, 1)
|
||||
}
|
||||
return C.calloc(n, 1)
|
||||
}
|
||||
|
||||
[unsafe_fn]
|
||||
|
Loading…
Reference in New Issue
Block a user