Fix problems reported by valgrind.

Replace memcpy with memmove in replace_string().
Fix buffer underun in restore_object()