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

Null pointer can be constructed and dereferenced in safe code #20497

Open
justinas opened this issue Jan 11, 2024 · 0 comments
Open

Null pointer can be constructed and dereferenced in safe code #20497

justinas opened this issue Jan 11, 2024 · 0 comments
Labels
Bug This tag is applied to issues which reports bugs. Unit: Checker Bugs/feature requests, that are related to the type checker. Unit: Type System Bugs/feature requests, that are related to the V types system.

Comments

@justinas
Copy link

justinas commented Jan 11, 2024

Describe the bug

V allows constructing and dereferencing null pointers in safe code. This results in undefined behavior using the C backend.

Reproduction Steps

fn main() {
    bar := &int(0)
    println(*bar)
}

Expected Behavior

&T(nil) is already illegal, so I would expect V to give a compile time error when using &T(0) as well.

In fact, in safe code casting an arbitrary number to a pointer should probably never be allowed.

Current Behavior

$ v -cflags '-fsanitize=undefined' cast.v
$ ./cast
/run/user/1000/v_1000/cast.1175514313447606659.tmp.c:12702:2: runtime error: load of null pointer of type 'int'
signal 11: segmentation fault
                                                        |       0x4058b9 | ./cast()
                                                        | 0x7fc553a3ffce | /nix/store/qn3ggz5sf3hkjs2c797xf7nan3amdxmp-glibc-2.38-27/lib/libc.so.6(+0x27fce)
                                                        | 0x7fc553a40089 | /nix/store/qn3ggz5sf3hkjs2c797xf7nan3amdxmp-glibc-2.38-27/lib/libc.so.6(__libc_start_main+0x89)
                                                        |       0x4058f5 | ./cast()

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.4 ac2dcc2

Environment details (OS name and version, etc.)

NixOS 23.11 (Tapir)
gcc (GCC) 12.3.0

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.

@justinas justinas added the Bug This tag is applied to issues which reports bugs. label Jan 11, 2024
@felipensp felipensp added Unit: Checker Bugs/feature requests, that are related to the type checker. Unit: Type System Bugs/feature requests, that are related to the V types system. labels Jan 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Unit: Checker Bugs/feature requests, that are related to the type checker. Unit: Type System Bugs/feature requests, that are related to the V types system.
Projects
None yet
Development

No branches or pull requests

2 participants