Skip to content

Commit a6a142f

Browse files
committed
C++: fix a bug in which a substraction could overflow
There was an image like this: ```slint height: 40px Image { width: parent.width; height: self.source.height * 1px; source: @image-url("...."); } ``` The `y` propery ended inlined like so: `(40 - the_image.source.get().size().height)/float(2)` but since height was `unsigned` the C++ rules means that the operation happens as unsigned and we have an overflow. and `y` turned out to be very big instead of slightly negative (for image whose height was larger than 40)
1 parent da33ffa commit a6a142f

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

internal/compiler/generator/cpp.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3268,6 +3268,7 @@ fn compile_expression(expr: &llr::Expression, ctx: &EvaluationContext) -> String
32683268
'&' => "&&",
32693269
'|' => "||",
32703270
'/' => "/(float)",
3271+
'-' => "-(float)", // conversion to float to avoid overflow between unsigned
32713272
_ => op.encode_utf8(&mut buffer),
32723273
},
32733274
)

tests/cases/elements/image.slint

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ TestCase := Rectangle {
2121

2222
property <length> img_width: img.width;
2323
property <length> img_height: img.height;
24+
in-out property <float> test_no_overflow: (21 - img.source.width) / 2; // (21 - 320)/2 = -149.5
2425
property <bool> test: img2.source-clip-height * 1px == img2.height && img2.source-clip-width * 1px == img2.width &&
25-
img2.width/1px == img2.source.width - 20 && img3.source.width == 0 && img3.source.height == 0;
26+
img2.width/1px == img2.source.width - 20 && img3.source.width == 0 && img3.source.height == 0 && test_no_overflow == -149.5;
2627
}
2728

2829
/*

0 commit comments

Comments
 (0)