Skip to content

v3: sizeof(heap-promoted struct variable) returns pointer size instead of struct size #29305

Closed

Description

@antono3

Describe the bug

For a heap-promoted local struct, sizeof(variable) uses its generated C pointer representation rather than its V value type. This can silently truncate raw struct copies.

Reproduction Steps

Save as repro.v:

struct Record {
mut:
    id u32
    values [1024]u32
}

fn update(mut record Record) {
    record.id = 7
}

fn main() {
    mut record := Record{}
    update(mut record)
    assert sizeof(record) == sizeof(Record)
}

Compile and execute the binary directly:

v -new-compiler -cc tcc -o repro repro.v
./repro

Also reproduces with -cc gcc.

Expected Behavior

The assertion passes: both sizes describe the 4100-byte Record value.

Current Behavior

The binary exits 1:

left value: sizeof(record) = 8
right value: sizeof(Record) = 4100
V panic: Assertion failed...

Relevant generated C:

main__Record* record = (main__Record*)memdup(&(main__Record){0}, sizeof(main__Record));
update(record);
if (!(sizeof(record) == sizeof(main__Record))) {
    // assertion fails
}

Workaround and Impact

Use sizeof(Record) explicitly instead of sizeof(record). This avoids truncated SPS/PPS/slice-header storage in the public antono2/v_vulkan_video player.

V version and Environment

V 0.5.2, upstream commit 0dc6a692ed5333cbe3f05ff63ae0766f0333038a (current master when retested), clean source-built checkout. Ubuntu 24.04 x86-64. Reproduces with strict -new-compiler using both bundled TinyCC and GCC 13.3.0. Released V 0.5.2 (7647ce1) with TinyCC passes.

No imports, native dependencies, or GPU are needed for this reproducer.

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions