Improve JNI code generation (array truncation)
Fixes DEADCODE coverity issue:
size_t max_size = 0;
if (max_size != 0 && ...)
Change-Id: I0e3ea4e4ddddb476519490c6f00208379089397c
Signed-off-by: Marek Gradzki <mgradzki@cisco.com>
diff --git a/vpp-api/java/jvpp/gen/jvppgen/jvpp_c_gen.py b/vpp-api/java/jvpp/gen/jvppgen/jvpp_c_gen.py
index ef07709..d63acd6 100644
--- a/vpp-api/java/jvpp/gen/jvppgen/jvpp_c_gen.py
+++ b/vpp-api/java/jvpp/gen/jvppgen/jvpp_c_gen.py
@@ -124,11 +124,14 @@
u64_struct_setter_template = Template("""
mp->${c_name} = clib_host_to_net_u64(${java_name});""")
+fixed_array_length_enforcement_template = Template("""
+ size_t max_size = ${field_length};
+ if (cnt > max_size) cnt = max_size;""")
+
u8_array_struct_setter_template = Template("""
if (${java_name}) {
jsize cnt = (*env)->GetArrayLength (env, ${java_name});
- size_t max_size = ${field_length};
- if (max_size != 0 && cnt > max_size) cnt = max_size;
+ ${field_length_check}
(*env)->GetByteArrayRegion(env, ${java_name}, 0, cnt, (jbyte *)mp->${c_name});
}
""")
@@ -138,8 +141,7 @@
if (${java_name}) {
size_t _i;
jsize cnt = (*env)->GetArrayLength (env, ${java_name});
- size_t max_size = ${field_length};
- if (max_size != 0 && cnt > max_size) cnt = max_size;
+ ${field_length_check}
for (_i = 0; _i < cnt; _i++) {
mp->${c_name}[_i] = clib_host_to_net_u16(${java_name}ArrayElements[_i]);
}
@@ -152,8 +154,7 @@
if (${java_name}) {
size_t _i;
jsize cnt = (*env)->GetArrayLength (env, ${java_name});
- size_t max_size = ${field_length};
- if (max_size != 0 && cnt > max_size) cnt = max_size;
+ ${field_length_check}
for (_i = 0; _i < cnt; _i++) {
mp->${c_name}[_i] = clib_host_to_net_u32(${java_name}ArrayElements[_i]);
}
@@ -166,8 +167,7 @@
if (${java_name}) {
size_t _i;
jsize cnt = (*env)->GetArrayLength (env, ${java_name});
- size_t max_size = ${field_length};
- if (max_size != 0 && cnt > max_size) cnt = max_size;
+ ${field_length_check}
for (_i = 0; _i < cnt; _i++) {
mp->${c_name}[_i] = clib_host_to_net_u64(${java_name}ArrayElements[_i]);
}
@@ -264,11 +264,16 @@
c_type = t[0]
c_name = t[1]
field_length = t[2][0]
+ field_length_check = ""
# check if we are processing variable length array:
if t[2][1]:
field_length = util.underscore_to_camelcase(t[2][0])
+ # enforce max length if array has fixed length or uses variable length syntax
+ if str(t[2][0]) != "0":
+ field_length_check = fixed_array_length_enforcement_template.substitute(field_length=field_length)
+
java_field_name = util.underscore_to_camelcase(c_name)
struct_setter_template = struct_setter_templates[c_type]
@@ -276,7 +281,7 @@
struct_setters += struct_setter_template.substitute(
c_name=c_name,
java_name=java_field_name,
- field_length=field_length)
+ field_length_check=field_length_check)
jni_impl.append(jni_impl_template.substitute(
inputfile=inputfile,