Trailing blanks and buffer overflows

Coverity scan check caught several places where possible buffer overflows
may happen. Also cleaned-up and removed a bunch of trailing spaces.