changeset 7232:7522a66a366a

8043720: (smartcardio) Native memory should be handled more accurately Reviewed-by: valeriep
author igerasim
date Sat, 31 May 2014 01:07:43 +0400
parents 2680adbe147a
children bc7f9d966c1d
files src/share/native/sun/security/smartcardio/pcsc.c
diffstat 1 files changed, 25 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/native/sun/security/smartcardio/pcsc.c	Sat May 31 01:03:35 2014 +0400
+++ b/src/share/native/sun/security/smartcardio/pcsc.c	Sat May 31 01:07:43 2014 +0400
@@ -125,7 +125,7 @@
 jobjectArray pcsc_multi2jstring(JNIEnv *env, char *spec) {
     jobjectArray result;
     jclass stringClass;
-    char *cp, **tab;
+    char *cp, **tab = NULL;
     jstring js;
     int cnt = 0;
 
@@ -179,7 +179,7 @@
 {
     SCARDCONTEXT context = (SCARDCONTEXT)jContext;
     LONG rv;
-    LPTSTR mszReaders;
+    LPTSTR mszReaders = NULL;
     DWORD size = 0;
     jobjectArray result;
 
@@ -190,19 +190,21 @@
     }
     dprintf1("-size: %d\n", size);
 
-    mszReaders = malloc(size);
-    if (mszReaders == NULL) {
-        throwOutOfMemoryError(env, NULL);
-        return NULL;
+    if (size) {
+        mszReaders = malloc(size);
+        if (mszReaders == NULL) {
+            throwOutOfMemoryError(env, NULL);
+            return NULL;
+        }
+
+        rv = CALL_SCardListReaders(context, NULL, mszReaders, &size);
+        if (handleRV(env, rv)) {
+            free(mszReaders);
+            return NULL;
+        }
+        dprintf1("-String: %s\n", mszReaders);
     }
 
-    rv = CALL_SCardListReaders(context, NULL, mszReaders, &size);
-    if (handleRV(env, rv)) {
-        free(mszReaders);
-        return NULL;
-    }
-    dprintf1("-String: %s\n", mszReaders);
-
     result = pcsc_multi2jstring(env, mszReaders);
     free(mszReaders);
     return result;
@@ -336,7 +338,7 @@
     const char *readerName;
 
     readerState = calloc(readers, sizeof(SCARD_READERSTATE));
-    if (readerState == NULL) {
+    if (readerState == NULL && readers > 0) {
         throwOutOfMemoryError(env, NULL);
         return NULL;
     }
@@ -348,6 +350,10 @@
     }
 
     for (i = 0; i < readers; i++) {
+        readerState[i].szReader = NULL;
+    }
+
+    for (i = 0; i < readers; i++) {
         jobject jReaderName = (*env)->GetObjectArrayElement(env, jReaderNames, i);
         if ((*env)->ExceptionCheck(env)) {
             goto cleanup;
@@ -369,9 +375,11 @@
         (*env)->DeleteLocalRef(env, jReaderName);
     }
 
-    rv = CALL_SCardGetStatusChange(context, (DWORD)jTimeout, readerState, readers);
-    if (handleRV(env, rv)) {
-        goto cleanup;
+    if (readers > 0) {
+        rv = CALL_SCardGetStatusChange(context, (DWORD)jTimeout, readerState, readers);
+        if (handleRV(env, rv)) {
+            goto cleanup;
+        }
     }
 
     jEventState = (*env)->NewIntArray(env, readers);