mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
builtin: Fix undefined behaviour when allocating empty structs (#4088)
This commit is contained in:
parent
7b1b647832
commit
cc75fe4fe5
@ -177,6 +177,9 @@ pub fn free(ptr voidptr) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn memdup(src voidptr, sz int) voidptr {
|
pub fn memdup(src voidptr, sz int) voidptr {
|
||||||
|
if sz == 0 {
|
||||||
|
return vcalloc(1)
|
||||||
|
}
|
||||||
mem := malloc(sz)
|
mem := malloc(sz)
|
||||||
return C.memcpy(mem, src, sz)
|
return C.memcpy(mem, src, sz)
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
struct A{
|
struct A {
|
||||||
mut:
|
mut:
|
||||||
val int
|
val int
|
||||||
nums []int
|
nums []int
|
||||||
}
|
}
|
||||||
|
|
||||||
struct B{
|
struct B {
|
||||||
mut:
|
mut:
|
||||||
a A
|
a A
|
||||||
}
|
}
|
||||||
@ -26,6 +26,9 @@ struct Foo {
|
|||||||
@type string
|
@type string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Empty {
|
||||||
|
}
|
||||||
|
|
||||||
//We need to make sure that this compiles with all the reserved names.
|
//We need to make sure that this compiles with all the reserved names.
|
||||||
struct ReservedKeywords {
|
struct ReservedKeywords {
|
||||||
delete int
|
delete int
|
||||||
@ -55,6 +58,12 @@ struct ReservedKeywords {
|
|||||||
while int
|
while int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_empty_struct() {
|
||||||
|
d := &Empty{}
|
||||||
|
println(d) // != voidptr(0)
|
||||||
|
println(sizeof(Empty)) // == 0
|
||||||
|
}
|
||||||
|
|
||||||
fn test_struct_levels() {
|
fn test_struct_levels() {
|
||||||
mut c := C{}
|
mut c := C{}
|
||||||
assert c.nums.len == 0
|
assert c.nums.len == 0
|
||||||
|
Loading…
Reference in New Issue
Block a user