Question

This function is buried in a complex nest so actually finding the cause is probably beyond anything I can ask, but I'm wondering if anyone might be able to give some tips on how I might go about debugging this. Here is the gist of the code I'm having trouble with

//func1.c
somestruct* func1(somestruct* mystruct)
{
    printf("func1: %p, %i\n", mystruct, mystruct->foo);
    return mystruct;
}
//func2.c
somestruct* func1(somestruct* mystruct);
void func2()
{
    somestruct *mystruct = malloc(sizeof(somestruct));
    mystruct->foo = 10;
    printf("func2: %p, %i\n", mystruct, mystruct->foo);
    mystruct = func1(mystruct);
    printf("back in func2: %p\n", mystruct);
    free(mystruct);
}

And I call func2. The output is like so

func2: 0x7f38a00008c0, 10
func1: 0x7f38a00008c0, 10
back in func2: 0xffffffffa00008c0
(SEGFAULT trying to free 0xffffffffa00008c0)

The actual code is more complex, "mystruct" gets passed around in many other places as well without issue, the fact that the functions are in different files seems like it might be part of the problem, yes it needs to return the pointer (the returned pointer is not guaranteed to be the same as the input pointer). It seems really weird to me that it's kind of (but not actually) getting truncated to 32 bits, and then filled with ffffffff at the top.

When compiled on a 32 bit machine it works exactly as it should.

I'd considered memory corruption somewhere, so I ran it through valgrind. Valgrind reports no errors, and in fact causes it to complete successfully. Textbook heisenbug. At least I can use GDB.

Does anyone have any idea what might be causing this, or at least how I might start tracking down the problem?

Was it helpful?

Solution

Check your code if you missed out function prototype (somestruct* func1(somestruct* mystruct);).in func2.c.

By default all return values are int. So if a prototype is missing for function then compiler treats the return value as 32-bit and generates code for 32-bit return value. Thats when your upper 4 bytes gets truncated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top