Setting last 8 bytes of char array on __uint64

I am implementing the SHA1 algorithm from pseudocode on wikipedia.

It says that I should add 64 bits to the original length in the message, so I tried the following:

// new_message is of type char[] and is 9+ bytes long *((__int64*)(new_message-8)) = (__int64) length; 

This leads to new_message memory corruption.

Can anyone spot an error?

Thanks!

Edit:

Jesus, I'm so stupid. new_message pointed to the beginning of my array, no wonder it crashed!
+4
source share
2 answers

Not definitely, without seeing the part of the code that shows what new_message set new_message .

It looks like you are overflowing the buffer, but this will ruin the heap header, which probably precedes &new_message[0] . You write __int64 8 bytes before the start of char[] , by sight of things.

+2
source

new_message-8 will return 8 bytes from the current pointer, is this the intention? append would mean adding 8 bytes at the end, you need to make sure 1> You have enough memory (8 additional bytes) at the end. 2> pointer points to the right place (where to add)

+3
source

All Articles