Since we compile with GCC and avr-libc  I would change malloc into alloca, which is automatically freed on return.