8039394: Compiler warnings about C++ exceptions in windows printing code
authorprr
Wed, 21 May 2014 11:12:18 -0700
changeset 25098 bbff0ad63272
parent 25097 147ac5785a84
child 25099 ba8a24b2576f
8039394: Compiler warnings about C++ exceptions in windows printing code Reviewed-by: bae, jgodinez
jdk/src/windows/native/sun/windows/awt_PrintJob.cpp
--- a/jdk/src/windows/native/sun/windows/awt_PrintJob.cpp	Wed May 21 10:50:14 2014 -0700
+++ b/jdk/src/windows/native/sun/windows/awt_PrintJob.cpp	Wed May 21 11:12:18 2014 -0700
@@ -2541,8 +2541,21 @@
      * rounded advances will drift away from the true advance.
      */
     if (glyphPos != NULL && strLen > 0) {
-         xadvances = (int*)SAFE_SIZE_ARRAY_ALLOC(safe_Malloc, strLen, sizeof(int));
-         xyadvances = (int*)SAFE_SIZE_ARRAY_ALLOC(safe_Malloc, strLen, sizeof(int) * 2);
+        try {
+            xadvances = (int*)SAFE_SIZE_ARRAY_ALLOC(safe_Malloc,
+                    strLen, sizeof(int));
+            xyadvances = (int*)SAFE_SIZE_ARRAY_ALLOC(safe_Malloc, strLen,
+                    sizeof(int) * 2);
+        } catch (std::bad_alloc&) {
+            if (xadvances != NULL) {
+                free(xadvances);
+                xadvances = NULL;
+            }
+            if (xyadvances != NULL) {
+                free(xyadvances);
+                xyadvances = NULL;
+            }
+        }
     }
     if (xadvances != NULL && xyadvances != NULL) {
         int *inxAdvances = xadvances;
@@ -2752,8 +2765,12 @@
     if ((imgWidthByteSz % sizeof(DWORD)) != 0)
         padBytes = sizeof(DWORD) - (imgWidthByteSz % sizeof(DWORD));
 
-    jbyte* alignedImage = (jbyte*) SAFE_SIZE_ARRAY_ALLOC(safe_Malloc,
+    jbyte* alignedImage = NULL;
+    try {
+        alignedImage = (jbyte*) SAFE_SIZE_ARRAY_ALLOC(safe_Malloc,
             imgWidthByteSz+padBytes, ROUND_TO_LONG(srcHeight));
+    } catch (std::bad_alloc&) {
+    }
     long newImgSize = (imgWidthByteSz+padBytes) * ROUND_TO_LONG(srcHeight);
 
     if (alignedImage != NULL) {
@@ -3355,36 +3372,6 @@
     // Set page size here.
 }
 
-
-/**
- * Return an array of POINTS describing the paper sizes supported
- * by the driver identified by 'deviceName' and 'portName'.
- * If there is an error, then NULL is returned.
- */
-static POINT *getPaperSizeList(LPCTSTR deviceName, LPCTSTR portName) {
-    DWORD numPaperSizes;
-    POINT *paperSizes = NULL;
-
-    SAVE_CONTROLWORD
-    numPaperSizes = DeviceCapabilities(deviceName, portName,
-                                       DC_PAPERSIZE, NULL, NULL);
-
-    if (numPaperSizes > 0) {
-        paperSizes = (POINT *)SAFE_SIZE_ARRAY_ALLOC(safe_Malloc, sizeof(*paperSizes), numPaperSizes);
-
-        DWORD result = DeviceCapabilities(deviceName, portName,
-                                          DC_PAPERSIZE, (LPTSTR) paperSizes,
-                                          NULL);
-        if (result == -1) {
-            free((char *) paperSizes);
-            paperSizes = NULL;
-        }
-    }
-    RESTORE_CONTROLWORD
-
-    return paperSizes;
-}
-
 static WORD getOrientationFromDevMode2(HGLOBAL hDevMode) {
 
     WORD orient = DMORIENT_PORTRAIT;
@@ -3989,20 +3976,33 @@
     numPaperSizes = (int)DeviceCapabilities(printer, port, DC_PAPERSIZE,
                                             NULL, NULL);
     if (numPaperSizes > 0) {
-        papers = (WORD*)SAFE_SIZE_ARRAY_ALLOC(safe_Malloc, sizeof(WORD), numPaperSizes);
-        paperSizes = (POINT *)SAFE_SIZE_ARRAY_ALLOC(safe_Malloc, sizeof(*paperSizes), numPaperSizes);
-
-        DWORD result1 = DeviceCapabilities(printer, port,
-                                           DC_PAPERS, (LPTSTR) papers, NULL);
-        DWORD result2 = DeviceCapabilities(printer, port,
-                                           DC_PAPERSIZE, (LPTSTR) paperSizes,
-                                           NULL);
-
-        if (result1 == -1 || result2 == -1 ) {
-            free((char *) papers);
-            papers = NULL;
-            free((char *) paperSizes);
-            paperSizes = NULL;
+        try {
+            papers = (WORD*)SAFE_SIZE_ARRAY_ALLOC(safe_Malloc, sizeof(WORD), numPaperSizes);
+            paperSizes = (POINT *)SAFE_SIZE_ARRAY_ALLOC(safe_Malloc, sizeof(*paperSizes), numPaperSizes);
+        } catch (std::bad_alloc&) {
+            if (papers != NULL) {
+                free((char*)papers);
+                papers = NULL;
+            }
+            if (paperSizes != NULL) {
+               free((char *)paperSizes);
+               paperSizes = NULL;
+            }
+        }
+
+        if (papers != NULL && paperSizes != NULL) {
+             DWORD result1 = DeviceCapabilities(printer, port,
+                                                DC_PAPERS, (LPTSTR) papers, NULL);
+            DWORD result2 = DeviceCapabilities(printer, port,
+                                               DC_PAPERSIZE, (LPTSTR) paperSizes,
+                                               NULL);
+
+            if (result1 == -1 || result2 == -1 ) {
+                free((char *) papers);
+                papers = NULL;
+                free((char *) paperSizes);
+                paperSizes = NULL;
+            }
         }
     }