Fix coverity warnings about dereferencing end() iterators.

Refs #9926
Merge remote-tracking branch 'origin/bugfix/9926_coverity_issues_mgigg'