src/share/native/sun/misc/MessageUtils.c

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -33,22 +33,34 @@
 
 static void
 printToFile(JNIEnv *env, jstring s, FILE *file)
 {
     char *sConverted;
-    int length;
+    int length = 0;
     int i;
     const jchar *sAsArray;
 
     if (s == NULL) {
       s = (*env)->NewStringUTF(env, "null");
       if (s == NULL) return;
     }
 
     sAsArray = (*env)->GetStringChars(env, s, NULL);
+    if (!sAsArray)
+        return;
     length = (*env)->GetStringLength(env, s);
+    if (length == 0) {
+        (*env)->ReleaseStringChars(env, s, sAsArray);
+        return;
+    }
     sConverted = (char *) malloc(length + 1);
+    if (!sConverted) {
+        (*env)->ReleaseStringChars(env, s, sAsArray);
+        JNU_ThrowOutOfMemoryError(env, NULL);
+        return;
+    }
+
     for(i = 0; i < length; i++) {
         sConverted[i] = (0x7f & sAsArray[i]);
     }
     sConverted[length] = '\0';
     jio_fprintf(file, "%s", sConverted);