Fix PR20705, crash on invalid.

dyn_cast -> dyn_cast_or_null to handle a null pointer.