From ddd7ce624abe2883257e9c2814fdf64ae18b204f Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 29 Mar 2014 16:53:57 +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/trunk@76219 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/msw/utilsgui.cpp | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) 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)