From d4cfe15965a27fe950bee2f07c79a0376cdb80ec Mon Sep 17 00:00:00 2001
From: Marek Gradzki <mgradzki@cisco.com>
Date: Thu, 15 Dec 2016 07:30:09 +0100
Subject: Fix NPE in JNI while handling int arrays, VPP-559

Change-Id: Idc3760b65e62cfa5d37dd7379e35331b7f95f913
Signed-off-by: Marek Gradzki <mgradzki@cisco.com>
---
 vpp-api/java/jvpp/gen/jvppgen/jni_gen.py | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

(limited to 'vpp-api/java/jvpp')

diff --git a/vpp-api/java/jvpp/gen/jvppgen/jni_gen.py b/vpp-api/java/jvpp/gen/jvppgen/jni_gen.py
index eee8660a2b0..328cc8d35d2 100644
--- a/vpp-api/java/jvpp/gen/jvppgen/jni_gen.py
+++ b/vpp-api/java/jvpp/gen/jvppgen/jni_gen.py
@@ -198,42 +198,42 @@ u8_array_struct_setter_template = Template("""
 """)
 
 u16_array_struct_setter_template = Template("""
-    jshort * ${field_reference_name}ArrayElements = (*env)->GetShortArrayElements(env, ${field_reference_name}, NULL);
     if (${field_reference_name}) {
+        jshort * ${field_reference_name}ArrayElements = (*env)->GetShortArrayElements(env, ${field_reference_name}, NULL);
         size_t _i;
         jsize cnt = (*env)->GetArrayLength (env, ${field_reference_name});
         ${field_length_check}
         for (_i = 0; _i < cnt; _i++) {
             mp->${c_name}[_i] = clib_host_to_net_u16(${field_reference_name}ArrayElements[_i]);
         }
+        (*env)->ReleaseShortArrayElements (env, ${field_reference_name}, ${field_reference_name}ArrayElements, 0);
     }
-    (*env)->ReleaseShortArrayElements (env, ${field_reference_name}, ${field_reference_name}ArrayElements, 0);
     """)
 
 u32_array_struct_setter_template = Template("""
-    jint * ${field_reference_name}ArrayElements = (*env)->GetIntArrayElements(env, ${field_reference_name}, NULL);
     if (${field_reference_name}) {
+        jint * ${field_reference_name}ArrayElements = (*env)->GetIntArrayElements(env, ${field_reference_name}, NULL);
         size_t _i;
         jsize cnt = (*env)->GetArrayLength (env, ${field_reference_name});
         ${field_length_check}
         for (_i = 0; _i < cnt; _i++) {
             mp->${c_name}[_i] = clib_host_to_net_u32(${field_reference_name}ArrayElements[_i]);
         }
+        (*env)->ReleaseIntArrayElements (env, ${field_reference_name}, ${field_reference_name}ArrayElements, 0);
     }
-    (*env)->ReleaseIntArrayElements (env, ${field_reference_name}, ${field_reference_name}ArrayElements, 0);
     """)
 
 u64_array_struct_setter_template = Template("""
-    jlong * ${field_reference_name}ArrayElements = (*env)->GetLongArrayElements(env, ${field_reference_name}, NULL);
     if (${field_reference_name}) {
+        jlong * ${field_reference_name}ArrayElements = (*env)->GetLongArrayElements(env, ${field_reference_name}, NULL);
         size_t _i;
         jsize cnt = (*env)->GetArrayLength (env, ${field_reference_name});
         ${field_length_check}
         for (_i = 0; _i < cnt; _i++) {
             mp->${c_name}[_i] = clib_host_to_net_u64(${field_reference_name}ArrayElements[_i]);
         }
+        (*env)->ReleaseLongArrayElements (env, ${field_reference_name}, ${field_reference_name}ArrayElements, 0);
     }
-    (*env)->ReleaseLongArrayElements (env, ${field_reference_name}, ${field_reference_name}ArrayElements, 0);
     """)
 
 struct_setter_templates = {'u8': u8_struct_setter_template,
-- 
cgit