fix array overflow issues found by David A.

Issues reported at
https://hypernews.cern.ch/HyperNews/CMS/get/swReleases/4108/1/1/1/1/1/1/1/1.html
and follow ups are mostly fixed here.