Bug 543644 - Device#getScreenDPI(): division by zero leads to very high DPI
Summary: Device#getScreenDPI(): division by zero leads to very high DPI
Status: VERIFIED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 4.10   Edit
Hardware: PC Linux
: P3 major (vote)
Target Milestone: 4.11 M3   Edit
Assignee: Eric Williams CLA
QA Contact:
URL:
Whiteboard:
Keywords: triaged
Depends on:
Blocks:
 
Reported: 2019-01-21 07:43 EST by Andreas Loth CLA
Modified: 2019-02-19 09:30 EST (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andreas Loth CLA 2019-01-21 07:43:59 EST
org.eclipse.swt.graphics.Device#getScreenDPI()

Point getScreenDPI () {
    int dpi = 96; //default value
    if (GTK.GTK_VERSION >= OS.VERSION(3, 22, 0)) {
        long /*int*/ display = GDK.gdk_display_get_default();
        long /*int*/ pMonitor = GDK.gdk_display_get_primary_monitor(display);
        if (pMonitor == 0) {
            pMonitor = GDK.gdk_display_get_monitor(display, 0);
        }
        int widthMM = GDK.gdk_monitor_get_width_mm(pMonitor); // returns 0
        int scaleFactor = GDK.gdk_monitor_get_scale_factor(pMonitor);
        GdkRectangle monitorGeometry = new GdkRectangle ();
        GDK.gdk_monitor_get_geometry(pMonitor, monitorGeometry);
        dpi = Compatibility.round (254 * monitorGeometry.width * scaleFactor, widthMM * 10); // = 2147483647
    }
    return new Point (dpi, dpi);
}


Monitors report that their size is 0 mm x 0 mm (some monitors do this, TigerVNC does not transmit the monitors physical size, resulting in 0 mm x 0 mm).

This causes GDK.gdk_monitor_get_width_mm(pMonitor) to return 0, which leads to widthMM == 0.
The method Compatibility.round requires the second parameter to be non-zero according to its Javadoc. 0 is passed in this situation.
This leads to a floating point division by zero. After casting this value to an int, Integer.MAX_VALUE is returned as dpi value.

Proposed fix:
Check widthMM for zero (or even negative values?) and return the default DPI in this case.

The TigerVNC project even said that using the physical size of a monitor is a problem itself and just the scale factor should be used:
https://github.com/TigerVNC/tigervnc/issues/789
Comment 1 Eric Williams CLA 2019-01-21 10:19:27 EST
Thanks for the bug report, we'll take a look.
Comment 2 Eclipse Genie CLA 2019-01-21 10:30:23 EST
New Gerrit change created: https://git.eclipse.org/r/135480
Comment 4 Eric Williams CLA 2019-01-21 10:51:10 EST
(In reply to Eclipse Genie from comment #3)
> Gerrit change https://git.eclipse.org/r/135480 was merged to [master].
> Commit:
> http://git.eclipse.org/c/platform/eclipse.platform.swt.git/commit/
> ?id=5970d5d79cf7605de4826347008adda9cf67a2ef

In master now.
Comment 5 Eric Williams CLA 2019-02-19 09:30:40 EST
Verified in I20190219-0600.