aboutsummaryrefslogtreecommitdiffstats
path: root/vpp-api/java/jvpp/gen/jvppgen
diff options
context:
space:
mode:
authorMarek Gradzki <mgradzki@cisco.com>2016-10-24 10:34:16 +0200
committerDamjan Marion <dmarion.lists@gmail.com>2016-10-25 15:24:06 +0000
commit5b3808ed633248fd304b9790c719665081ba7a2b (patch)
tree73ed84ddd2c3a7ace77dac7556099ef589ee2804 /vpp-api/java/jvpp/gen/jvppgen
parent948e00ff5eded0806fc7f54e8f3943aade2bd14a (diff)
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>
Diffstat (limited to 'vpp-api/java/jvpp/gen/jvppgen')
-rw-r--r--vpp-api/java/jvpp/gen/jvppgen/jvpp_c_gen.py23
1 files changed, 14 insertions, 9 deletions
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 ef077090467..d63acd6965f 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 @@ i32_struct_setter_template = Template("""
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 @@ u16_array_struct_setter_template = Template("""
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 @@ u32_array_struct_setter_template = Template("""
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 @@ u64_array_struct_setter_template = Template("""
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 @@ def generate_jni_impl(func_list, plugin_name, inputfile):
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 @@ def generate_jni_impl(func_list, plugin_name, inputfile):
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,