fix segfault on legends with keyimage (#4808)