Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C pointer trouble: it loses address information when returned from a function

Tags:

c

pointers

I have some C code here where a pointer is allocated and returned to the parent caller/parent function. Using GDB, I noticed that the pointer allocation is fine but when I return it, it loses the high bits of the first four bytes of the address. Basically it goes like this:

allocate pointer A
allocate pointer B
Free A
Return B

Now, if I change this to:

allocate pointer B
allocate pointer A
Free A
Return B

Things work fine. What is strange is that this code worked fine for 5+ years and now it is giving us trouble on some very specific cases. What worries us is that the address is changed, we could not detect this error in any other way, only when we attached gdb and started looking around we saw the change.

Anyone here seen something similar or knows why this is happening? By the way, we are not trying to write more than we can handle in A or B, both buffers are the right size for the data.

thanks in advance for any clue.

like image 233
Andre Garzia Avatar asked Mar 25 '26 07:03

Andre Garzia


2 Answers

Smells like you didn't declare a prototype of your function. This went well for years on 32bit machines where int and void* have the same width. Nowadays these are different, and you loose your high order bytes.

You compile with -Wall or something like that, I suppose?

like image 111
Jens Gustedt Avatar answered Mar 27 '26 21:03

Jens Gustedt


This could be due to many reasons, overflow of buffers, limited heap space etc. Posting code would help!

like image 35
Kakira Avatar answered Mar 27 '26 21:03

Kakira



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!