Fix crash on adding attachments, due to replacing range out of bounds.