1
0
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:
Uwe Krüger 2020-05-07 22:41:41 +02:00 committed by GitHub
parent 673acdbd00
commit 722a2c71c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 5 deletions

View File

@ -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)
}

View File

@ -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]