0

I have a segfault occurring because I have a cpputest mock which returns a pointer to a struct, at the point that the mock is called, and inside the mock itself, the address of the struct is something like for example 0x7ffd59372178, but when I check the address of the pointer in the function which is passed the pointer to the struct by the mock, it ends up with a truncated version of the address, like for example 0x59372178, then, it tries to use the pointer, and segfaults because it's not looking at the right memory address.

I thought at first the function and the mock were operating with different definitions of the struct, but I made sure there was only one version of it and the behavior was the same. I also tried doing the same thing with just a float pointer to see if that would work, but that also ran into the same problem. In writing this example (which actually does work correctly) I proved that it's not an issue with an interaction between c and cpp, so I think now I need to investigate my buildchain.

What could be the probable cause(s) of the truncation of the pointer address I'm seeing?

test.cpp

#include "CppUTest/TestHarness.h"
#include "CppUTestExt/MockSupport.h"
#include "CppUTest/CommandLineTestRunner.h"

extern "C"
{
    #include "includes.h"
    #include <stdio.h>

    DivStruct_t* GetDivPtr()
    {
        void* retVal = mock().actualCall("GetDivPtr").returnPointerValue();

        printf("fake div in mock %p \n", retVal);

        return (DivStruct_t*)retVal;
    }
}

TEST_GROUP( mockTest )
{
    void setup()
    {
        // Initialize before each test
    }

    void teardown()
    {
        // Deinitialize after each test
        mock().clear();
    }
};

TEST ( mockTest, passingPointer )
{
    DivStruct_t fakeDiv;
    fakeDiv.f1 = 8.0;
    fakeDiv.f2 = 6.0;
    printf("fake div addr %p \n", (void*)&fakeDiv);
    mock().expectOneCall("GetDivPtr").andReturnValue(&fakeDiv);

    Cfunction();

    mock().checkExpectations();
}

function.c

#include "includes.h"
void Cfunction()
{
    DivStruct_t* div = GetDivPtr();
    // seg fault happens here
    printf("div addr %p \n", (void*)&div);
    float f1Val = div->f1;

    return;
}

includes.h

typedef struct
{
    float f1;
    float f2;
} DivStruct_t;

DivStruct_t* GetDivPtr();
void Cfunction();

main.cpp

#include "CppUTest/CommandLineTestRunner.h"

int main( int ac, char ** av )
{
    return CommandLineTestRunner::RunAllTests( ac, av );
}
1
  • I am not familiar with that test framework - at first sight mock() is looking like returning a temporary object and mock().actualCall("GetDivPtr").returnPointerValue() is returning a dangling pointer. Commented Feb 6 at 21:55

1 Answer 1

1

I found a solution based on this post:

64 bit function returns 32 bit pointer

turns out I needed to define the function which calls the mock().actualCall in my header file of the c function

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.