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
--- 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()
{