Prevent undefined behavior due to typedef array usage.

This commit is contained in:
John Lindgren
2018-12-04 18:22:53 -05:00
parent 789c5852b5
commit cfa46d6440
2 changed files with 12 additions and 5 deletions
+4 -1
View File
@@ -73,7 +73,10 @@ class CMockGeneratorUtils
if (arg[:ptr?] or @treat_as.include?(arg[:type]))
" #{dest} = #{arg[:name]};\n"
else
" memcpy(&#{dest}, &#{arg[:name]}, sizeof(#{arg[:type]}));\n"
assert_expr = "sizeof(#{arg[:name]}) == sizeof(#{arg[:type]}) ? 1 : -1"
comment = "/* add #{arg[:type]} to :treat_as_array if this causes an error */"
" memcpy(&#{dest}, &#{arg[:name]},\n" +
" sizeof(#{arg[:type]}[#{assert_expr}])); #{comment}\n"
end
end
+8 -4
View File
@@ -106,7 +106,8 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do
expected3 = " cmock_call_instance->Expected_Kiwi = Kiwi;\n"
arg4 = { :name => "Lime", :const? => false, :type => 'LIME_T', :ptr? => false }
expected4 = " memcpy(&cmock_call_instance->Expected_Lime, &Lime, sizeof(LIME_T));\n"
expected4 = " memcpy(&cmock_call_instance->Expected_Lime, &Lime,\n" +
" sizeof(LIME_T[sizeof(Lime) == sizeof(LIME_T) ? 1 : -1])); /* add LIME_T to :treat_as_array if this causes an error */\n"
assert_equal(expected1, @cmock_generator_utils_simple.code_add_an_arg_expectation(arg1))
assert_equal(expected2, @cmock_generator_utils_simple.code_add_an_arg_expectation(arg2))
@@ -131,7 +132,8 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do
" cmock_call_instance->ReturnThruPtr_Kiwi_Used = 0;\n"
arg4 = { :name => "Lime", :const? => false, :type => 'LIME_T', :ptr? => false }
expected4 = " memcpy(&cmock_call_instance->Expected_Lime, &Lime, sizeof(LIME_T));\n" +
expected4 = " memcpy(&cmock_call_instance->Expected_Lime, &Lime,\n" +
" sizeof(LIME_T[sizeof(Lime) == sizeof(LIME_T) ? 1 : -1])); /* add LIME_T to :treat_as_array if this causes an error */\n" +
" cmock_call_instance->IgnoreArg_Lime = 0;\n"
assert_equal(expected1, @cmock_generator_utils_complex.code_add_an_arg_expectation(arg1))
@@ -153,7 +155,8 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do
}
expected = "void CMockExpectParameters_Melon(CMOCK_Melon_CALL_INSTANCE* cmock_call_instance, stuff)\n{\n" +
" cmock_call_instance->Expected_MyIntPtr = MyIntPtr;\n" +
" memcpy(&cmock_call_instance->Expected_MyMyType, &MyMyType, sizeof(MY_TYPE));\n" +
" memcpy(&cmock_call_instance->Expected_MyMyType, &MyMyType,\n" +
" sizeof(MY_TYPE[sizeof(MyMyType) == sizeof(MY_TYPE) ? 1 : -1])); /* add MY_TYPE to :treat_as_array if this causes an error */\n" +
" cmock_call_instance->Expected_MyStr = MyStr;\n" +
"}\n\n"
assert_equal(expected, @cmock_generator_utils_simple.code_add_argument_loader(function))
@@ -169,7 +172,8 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do
" cmock_call_instance->Expected_MyIntPtr_Depth = MyIntPtr_Depth;\n" +
" cmock_call_instance->IgnoreArg_MyIntPtr = 0;\n" +
" cmock_call_instance->ReturnThruPtr_MyIntPtr_Used = 0;\n" +
" memcpy(&cmock_call_instance->Expected_MyMyType, &MyMyType, sizeof(MY_TYPE));\n" +
" memcpy(&cmock_call_instance->Expected_MyMyType, &MyMyType,\n" +
" sizeof(MY_TYPE[sizeof(MyMyType) == sizeof(MY_TYPE) ? 1 : -1])); /* add MY_TYPE to :treat_as_array if this causes an error */\n" +
" cmock_call_instance->IgnoreArg_MyMyType = 0;\n" +
" cmock_call_instance->Expected_MyStr = MyStr;\n" +
" cmock_call_instance->IgnoreArg_MyStr = 0;\n" +