Correct issues found by AddressSanitizer.

The changes in this pull request fixes issues found by AddressSanitizer from LLVM 3.4.
Merge pull request #2699 from redstar/asan

Correct issues found by AddressSanitizer.