From b805e8bdde5d57a967e6840b541f4cd6d41fb9d8 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 29 Mar 2014 16:53:05 +0000 Subject: [PATCH] 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. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_3_0_BRANCH@76214 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + src/msw/utilsgui.cpp | 13 +++++-------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index e01c1c7524..d227b04427 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -652,6 +652,7 @@ wxMSW: - Fix wxExecute() keeping open too many thread handles for too long (dannchr). - Fix clicking on togglable columns in wxDataViewCtrl (Laurent Poujoulat). - Fix expander in non left-most position in wxDataViewCtrl (Laurent Poujoulat). +- Don't fail when using large paper sizes in print preview. wxOSX: diff --git a/src/msw/utilsgui.cpp b/src/msw/utilsgui.cpp index a174c666b5..1b8ee4a8da 100644 --- a/src/msw/utilsgui.cpp +++ b/src/msw/utilsgui.cpp @@ -257,10 +257,9 @@ void PixelToHIMETRIC(LONG *x, LONG *y, HDC hdcRef) iWidthPels = GetDeviceCaps(hdcRef, HORZRES), iHeightPels = GetDeviceCaps(hdcRef, VERTRES); - *x *= (iWidthMM * 100); - *x /= iWidthPels; - *y *= (iHeightMM * 100); - *y /= iHeightPels; + // Take care to use MulDiv() here to avoid overflow. + *x = ::MulDiv(*x, iWidthMM * 100, iWidthPels); + *y = ::MulDiv(*y, iHeightMM * 100, iHeightPels); } void HIMETRICToPixel(LONG *x, LONG *y, HDC hdcRef) @@ -270,10 +269,8 @@ void HIMETRICToPixel(LONG *x, LONG *y, HDC hdcRef) iWidthPels = GetDeviceCaps(hdcRef, HORZRES), iHeightPels = GetDeviceCaps(hdcRef, VERTRES); - *x *= iWidthPels; - *x /= (iWidthMM * 100); - *y *= iHeightPels; - *y /= (iHeightMM * 100); + *x = ::MulDiv(*x, iWidthPels, iWidthMM * 100); + *y = ::MulDiv(*y, iHeightPels, iHeightMM * 100); } void HIMETRICToPixel(LONG *x, LONG *y)