Commit 42d75f1a25

ominitay <37453713+ominitay@users.noreply.github.com>
2022-03-10 20:53:28
std.math.lossyCast: fix integer overflow
Fixes integer overflow caused by cast from maxInt(u32) as an f32 to u32.
1 parent b3259b4
Changed files (1)
lib
lib/std/math.zig
@@ -1190,18 +1190,18 @@ pub fn lossyCast(comptime T: type, value: anytype) T {
         .Int => {
             switch (@typeInfo(@TypeOf(value))) {
                 .Int, .ComptimeInt => {
-                    if (value > maxInt(T)) {
+                    if (value >= maxInt(T)) {
                         return @as(T, maxInt(T));
-                    } else if (value < minInt(T)) {
+                    } else if (value <= minInt(T)) {
                         return @as(T, minInt(T));
                     } else {
                         return @intCast(T, value);
                     }
                 },
                 .Float, .ComptimeFloat => {
-                    if (value > maxInt(T)) {
+                    if (value >= maxInt(T)) {
                         return @as(T, maxInt(T));
-                    } else if (value < minInt(T)) {
+                    } else if (value <= minInt(T)) {
                         return @as(T, minInt(T));
                     } else {
                         return @floatToInt(T, value);
@@ -1218,6 +1218,7 @@ test "lossyCast" {
     try testing.expect(lossyCast(i16, 70000.0) == @as(i16, 32767));
     try testing.expect(lossyCast(u32, @as(i16, -255)) == @as(u32, 0));
     try testing.expect(lossyCast(i9, @as(u32, 200)) == @as(i9, 200));
+    try testing.expect(lossyCast(u32, @as(f32, maxInt(u32))) == maxInt(u32));
 }
 
 test "f64_min" {