felixjones / agbabi

Provides GBA optimized functions for some common, handy operations.
Other
68 stars 4 forks source link

Possible stack corruption in memmove #4

Closed JoaoBaptMG closed 2 years ago

JoaoBaptMG commented 2 years ago

While analysing the source code of memmove, I figure out that this code might have a stack corruption. The code looks like this:

memmove:
    push    {r0, lr}
    cmp     r0, r1
    bgt     __agbabi_rmemcpy
    b       __aeabi_memcpy
    pop     {r0, lr}
    bx      lr

The first line pushes r0 and lr into the stack (possibly for register preservation), but then it uses an unconditional branch in the code, so, assuming __agbabi_rmemcpy and __aeabi_memcpy return to their callers issuing a bx lr, the last two lines of code will never be executed, making the two registers "spill" into the caller frame, causing a stack corruption.

I think you meant to use bl instead of b here?

felixjones commented 2 years ago

Thanks loads for spotting this! Solved with a5dbca423d42bb4a4f7610bd78f4a192275a6f5a