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

Casting int to array backed bit_fields can crash the compiler #3703

Open
hitchh1k3r opened this issue Jun 7, 2024 · 2 comments
Open

Casting int to array backed bit_fields can crash the compiler #3703

hitchh1k3r opened this issue Jun 7, 2024 · 2 comments

Comments

@hitchh1k3r
Copy link

hitchh1k3r commented Jun 7, 2024

Context

I was trying to set constant values for a 24bit bit_field.

Odin: dev-2024-06:96c06185d
OS: Windows 10 Professional (version: 22H2), build 19045.4412
CPU: Intel(R) Core(TM) i7-4771 CPU @ 3.50GHz
RAM: 16311 MiB
Backend: LLVM 17.0.1

Expected Behavior

The compiler should not crash.

Current Behavior

The compiler crashes with something like:

[DEBUG] 256
u8 -> 1
[...]\Odin\src\llvm_backend_const.cpp(414): Assertion Failure: `sz >= max_count` max_count: 2, sz: 1, written: 0, type u8

Failure Information

The compiler crashes when casting an integer to an array backed bit_field if the integer is larger than the array's individual elements can hold.

Here is a minimal reproduction:

package main

import "core:fmt"

main :: proc() {
  BitField :: bit_field [2]u8 {}
  fmt.println(BitField(256))
}
@hitchh1k3r hitchh1k3r changed the title Large value constants of array backed bit_fields crash the compiler Casting int to array backed bit_fields can crash the compiler Jun 8, 2024
@hitchh1k3r
Copy link
Author

While playing around with the crash, I found a second one that is probably related. If you cast a variable to an array backed bit_field:

BitField :: bit_field [2]u8 {}
i := u8(0)
fmt.println(BitField(i)) // Crash

this assert fails:

[...]Odin\src\llvm_backend_utility.cpp(1459): Assertion Failure: `is_type_array(st) || is_type_enumerated_array(st) || is_type_matrix(st)` bit_field [2]u8 { }

It's okay if you cast an array though:

BitField :: bit_field [2]u8 {}
i := [2]u8{ 0, 0 }
fmt.println(BitField(i)) // Okay

Which makes me think the int -> array backed bit_field is not supposed to be allowed, but there is just no error message that an array is expected.

@gingerBill
Copy link
Member

This bug is caused by the array-programming cast but it should not be allowed.

@github-actions github-actions bot added the stale label Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants