Bug 1439881 - Avoid returning monitor scale 0; r?stransky draft
authorJan Horak <jhorak@redhat.com>
Thu, 22 Feb 2018 13:59:38 +0100
changeset 758433 59e7937022367c876aee1292dbc15784b3157d78
parent 758314 67d9be602f766a932884bc6e3b139fb36df7d026
push id100053
push userbmo:jhorak@redhat.com
push dateThu, 22 Feb 2018 13:01:16 +0000
reviewersstransky
bugs1439881
milestone60.0a1
Bug 1439881 - Avoid returning monitor scale 0; r?stransky Because of rounding errors there's a change that returned monitor scale is 0. That would lead to SIGFPE because it's later used in division. MozReview-Commit-ID: 4d7nHaBm4XG
widget/gtk/nsNativeThemeGTK.cpp
--- a/widget/gtk/nsNativeThemeGTK.cpp
+++ b/widget/gtk/nsNativeThemeGTK.cpp
@@ -71,17 +71,25 @@ GetMonitorScaleFactor(nsIFrame* aFrame)
   if (scale <= 0) {
     nsIWidget* rootWidget = aFrame->PresContext()->GetRootWidget();
     if (rootWidget) {
         // We need to use GetDefaultScale() despite it returns monitor scale
         // factor multiplied by font scale factor because it is the only scale
         // updated in nsPuppetWidget.
         // Since we don't want to apply font scale factor for UI elements
         // (because GTK does not do so) we need to remove that from returned value.
-        return rootWidget->GetDefaultScale().scale / gfxPlatformGtk::GetFontScaleFactor();
+        // The computed monitor scale factor needs to be rounded before casting to
+        // integer to avoid rounding errors which would lead to returning 0.
+        int monitorScale = int(round(rootWidget->GetDefaultScale().scale
+              / gfxPlatformGtk::GetFontScaleFactor()));
+        if (monitorScale < 1) {
+          NS_WARNING(nsPrintfCString("Invalid monitor scale: %d", monitorScale).get());
+          return 1;
+        }
+        return monitorScale;
     }
   }
   // Use monitor scaling factor where devPixelsPerPx is set
   return ScreenHelperGTK::GetGTKMonitorScaleFactor();
 }
 
 nsNativeThemeGTK::nsNativeThemeGTK()
 {