Prevent potential NULL pointer dereference in expand_compare