be more strict with potential null after dynamic_cast, re #10713,