Opened 7 months ago

Closed 7 months ago

Last modified 7 months ago

#16138 closed defect (fixed)

Large paper sizes can trigger long overflows in PixelToHIMETRIC calculation for print preview

Reported by: eco Owned by: vadz
Priority: normal Milestone:
Component: wxMSW Version: 3.0.0
Keywords: Cc:
Blocked By: Blocking:
Patch: no

Description

Upgraded to wxWidgets 3.0 from wxWidgets 2.8 and the new enhmetafile print preview triggers this bug when using large paper sizes. It manifests by a dialog box saying "Sorry, not enough memory to create a preview." when the user tries to view a print preview.

For reference here's PixelToHIMETRIC:

void PixelToHIMETRIC(LONG *x, LONG *y, HDC hdcRef)
{
    int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE),
        iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE),
        iWidthPels = GetDeviceCaps(hdcRef, HORZRES),
        iHeightPels = GetDeviceCaps(hdcRef, VERTRES);

    *x *= (iWidthMM * 100);
    *x /= iWidthPels;
    *y *= (iHeightMM * 100);
    *y /= iHeightPels;
}

Here are some real world values for printing to an ARCH E (36" × 48" or 914mm × 1219mm) on a 44-inch printer:

*x at entry ==  28567
*y at entry ==  21367

iHeightMM   ==  905
iWidthMM    ==  1209
iWidthPels  ==  28567
iHeightPels ==  21367

*x at exit  ==  9549
*y at exit  == -50888

I would include a patch but I looked at wxLongLong and wasn't really sure how to use it for multiplication/division on non-native long long platforms (not sure if that's even necessary since this is msw specific but I'm completely unfamiliar with Windows CE).

Change History (4)

comment:1 Changed 7 months ago by vadz

  • Owner set to vadz
  • Status changed from new to accepted

I think there is no need to use long long here (although we could, too), there is already a function to do what we need in Windows, so I'm just going to use it here. Please test and reopen if it still doesn't work after the fix I'm going to commit soon, TIA!

comment:2 Changed 7 months ago by VZ

  • Resolution set to fixed
  • Status changed from accepted to closed

(In [76214]) Avoid overflows when calculating metafile coordinates in wxMSW.

Use MulDiv() instead of naive multiplication followed by division as this
could overflow, and did when large paper sizes were used in print preview.

Closes #16138.

comment:3 Changed 7 months ago by VZ

(In [76219]) Avoid overflows when calculating metafile coordinates in wxMSW.

Use MulDiv() instead of naive multiplication followed by division as this
could overflow, and did when large paper sizes were used in print preview.

Closes #16138.

comment:4 Changed 7 months ago by eco

The fix works. Thank you.

Note: See TracTickets for help on using tickets.