Skip to content

Commit

Permalink
[#616] Fix java generator to check parameters in packed write
Browse files Browse the repository at this point in the history
  • Loading branch information
Mi-La committed May 31, 2024
1 parent c42d5c8 commit a2074dc
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 12 deletions.
27 changes: 16 additions & 11 deletions compiler/extensions/java/freemarker/CompoundField.inc.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,8 @@ ${I}<@field_member_name field/> = new ${field.typeInfo.typeFullName}(in<#rt>
</#macro>


<#macro compound_write_field_offset_check field compoundName indent>
<#macro compound_check_offset_field field compoundName indent>
<#local I>${""?left_pad(indent * 4)}</#local>
${I}out.alignTo(java.lang.Byte.SIZE);
${I}if (out.getBytePosition() != <@compound_field_get_offset field/>)
${I}{
${I} throw new zserio.runtime.ZserioError("Write: Wrong offset for field ${compoundName}.${field.name}: " +
Expand Down Expand Up @@ -227,8 +226,11 @@ ${I}}
${I}out.alignTo(${field.alignmentValue});
</#if>
<#if field.offset?? && !field.offset.containsIndex>
<@compound_write_field_offset_check field, compoundName, indent/>
${I}out.alignTo(java.lang.Byte.SIZE);
<@compound_check_offset_field field, compoundName, indent/>
</#if>
<@compound_check_array_length_field field, compoundName, indent/>
<@compound_check_parameterized_field field, compoundName, indent/>
<#if packed && field.isPackable && !field.array??>
<#if field.typeInfo.isIntegral>
${I}zserioContext.${field.getterName}().write(<@array_traits field/>, out,
Expand All @@ -237,19 +239,11 @@ ${I} new ${field.typeInfo.arrayableInfo.arrayElement}(<@compound_get_fiel
${I}<@compound_get_field field/>.write(zserioContext.${field.getterName}(), out);
</#if>
<#elseif field.array??>
<#if field.array.length??>
${I}if (<@compound_get_field field/>.size() != (int)(${field.array.length}))
${I}{
${I} throw new zserio.runtime.ZserioError("Write: Wrong array length for field ${compoundName}.${field.name}: " +
${I} <@compound_get_field field/>.size() + " != " + (int)(${field.array.length}) + "!");
${I}}
</#if>
${I}<@compound_get_field field/>.write<@array_field_packed_suffix field, packed/>(out);
<#elseif field.runtimeFunction??>
${I}out.write${field.runtimeFunction.suffix}(<@compound_get_field field/><#if field.runtimeFunction.arg??>, ${field.runtimeFunction.arg}</#if>);
<#else>
<#-- enum or compound -->
<@compound_check_parameterized_field field, compoundName, indent/>
${I}<@compound_get_field field/>.write(out);
</#if>
</#macro>
Expand All @@ -262,6 +256,17 @@ ${I} throw new zserio.runtime.ConstraintError("Constraint violated at ${compo
</#if>
</#macro>

<#macro compound_check_array_length_field field compoundName indent>
<#local I>${""?left_pad(indent * 4)}</#local>
<#if field.array?? && field.array.length??>
${I}if (<@compound_get_field field/>.size() != (int)(${field.array.length}))
${I}{
${I} throw new zserio.runtime.ZserioError("Write: Wrong array length for field ${compoundName}.${field.name}: " +
${I} <@compound_get_field field/>.size() + " != " + (int)(${field.array.length}) + "!");
${I}}
</#if>
</#macro>

<#macro compound_check_parameterized_field field compoundName indent>
<#local I>${""?left_pad(indent * 4)}</#local>
<#if field.compound?? && field.compound.instantiatedParameters?has_content>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ ${I}zserio_writer.alignto(${field.alignmentValue})
</#if>
<#if field.offset?? && !field.offset.containsIndex>
${I}zserio_writer.alignto(8)
<@compound_check_offset_field field, compoundName, "zserio_writer.bitposition", indent/>
<@compound_check_offset_field field, compoundName, "zserio_writer.bitposition", indent/>
</#if>
<@compound_check_constraint_field field, compoundName, indent/>
<@compound_check_array_length_field field, compoundName, indent/>
Expand Down
1 change: 1 addition & 0 deletions test/language/parameterized_types/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ add_custom_test(parameterized_types
${CMAKE_CURRENT_SOURCE_DIR}/cpp/PackedArrayElementParamTest.cpp
${CMAKE_CURRENT_SOURCE_DIR}/cpp/ParamWithOptionalTest.cpp
${CMAKE_CURRENT_SOURCE_DIR}/cpp/ParameterizedInnerClassesClashingTest.cpp
${CMAKE_CURRENT_SOURCE_DIR}/cpp/ParameterizedNestedInArrayTest.cpp
${CMAKE_CURRENT_SOURCE_DIR}/cpp/ParameterizedParamTest.cpp
${CMAKE_CURRENT_SOURCE_DIR}/cpp/SimpleParamTest.cpp
${CMAKE_CURRENT_SOURCE_DIR}/cpp/SubtypedBitfieldParamTest.cpp
Expand Down
4 changes: 4 additions & 0 deletions test/language/parameterized_types/ClangTidySuppressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ bugprone-forwarding-reference-overload:gen/parameterized_types/array_element_par
bugprone-forwarding-reference-overload:gen/parameterized_types/nested_parameterized_field/TopLevel.h
bugprone-forwarding-reference-overload:gen/parameterized_types/param_with_optional/ExtraValue.h
bugprone-forwarding-reference-overload:gen/parameterized_types/parameterized_inner_classes_clashing/ElementChildrenInitializer_array.h
bugprone-forwarding-reference-overload:gen/parameterized_types/parameterized_nested_in_array/Element.h
bugprone-forwarding-reference-overload:gen/parameterized_types/subtyped_bitfield_param/SubtypedBitfieldParamHolder.h

bugprone-unhandled-self-assignment:gen/parameterized_types/array_element_param/Block.cpp
Expand Down Expand Up @@ -38,6 +39,7 @@ cppcoreguidelines-pro-type-member-init:gen/parameterized_types/packed_array_elem
cppcoreguidelines-pro-type-member-init:gen/parameterized_types/param_with_optional/ExtraValue.cpp
cppcoreguidelines-pro-type-member-init:gen/parameterized_types/param_with_optional/Value.cpp
cppcoreguidelines-pro-type-member-init:gen/parameterized_types/parameterized_inner_classes_clashing/Compound.cpp
cppcoreguidelines-pro-type-member-init:gen/parameterized_types/parameterized_nested_in_array/Parameterized.cpp
cppcoreguidelines-pro-type-member-init:gen/parameterized_types/parameterized_param/ParameterizedParam.cpp
cppcoreguidelines-pro-type-member-init:gen/parameterized_types/parameterized_param/Param.cpp
cppcoreguidelines-pro-type-member-init:gen/parameterized_types/simple_param/Item.cpp
Expand All @@ -64,6 +66,7 @@ performance-move-const-arg:gen/parameterized_types/parameterized_inner_classes_c
performance-move-const-arg:gen/parameterized_types/parameterized_inner_classes_clashing/ElementFactory_array.cpp
performance-move-const-arg:gen/parameterized_types/parameterized_inner_classes_clashing/ElementInitializer_array.cpp
performance-move-const-arg:gen/parameterized_types/parameterized_inner_classes_clashing/Parent.cpp
performance-move-const-arg:gen/parameterized_types/parameterized_nested_in_array/Parameterized.cpp
performance-move-const-arg:gen/parameterized_types/parameterized_param/ParameterizedParamHolder.cpp
performance-move-const-arg:gen/parameterized_types/parameterized_param/ParameterizedParam.cpp
performance-move-const-arg:gen/parameterized_types/parameterized_param/Param.cpp
Expand All @@ -89,6 +92,7 @@ readability-make-member-function-const:gen/parameterized_types/param_with_option
readability-make-member-function-const:gen/parameterized_types/param_with_optional/Param.cpp
readability-make-member-function-const:gen/parameterized_types/parameterized_inner_classes_clashing/Compound.cpp
readability-make-member-function-const:gen/parameterized_types/parameterized_inner_classes_clashing/Parent.cpp
readability-make-member-function-const:gen/parameterized_types/parameterized_nested_in_array/Parameterized.cpp
readability-make-member-function-const:gen/parameterized_types/parameterized_param/ParameterizedParamHolder.cpp
readability-make-member-function-const:gen/parameterized_types/parameterized_param/Param.cpp
readability-make-member-function-const:gen/parameterized_types/simple_param/Item.cpp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "gtest/gtest.h"
#include "parameterized_types/parameterized_nested_in_array/Holder.h"
#include "zserio/BitStreamReader.h"
#include "zserio/BitStreamWriter.h"
#include "zserio/SerializeUtil.h"

namespace parameterized_types
{
namespace parameterized_nested_in_array
{

class ParameterizedNestedInArrayTest : public ::testing::Test
{
protected:
zserio::BitBuffer bitBuffer = zserio::BitBuffer(1024);
};

TEST_F(ParameterizedNestedInArrayTest, writeRead)
{
Holder holder({{Element{Parameterized{6}}}}, {{Element{Parameterized{6}}}});

const auto bitBuffer = zserio::serialize(holder);
const Holder readHolder = zserio::deserialize<Holder>(bitBuffer);

ASSERT_EQ(holder, readHolder);
}

TEST_F(ParameterizedNestedInArrayTest, parameterCheckExceptionInArray)
{
Holder holder({{Element{Parameterized{7}}}}, {{}});
holder.getElementArray().at(0).getParameterized().initialize(6); // wrong value

zserio::BitStreamWriter writer(bitBuffer);
ASSERT_THROW(holder.write(writer), zserio::CppRuntimeException);
}

TEST_F(ParameterizedNestedInArrayTest, parameterCheckExceptionInPackedArray)
{
Holder holder({{}}, {{Element{Parameterized{7}}}});
holder.getElementPackedArray().at(0).getParameterized().initialize(6); // wrong value

zserio::BitStreamWriter writer(bitBuffer);
ASSERT_THROW(holder.write(writer), zserio::CppRuntimeException);
}

} // namespace parameterized_nested_in_array
} // namespace parameterized_types
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package parameterized_types.parameterized_nested_in_array;

import static org.junit.jupiter.api.Assertions.*;

import java.io.IOException;

import org.junit.jupiter.api.Test;

import zserio.runtime.ZserioError;
import zserio.runtime.io.BitBuffer;
import zserio.runtime.io.ByteArrayBitStreamWriter;
import zserio.runtime.io.SerializeUtil;

public class ParameterizedNestedInArrayTest
{
@Test
public void writeRead()
{
final Holder holder = new Holder(new Element[] {new Element(new Parameterized((short)5, (long)6))},
new Element[] {new Element(new Parameterized((short)5, (long)6))});

final BitBuffer bitBuffer = SerializeUtil.serialize(holder);
final Holder readHolder = SerializeUtil.deserialize(Holder.class, bitBuffer);
assertEquals(holder, readHolder);
}

@Test
public void parameterCheckExceptionInArray() throws IOException
{
final Holder holder =
new Holder(new Element[] {new Element(new Parameterized((short)6, (long)7))}, new Element[0]);

final ByteArrayBitStreamWriter writer = new ByteArrayBitStreamWriter();

assertThrows(ZserioError.class, () -> holder.write(writer));
}

@Test
public void parameterCheckExceptionInPackedArray() throws IOException
{
final Holder holder =
new Holder(new Element[0], new Element[] {new Element(new Parameterized((short)6, (long)7))});

final ByteArrayBitStreamWriter writer = new ByteArrayBitStreamWriter();

assertThrows(ZserioError.class, () -> holder.write(writer));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import zserio

import ParameterizedTypes


class ParameterizedNestedInArrayTest(ParameterizedTypes.TestCase):
def testWriteRead(self):
holder = self.api.Holder(
[self.api.Element(self.api.Parameterized(5, 6))], [self.api.Element(self.api.Parameterized(5, 6))]
)
bitBuffer = zserio.serialize(holder)
readHolder = zserio.deserialize(self.api.Holder, bitBuffer)
self.assertEqual(holder, readHolder)

def testParameterCheckExceptionInArray(self):
holder = self.api.Holder([self.api.Element(self.api.Parameterized(6, 7))], [])
writer = zserio.BitStreamWriter()
with self.assertRaises(zserio.PythonRuntimeException):
holder.write(writer)

def testParameterCheckExceptionInPackedArray(self):
holder = self.api.Holder([], [self.api.Element(self.api.Parameterized(6, 7))])
writer = zserio.BitStreamWriter()
with self.assertRaises(zserio.PythonRuntimeException):
holder.write(writer)
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import parameterized_types.nested_parameterized_field.*;
import parameterized_types.packed_array_element_param.*;
import parameterized_types.param_with_optional.*;
import parameterized_types.parameterized_inner_classes_clashing.*;
import parameterized_types.parameterized_nested_in_array.*;
import parameterized_types.parameterized_param.*;
import parameterized_types.simple_param.*;
import parameterized_types.subtyped_bitfield_param.*;
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package parameterized_types.parameterized_nested_in_array;

struct Parameterized(uint8 param)
{
uint32 field : field > param;
};

struct Element
{
Parameterized(5) parameterized;
};

struct Holder
{
Element elementArray[];
packed Element elementPackedArray[];
};

0 comments on commit a2074dc

Please sign in to comment.