From 2910ca354c741cded6656bf15b4d5216d4417ca0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Fri, 4 Nov 2022 17:42:52 +0100 Subject: [PATCH] wayland: use fp math all the way when calculating DPI MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This fixes an FPE when the monitor’s physical width/height is so small that the conversion from mm to inch resulted in inches being zero. --- CHANGELOG.md | 2 ++ wayland.c | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3546d92e..9c41f21a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -94,10 +94,12 @@ * Background transparency being applied to the text "behind" the cursor. Only applies to block cursor using inversed fg/bg colors. ([#1205][1205]). +* Crash when monitor’s physical size is "too small" ([#1209][1209]). [1173]: https://codeberg.org/dnkl/foot/issues/1173 [1190]: https://codeberg.org/dnkl/foot/issues/1190 [1205]: https://codeberg.org/dnkl/foot/issues/1205 +[1209]: https://codeberg.org/dnkl/foot/issues/1209 ### Security diff --git a/wayland.c b/wayland.c index fb103ad1..974dfb39 100644 --- a/wayland.c +++ b/wayland.c @@ -370,8 +370,8 @@ output_update_ppi(struct monitor *mon) if (mon->dim.mm.width <= 0 || mon->dim.mm.height <= 0) return; - int x_inches = mon->dim.mm.width * 0.03937008; - int y_inches = mon->dim.mm.height * 0.03937008; + double x_inches = mon->dim.mm.width * 0.03937008; + double y_inches = mon->dim.mm.height * 0.03937008; mon->ppi.real.x = mon->dim.px_real.width / x_inches; mon->ppi.real.y = mon->dim.px_real.height / y_inches; @@ -407,7 +407,7 @@ output_update_ppi(struct monitor *mon) mon->ppi.scaled.x = scaled_width / x_inches; mon->ppi.scaled.y = scaled_height / y_inches; - float px_diag = sqrt(pow(scaled_width, 2) + pow(scaled_height, 2)); + double px_diag = sqrt(pow(scaled_width, 2) + pow(scaled_height, 2)); mon->dpi = px_diag / mon->inch * mon->scale; }