From 16d12416fde6305458cad0b905bb077b421d57a2 Mon Sep 17 00:00:00 2001 From: Mark VanderVoord Date: Mon, 9 Jan 2023 14:16:30 -0500 Subject: [PATCH] Force ourselves to test with Ruby 3. Fix some issues that have been lurking. --- .github/workflows/main.yml | 5 +++++ lib/cmock_generator.rb | 6 +++++- src/cmock.c | 19 ++++++++++++++++--- test/c/TestCMockC.c | 2 +- test/unit/cmock_header_parser_test.rb | 12 ++++++------ 5 files changed, 33 insertions(+), 11 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index fad6c78..eb961f5 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -16,6 +16,11 @@ jobs: name: "Unit Tests" runs-on: ubuntu-latest steps: + - uses: ruby/setup-ruby@v1 + with: + ruby-version: '3.0' # Not needed with a .ruby-version file + bundler-cache: true # runs 'bundle install' and caches + # Install Multilib - name: Install Multilib run: | diff --git a/lib/cmock_generator.rb b/lib/cmock_generator.rb index 7905524..b544d74 100644 --- a/lib/cmock_generator.rb +++ b/lib/cmock_generator.rb @@ -137,7 +137,11 @@ class CMockGenerator @file_writer.create_file(mock_project[:module_name] + '.c', @subdir) do |file, fullname| blank_project = mock_project.clone blank_project[:parsed_stuff] = { :functions => [] } - create_source_header_section(file, fullname, blank_project) if existing.empty? + if (existing.empty?) + create_source_header_section(file, fullname, blank_project) + else + file << existing << "\n" + end mock_project[:parsed_stuff][:functions].each do |function| create_function_skeleton(file, function, existing) end diff --git a/src/cmock.c b/src/cmock.c index 7749e3d..e6a1197 100644 --- a/src/cmock.c +++ b/src/cmock.c @@ -25,10 +25,9 @@ static unsigned char* CMock_Guts_Buffer = NULL; static CMOCK_MEM_INDEX_TYPE CMock_Guts_BufferSize = CMOCK_MEM_ALIGN_SIZE; static CMOCK_MEM_INDEX_TYPE CMock_Guts_FreePtr = CMOCK_MEM_ALIGN_SIZE; #else -static long long CMock_Guts_Space[(CMOCK_MEM_SIZE + CMOCK_MEM_ALIGN_SIZE + sizeof(long long) - 1) / - sizeof(long long)]; +static long long CMock_Guts_Space[(CMOCK_MEM_SIZE + CMOCK_MEM_ALIGN_SIZE + sizeof(long long) - 1) / sizeof(long long)]; static unsigned char* CMock_Guts_Buffer = (unsigned char *)CMock_Guts_Space; -static CMOCK_MEM_INDEX_TYPE CMock_Guts_BufferSize = sizeof(CMock_Guts_Space); +static CMOCK_MEM_INDEX_TYPE CMock_Guts_BufferSize = CMOCK_MEM_SIZE + CMOCK_MEM_ALIGN_SIZE;//sizeof(CMock_Guts_Space); static CMOCK_MEM_INDEX_TYPE CMock_Guts_FreePtr = CMOCK_MEM_ALIGN_SIZE; #endif @@ -41,12 +40,16 @@ CMOCK_MEM_INDEX_TYPE CMock_Guts_MemNew(CMOCK_MEM_INDEX_TYPE size) /* verify arguments valid (we must be allocating space for at least 1 byte, and the existing chain must be in memory somewhere) */ if (size < 1) + { return CMOCK_GUTS_NONE; + } /* verify we have enough room */ size = size + CMOCK_MEM_INDEX_SIZE; if (size & CMOCK_MEM_ALIGN_MASK) + { size = (size + CMOCK_MEM_ALIGN_MASK) & ~CMOCK_MEM_ALIGN_MASK; + } if ((CMock_Guts_BufferSize - CMock_Guts_FreePtr) < size) { #ifndef CMOCK_MEM_DYNAMIC @@ -105,9 +108,13 @@ CMOCK_MEM_INDEX_TYPE CMock_Guts_MemChain(CMOCK_MEM_INDEX_TYPE root_index, CMOCK_ do { index = *(CMOCK_MEM_INDEX_TYPE*)((CMOCK_MEM_PTR_AS_INT)next - CMOCK_MEM_INDEX_SIZE); if (index >= CMock_Guts_FreePtr) + { return CMOCK_GUTS_NONE; + } if (index > 0) + { next = (void*)(&CMock_Guts_Buffer[index]); + } } while (index > 0); *(CMOCK_MEM_INDEX_TYPE*)((CMOCK_MEM_PTR_AS_INT)next - CMOCK_MEM_INDEX_SIZE) = (CMOCK_MEM_INDEX_TYPE)((CMOCK_MEM_PTR_AS_INT)obj - (CMOCK_MEM_PTR_AS_INT)CMock_Guts_Buffer); return root_index; @@ -124,16 +131,22 @@ CMOCK_MEM_INDEX_TYPE CMock_Guts_MemNext(CMOCK_MEM_INDEX_TYPE previous_item_index /* There is nothing "next" if the pointer isn't from our buffer */ if ((previous_item_index < CMOCK_MEM_ALIGN_SIZE) || (previous_item_index >= CMock_Guts_FreePtr)) + { return CMOCK_GUTS_NONE; + } previous_item = (void*)(&CMock_Guts_Buffer[previous_item_index]); /* if the pointer is good, then use it to look up the next index * (we know the first element always goes in zero, so NEXT must always be > 1) */ index = *(CMOCK_MEM_INDEX_TYPE*)((CMOCK_MEM_PTR_AS_INT)previous_item - CMOCK_MEM_INDEX_SIZE); if ((index > 1) && (index < CMock_Guts_FreePtr)) + { return index; + } else + { return CMOCK_GUTS_NONE; + } } /*------------------------------------------------------- diff --git a/test/c/TestCMockC.c b/test/c/TestCMockC.c index 769a2b3..a5c2194 100644 --- a/test/c/TestCMockC.c +++ b/test/c/TestCMockC.c @@ -32,7 +32,7 @@ void test_MemNewWillReturnNullIfGivenIllegalSizes(void) void test_MemShouldProtectAgainstMemoryOverflow(void) { - (void)CMock_Guts_MemNew(CMOCK_MEM_SIZE - TEST_MEM_INDEX_SIZE); + TEST_ASSERT_NOT_EQUAL_UINT( CMOCK_GUTS_NONE, CMock_Guts_MemNew(CMOCK_MEM_SIZE - TEST_MEM_INDEX_SIZE) ); //verify we've used all the memory TEST_ASSERT_LESS_OR_EQUAL_UINT32(TEST_MEM_INDEX_SIZE, CMock_Guts_MemBytesFree()); diff --git a/test/unit/cmock_header_parser_test.rb b/test/unit/cmock_header_parser_test.rb index bee6a24..eedc0cb 100644 --- a/test/unit/cmock_header_parser_test.rb +++ b/test/unit/cmock_header_parser_test.rb @@ -41,7 +41,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do end it "create and initialize variables to defaults appropriately" do - assert_equal(nil, @parser.funcs) + assert_nil(@parser.funcs) assert_equal(['const', '__ramfunc', 'funky_attrib', 'SQLITE_API'], @parser.c_attributes) assert_equal(['void','MY_FUNKY_VOID'], @parser.treat_as_void) end @@ -377,7 +377,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do @parser.parse("module", source) end - assert_equal(nil, @parser.funcs) + assert_nil(@parser.funcs) # verify exception message begin @@ -401,7 +401,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do @parser.parse("module", source) end - assert_equal(nil, @parser.funcs) + assert_nil(@parser.funcs) # verify exception message begin @@ -429,7 +429,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do @parser.parse("module", source) end - assert_equal(nil, @parser.funcs) + assert_nil(@parser.funcs) # verify exception message begin @@ -670,7 +670,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do @parser.parse("module", source) end - assert_equal(nil, @parser.funcs) + assert_nil(@parser.funcs) # verify exception message begin @@ -699,7 +699,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do @parser.parse("module", source) end - assert_equal(nil, @parser.funcs) + assert_nil(@parser.funcs) # verify exception message begin