Skip to content

Commit ab63d1a

Browse files
lenemterdanirabbit
andauthored
DiskBar: Add some safety checks to constraints calculations (#901)
Co-authored-by: Danielle Foré <danielle@elementary.io>
1 parent c834b0d commit ab63d1a

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/Widgets/DiskBar.vala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ public class Installer.DiskBar: Gtk.Box {
116116
);
117117
}
118118

119+
// make sure if somehow used_sectors > total_disk_sectors, we clamp it to total_disk_sectors
120+
used_sectors = uint64.min (used_sectors, total_disk_sectors);
121+
119122
// If more than 1% of the disk is unused, show a block for the unused space
120123
var unused_sectors = total_disk_sectors - used_sectors;
121124
if ((double) unused_sectors / total_disk_sectors > 0.01) {
@@ -147,8 +150,11 @@ public class Installer.DiskBar: Gtk.Box {
147150
}
148151

149152
private void append_partition (Gtk.Widget widget, double percentage) {
150-
// Truncate to 2 decimal places (round down), to ensure we don't go over 100% because of rounding errors
151-
percentage = (int)(percentage * 100) / 100.0;
153+
// Truncate to 2 decimal places (round down), to ensure we don't go over 100% because of rounding errors.
154+
// Also make sure percentage is never 0, otherwise we can assertion error:
155+
// gtk_constraint_expression_new_subject: assertion failed: (!G_APPROX_VALUE (term->coefficient, 0.0, 0.001))
156+
// Also we assume partitions.size is less than 100
157+
percentage = ((int) (percentage * 100) / 100.0).clamp (0.01, 1.0 - partitions.size / 100.0);
152158

153159
widget.set_parent (this);
154160

0 commit comments

Comments
 (0)