zig
zig copied to clipboard
"bitcast packed union to integer" behavior test causing undefined behavior in the compiler
Zig Version
0.12.0-dev.3405+31791ae15
Steps to Reproduce and Observed Behavior
test "bitcast packed union to integer" {
const U = packed union {
x: u1,
y: u2,
};
comptime {
const a = U{ .x = 1 };
const b = U{ .y = 2 };
const cast_a = @as(u2, @bitCast(a));
const cast_b = @as(u2, @bitCast(b));
// truncated because the upper bit is garbage memory that we don't care about
try testing.expectEqual(@as(u1, 1), @as(u1, @truncate(cast_a)));
try testing.expectEqual(@as(u2, 2), cast_b);
}
}
$ valgrind debug/bin/zig test example.zig -fno-emit-bin
==1642303== Thread 1:
==1642303== Conditional jump or move depends on uninitialised value(s)
==1642303== at 0x69022BA: math.log2_int__anon_104847 (math.zig:1348)
==1642303== by 0x65F7A20: math.log2.log2__anon_92960 (log2.zig:39)
==1642303== by 0x643A8A4: math.big.int.calcLimbLen__anon_89347 (int.zig:33)
==1642303== by 0x6128550: math.big.int.Mutable.set__anon_78775 (int.zig:232)
==1642303== by 0x5E58728: math.big.int.Mutable.init__anon_63941 (int.zig:178)
==1642303== by 0x6654DF0: InternPool.Key.Int.Storage.toBigInt (InternPool.zig:929)
==1642303== by 0x691F720: InternPool.Key.hash64 (InternPool.zig:1137)
==1642303== by 0x6431A57: InternPool.Key.hash32 (InternPool.zig:1065)
==1642303== by 0x6123366: InternPool.KeyAdapter.hash (InternPool.zig:358)
==1642303== by 0x612455A: checkedHash (array_hash_map.zig:1841)
==1642303== by 0x612455A: array_hash_map.ArrayHashMapUnmanaged(void,void,array_hash_map.AutoContext(void),true).getOrPutInternal__anon_78747 (array_hash_map.zig:1658)
==1642303== by 0x5E546B5: array_hash_map.ArrayHashMapUnmanaged(void,void,array_hash_map.AutoContext(void),true).getOrPutAssumeCapacityAdapted__anon_63790 (array_hash_map.zig:816)
==1642303== by 0x5E5383D: array_hash_map.ArrayHashMapUnmanaged(void,void,array_hash_map.AutoContext(void),true).getOrPutContextAdapted__anon_63786 (array_hash_map.zig:753)
==1642303==
==1642303== Conditional jump or move depends on uninitialised value(s)
==1642303== at 0x61245D6: array_hash_map.ArrayHashMapUnmanaged(void,void,array_hash_map.AutoContext(void),true).getOrPutInternal__anon_78747 (array_hash_map.zig:1664)
==1642303== by 0x5E546B5: array_hash_map.ArrayHashMapUnmanaged(void,void,array_hash_map.AutoContext(void),true).getOrPutAssumeCapacityAdapted__anon_63790 (array_hash_map.zig:816)
==1642303== by 0x5E5383D: array_hash_map.ArrayHashMapUnmanaged(void,void,array_hash_map.AutoContext(void),true).getOrPutContextAdapted__anon_63786 (array_hash_map.zig:753)
==1642303== by 0x5E54795: array_hash_map.ArrayHashMapUnmanaged(void,void,array_hash_map.AutoContext(void),true).getOrPutAdapted__anon_63785 (array_hash_map.zig:738)
==1642303== by 0x5C62D55: InternPool.get (InternPool.zig:4992)
==1642303== by 0x5CF9F19: Module.intern (Module.zig:5713)
==1642303== by 0x5FA8067: Module.intValue_u64 (Module.zig:5923)
==1642303== by 0x621A804: Module.intValue__anon_82795 (Module.zig:5903)
==1642303== by 0x6A59EC2: Value.readFromMemory (Value.zig:948)
==1642303== by 0x6AF42ED: Sema.bitCastVal (Sema.zig:31512)
==1642303== by 0x65AE0A5: Sema.bitCast (Sema.zig:31479)
==1642303== by 0x698A395: Sema.zirBitcast (Sema.zig:10494)
Expected Behavior
No errors found by Valgrind.