From a0207f380043d58a15828b522e48751f0597ad8d Mon Sep 17 00:00:00 2001 From: Mark VanderVoord Date: Tue, 16 Jun 2026 21:41:07 -0400 Subject: [PATCH] void* handling within the array plugin defaults to bytes. --- lib/cmock_generator_utils.rb | 5 + ...oid_pointer_treat_as_void_array_plugin.yml | 102 ++++++++++++++++++ test/unit/cmock_generator_utils_test.rb | 22 ++++ 3 files changed, 129 insertions(+) create mode 100644 test/system/test_interactions/void_pointer_treat_as_void_array_plugin.yml diff --git a/lib/cmock_generator_utils.rb b/lib/cmock_generator_utils.rb index 22e8aff..029b83e 100644 --- a/lib/cmock_generator_utils.rb +++ b/lib/cmock_generator_utils.rb @@ -160,6 +160,11 @@ class CMockGeneratorUtils # void* cannot be safely dereferenced; without the array plugin there's no way # to specify depth, so fall back to comparing the pointer value itself ['UNITY_TEST_ASSERT_EQUAL_PTR', ''] + elsif arg[:ptr?] && @arrays && void_pointer_type?(c_type) + # With the array plugin, treat_as_void aliases not in treat_as would fall back to + # MEMORY_ARRAY with sizeof(void), which is illegal. Use HEX8_ARRAY (byte comparison) + # to match what literal void* gets from the default treat_as entry. + ['UNITY_TEST_ASSERT_EQUAL_HEX8_ARRAY', ''] else @helpers.nil? || @helpers[:unity_helper].nil? ? ['UNITY_TEST_ASSERT_EQUAL', ''] : @helpers[:unity_helper].get_helper(c_type) end diff --git a/test/system/test_interactions/void_pointer_treat_as_void_array_plugin.yml b/test/system/test_interactions/void_pointer_treat_as_void_array_plugin.yml new file mode 100644 index 0000000..5d9feed --- /dev/null +++ b/test/system/test_interactions/void_pointer_treat_as_void_array_plugin.yml @@ -0,0 +1,102 @@ +# ========================================================================= +# CMock - Automatic Mock Generation for C +# ThrowTheSwitch.org +# Copyright (c) 2007-26 Mike Karlesky, Mark VanderVoord, & Greg Williams +# SPDX-License-Identifier: MIT +# ========================================================================= + +--- +#The purpose of this test is to verify that pointers to treat_as_void alias types +#(e.g. MY_VOID*) use HEX8_ARRAY byte comparison when the array plugin is active, +#rather than falling through to MEMORY_ARRAY which would emit sizeof(void) — illegal C. +:cmock: + :plugins: + - :array + :when_ptr: :smart + :treat_as_void: + - MY_VOID + +:systest: + :types: | + typedef void MY_VOID; + + :mockable: | + void get_my_void_ptr(MY_VOID* ptr); + + :source: + :header: | + void function_a(MY_VOID* arg); + + :code: | + void function_a(MY_VOID* arg) { + get_my_void_ptr(arg); + } + + :tests: + :common: | + void setUp(void) {} + void tearDown(void) {} + + :units: + - :pass: TRUE + :should: 'handle treat_as_void pointer with same pointer using standard expect' + :code: | + test() + { + char* a = "Hello"; + get_my_void_ptr_Expect(a); + function_a(a); + } + + - :pass: TRUE + :should: 'handle treat_as_void pointer with same content using ExpectWithArray' + :code: | + test() + { + char* a = "Hello"; + char* b = "Hello"; + get_my_void_ptr_ExpectWithArray(a, 5); + function_a(b); + } + + - :pass: FALSE + :should: 'detect treat_as_void pointer content mismatch with ExpectWithArray' + :code: | + test() + { + char* a = "Hello"; + char* b = "Jello"; + get_my_void_ptr_ExpectWithArray(a, 5); + function_a(b); + } + + - :pass: TRUE + :should: 'handle null treat_as_void pointer comparison' + :code: | + test() + { + get_my_void_ptr_Expect(NULL); + function_a(NULL); + } + + - :pass: TRUE + :should: 'handle treat_as_void pointer comparison with depth 0 using smart ptr comparison' + :code: | + test() + { + char* a = "Hello"; + get_my_void_ptr_ExpectWithArray(a, 0); + function_a(a); + } + + - :pass: FALSE + :should: 'detect treat_as_void pointer value mismatch with depth 0 in smart mode' + :code: | + test() + { + static char a[] = "Hello"; + static char b[] = "Hello"; + get_my_void_ptr_ExpectWithArray(a, 0); + function_a(b); + } +... diff --git a/test/unit/cmock_generator_utils_test.rb b/test/unit/cmock_generator_utils_test.rb index a6a3f57..b05f4d3 100644 --- a/test/unit/cmock_generator_utils_test.rb +++ b/test/unit/cmock_generator_utils_test.rb @@ -446,4 +446,26 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do " }\n" assert_equal(expected, utils.code_verify_an_arg_expectation(function, arg)) end + + it 'handle treat_as_void alias pointer with array plugin using HEX8_ARRAY to avoid sizeof(void)' do + config_stub = create_stub({ + when_ptr: :compare_data, + enforce_strict_ordering: false, + plugins: [:array], + treat_as: {}, + treat_as_void: ['MY_VOID'] + }) + utils = CMockGeneratorUtils.new(config_stub, {:unity_helper => @unity_helper}) + function = { :name => 'Pear' } + + arg = {:type => "MY_VOID*", :name => 'MyVoidAliasPtr', :ptr? => true, :const? => false, :const_ptr? => false} + expected = " {\n" + + " UNITY_SET_DETAILS(CMockString_Pear,CMockString_MyVoidAliasPtr);\n" + + " if (cmock_call_instance->Expected_MyVoidAliasPtr == NULL)\n" + + " { UNITY_TEST_ASSERT_NULL(MyVoidAliasPtr, cmock_line, CMockStringExpNULL); }\n" + + " else\n" + + " { UNITY_TEST_ASSERT_EQUAL_HEX8_ARRAY(cmock_call_instance->Expected_MyVoidAliasPtr, MyVoidAliasPtr, cmock_call_instance->Expected_MyVoidAliasPtr_Depth, cmock_line, CMockStringMismatch); }\n" + + " }\n" + assert_equal(expected, utils.code_verify_an_arg_expectation(function, arg)) + end end