diff options
author | Frediano Ziglio <freddy77@gmail.com> | 2020-09-18 08:49:02 +0100 |
---|---|---|
committer | Frediano Ziglio <freddy77@gmail.com> | 2021-02-22 09:11:35 +0000 |
commit | 6b662331f7bef5d9058029885184ea1f15e9ef0d (patch) | |
tree | 075a7fca6c805611618114e4b48dc72f8c7d500f /python_modules | |
parent | bd15c96f545b100c1c872cf57b45fa0b54481cc9 (diff) |
codegen: Handle zero_terminated attribute in demashaller
Make sure the output array is zero terminated to simplify
code using the output structures.
Changed in generated code:
diff -ru gen/generated_client_demarshallers.c common/generated_client_demarshallers.c
--- gen/generated_client_demarshallers.c 2021-02-21 15:13:42.004307087 +0000
+++ common/generated_client_demarshallers.c 2021-02-21 15:13:58.916513426 +0000
@@ -565,15 +565,24 @@
return NULL;
}
-static uint8_t * parse_array_uint8(uint8_t *message_start, SPICE_GNUC_UNUSED uint8_t *message_end, uint8_t *struct_data, PointerInfo *this_ptr_info)
+static uint8_t * parse_array_uint8_terminated(uint8_t *message_start, SPICE_GNUC_UNUSED uint8_t *message_end, uint8_t *struct_data, PointerInfo *this_ptr_info)
{
uint8_t *in = message_start + this_ptr_info->offset;
uint8_t *end;
end = struct_data;
memcpy(end, in, this_ptr_info->nelements);
+#if defined(__GNUC__)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wstringop-overflow"
+#endif
+ ((char *) (end))[this_ptr_info->nelements] = 0;
+#if defined(__GNUC__)
+#pragma GCC diagnostic pop
+#endif
in += this_ptr_info->nelements;
end += this_ptr_info->nelements;
+ end += 1;
return end;
}
@@ -622,7 +631,8 @@
dst_info_host_data__array__nelements = host_size__value;
dst_info_host_data__array__nw_size = dst_info_host_data__array__nelements;
- dst_info_host_data__array__mem_size = sizeof(uint8_t) * dst_info_host_data__array__nelements;
+ dst_info_host_data__array__mem_size = sizeof(uint8_t) * dst_info_host_data__array__nelements + sizeof(uint8_t);
+ dst_info_host_data__array__mem_size = SPICE_ALIGN(dst_info_host_data__array__mem_size, 4);
if (SPICE_UNLIKELY(dst_info_host_data__array__nw_size > (uintptr_t) (message_end - message_start - host_data__value))) {
goto error;
}
@@ -650,7 +660,8 @@
dst_info_cert_subject_data__array__nelements = cert_subject_size__value;
dst_info_cert_subject_data__array__nw_size = dst_info_cert_subject_data__array__nelements;
- dst_info_cert_subject_data__array__mem_size = sizeof(uint8_t) * dst_info_cert_subject_data__array__nelements;
+ dst_info_cert_subject_data__array__mem_size = sizeof(uint8_t) * dst_info_cert_subject_data__array__nelements + sizeof(uint8_t);
+ dst_info_cert_subject_data__array__mem_size = SPICE_ALIGN(dst_info_cert_subject_data__array__mem_size, 4);
if (SPICE_UNLIKELY(dst_info_cert_subject_data__array__nw_size > (uintptr_t) (message_end - message_start - cert_subject_data__value))) {
goto error;
}
@@ -685,14 +696,14 @@
out->dst_info.sport = consume_uint16(&in);
out->dst_info.host_size = consume_uint32(&in);
ptr_info[n_ptr].offset = consume_uint32(&in);
- ptr_info[n_ptr].parse = parse_array_uint8;
+ ptr_info[n_ptr].parse = parse_array_uint8_terminated;
ptr_info[n_ptr].dest = (void **)&out->dst_info.host_data;
host_data__array__nelements = out->dst_info.host_size;
ptr_info[n_ptr].nelements = host_data__array__nelements;
n_ptr++;
out->dst_info.cert_subject_size = consume_uint32(&in);
ptr_info[n_ptr].offset = consume_uint32(&in);
- ptr_info[n_ptr].parse = parse_array_uint8;
+ ptr_info[n_ptr].parse = parse_array_uint8_terminated;
ptr_info[n_ptr].dest = (void **)&out->dst_info.cert_subject_data;
cert_subject_data__array__nelements = out->dst_info.cert_subject_size;
ptr_info[n_ptr].nelements = cert_subject_data__array__nelements;
@@ -1050,7 +1061,8 @@
host_data__array__nelements = host_size__value;
host_data__array__nw_size = host_data__array__nelements;
- host_data__array__mem_size = sizeof(uint8_t) * host_data__array__nelements;
+ host_data__array__mem_size = sizeof(uint8_t) * host_data__array__nelements + sizeof(uint8_t);
+ host_data__array__mem_size = SPICE_ALIGN(host_data__array__mem_size, 4);
if (SPICE_UNLIKELY(host_data__array__nw_size > (uintptr_t) (message_end - message_start - host_data__value))) {
goto error;
}
@@ -1078,7 +1090,8 @@
cert_subject_data__array__nelements = cert_subject_size__value;
cert_subject_data__array__nw_size = cert_subject_data__array__nelements;
- cert_subject_data__array__mem_size = sizeof(uint8_t) * cert_subject_data__array__nelements;
+ cert_subject_data__array__mem_size = sizeof(uint8_t) * cert_subject_data__array__nelements + sizeof(uint8_t);
+ cert_subject_data__array__mem_size = SPICE_ALIGN(cert_subject_data__array__mem_size, 4);
if (SPICE_UNLIKELY(cert_subject_data__array__nw_size > (uintptr_t) (message_end - message_start - cert_subject_data__value))) {
goto error;
}
@@ -1107,13 +1120,13 @@
out->sport = consume_uint16(&in);
out->host_size = consume_uint32(&in);
ptr_info[n_ptr].offset = consume_uint32(&in);
- ptr_info[n_ptr].parse = parse_array_uint8;
+ ptr_info[n_ptr].parse = parse_array_uint8_terminated;
ptr_info[n_ptr].dest = (void **)&out->host_data;
ptr_info[n_ptr].nelements = host_data__array__nelements;
n_ptr++;
out->cert_subject_size = consume_uint32(&in);
ptr_info[n_ptr].offset = consume_uint32(&in);
- ptr_info[n_ptr].parse = parse_array_uint8;
+ ptr_info[n_ptr].parse = parse_array_uint8_terminated;
ptr_info[n_ptr].dest = (void **)&out->cert_subject_data;
ptr_info[n_ptr].nelements = cert_subject_data__array__nelements;
n_ptr++;
@@ -1338,7 +1351,8 @@
dst_info_host_data__array__nelements = host_size__value;
dst_info_host_data__array__nw_size = dst_info_host_data__array__nelements;
- dst_info_host_data__array__mem_size = sizeof(uint8_t) * dst_info_host_data__array__nelements;
+ dst_info_host_data__array__mem_size = sizeof(uint8_t) * dst_info_host_data__array__nelements + sizeof(uint8_t);
+ dst_info_host_data__array__mem_size = SPICE_ALIGN(dst_info_host_data__array__mem_size, 4);
if (SPICE_UNLIKELY(dst_info_host_data__array__nw_size > (uintptr_t) (message_end - message_start - host_data__value))) {
goto error;
}
@@ -1366,7 +1380,8 @@
dst_info_cert_subject_data__array__nelements = cert_subject_size__value;
dst_info_cert_subject_data__array__nw_size = dst_info_cert_subject_data__array__nelements;
- dst_info_cert_subject_data__array__mem_size = sizeof(uint8_t) * dst_info_cert_subject_data__array__nelements;
+ dst_info_cert_subject_data__array__mem_size = sizeof(uint8_t) * dst_info_cert_subject_data__array__nelements + sizeof(uint8_t);
+ dst_info_cert_subject_data__array__mem_size = SPICE_ALIGN(dst_info_cert_subject_data__array__mem_size, 4);
if (SPICE_UNLIKELY(dst_info_cert_subject_data__array__nw_size > (uintptr_t) (message_end - message_start - cert_subject_data__value))) {
goto error;
}
@@ -1401,14 +1416,14 @@
out->dst_info.sport = consume_uint16(&in);
out->dst_info.host_size = consume_uint32(&in);
ptr_info[n_ptr].offset = consume_uint32(&in);
- ptr_info[n_ptr].parse = parse_array_uint8;
+ ptr_info[n_ptr].parse = parse_array_uint8_terminated;
ptr_info[n_ptr].dest = (void **)&out->dst_info.host_data;
host_data__array__nelements = out->dst_info.host_size;
ptr_info[n_ptr].nelements = host_data__array__nelements;
n_ptr++;
out->dst_info.cert_subject_size = consume_uint32(&in);
ptr_info[n_ptr].offset = consume_uint32(&in);
- ptr_info[n_ptr].parse = parse_array_uint8;
+ ptr_info[n_ptr].parse = parse_array_uint8_terminated;
ptr_info[n_ptr].dest = (void **)&out->dst_info.cert_subject_data;
cert_subject_data__array__nelements = out->dst_info.cert_subject_size;
ptr_info[n_ptr].nelements = cert_subject_data__array__nelements;
@@ -7582,7 +7597,8 @@
name__array__nelements = name_size__value;
name__array__nw_size = name__array__nelements;
- name__array__mem_size = sizeof(uint8_t) * name__array__nelements;
+ name__array__mem_size = sizeof(uint8_t) * name__array__nelements + sizeof(uint8_t);
+ name__array__mem_size = SPICE_ALIGN(name__array__mem_size, 4);
if (SPICE_UNLIKELY(name__array__nw_size > (uintptr_t) (message_end - message_start - name__value))) {
goto error;
}
@@ -7609,7 +7625,7 @@
out->name_size = consume_uint32(&in);
ptr_info[n_ptr].offset = consume_uint32(&in);
- ptr_info[n_ptr].parse = parse_array_uint8;
+ ptr_info[n_ptr].parse = parse_array_uint8_terminated;
ptr_info[n_ptr].dest = (void **)&out->name;
ptr_info[n_ptr].nelements = name__array__nelements;
n_ptr++;
Signed-off-by: Frediano Ziglio <freddy77@gmail.com>
Acked-by: Victor Toso <victortoso@redhat.com>
Diffstat (limited to 'python_modules')
-rw-r--r-- | python_modules/demarshal.py | 27 | ||||
-rw-r--r-- | python_modules/ptypes.py | 2 |
2 files changed, 28 insertions, 1 deletions
diff --git a/python_modules/demarshal.py b/python_modules/demarshal.py index efc4c99..2a111be 100644 --- a/python_modules/demarshal.py +++ b/python_modules/demarshal.py @@ -384,6 +384,10 @@ def write_validate_array_item(writer, container, item, scope, parent_scope, star # TODO: Overflow check the multiplication if array.has_attr("ptr_array"): writer.assign(mem_size, "sizeof(void *) + SPICE_ALIGN(%s * %s, 4)" % (element_type.sizeof(), nelements)) + elif array.has_attr('zero_terminated'): + # don't use +1 here to avoid possible integer overflow or suboptimizations + writer.assign(mem_size, "%s * %s + %s" % (element_type.sizeof(), nelements, element_type.sizeof())) + writer.assign(mem_size, "SPICE_ALIGN(%s, 4)" % mem_size); else: writer.assign(mem_size, "%s * %s" % (element_type.sizeof(), nelements)) want_mem_size = False @@ -724,7 +728,10 @@ def write_switch_parser(writer, container, switch, dest, scope): def write_parse_ptr_function(writer, target_type): if target_type.is_array(): - parse_function = "parse_array_%s" % target_type.element_type.primitive_type() + if target_type.has_attr("zero_terminated"): + parse_function = "parse_array_%s_terminated" % target_type.element_type.primitive_type() + else: + parse_function = "parse_array_%s" % target_type.element_type.primitive_type() else: parse_function = "parse_struct_%s" % target_type.c_type() if writer.is_generated("parser", parse_function): @@ -787,10 +794,28 @@ def write_array_parser(writer, member, nelements, array, dest, scope): if array.has_attr("ptr_array"): raise Exception("Attribute ptr_array not supported for arrays of int8/uint8") writer.statement("memcpy(%s, in, %s)" % (array_start, nelements)) + if array.has_attr("zero_terminated"): + indentation = writer.indentation + writer.indentation = 0; + writer.writeln("#if defined(__GNUC__)") + writer.writeln("#pragma GCC diagnostic push") + writer.writeln("#pragma GCC diagnostic ignored \"-Wstringop-overflow\"") + writer.writeln("#endif") + writer.indentation = indentation; + writer.assign("((char *) (%s))[%s]" % (array_start, nelements), 0) + writer.indentation = 0; + writer.writeln("#if defined(__GNUC__)") + writer.writeln("#pragma GCC diagnostic pop") + writer.writeln("#endif") + writer.indentation = indentation; writer.increment("in", nelements) if at_end: writer.increment("end", nelements) + if array.has_attr("zero_terminated"): + writer.increment("end", 1) else: + if array.has_attr("zero_terminated"): + raise Exception("Attribute zero_terminated specified for wrong array") with writer.index() as index: if member: array_pos = "%s[%s]" % (array_start, index) diff --git a/python_modules/ptypes.py b/python_modules/ptypes.py index e3eb8fd..d68dc67 100644 --- a/python_modules/ptypes.py +++ b/python_modules/ptypes.py @@ -553,6 +553,8 @@ class ArrayType(Type): return writer.writeln('%s *%s[0];' % (self.c_type(), name)) if member.has_end_attr(): return writer.writeln('%s %s[0];' % (self.c_type(), name)) + if self.is_constant_length() and self.has_attr("zero_terminated"): + return writer.writeln('%s %s[%s];' % (self.c_type(), name, self.size + 1)) if self.is_constant_length(): return writer.writeln('%s %s[%s];' % (self.c_type(), name, self.size)) if self.is_identifier_length(): |