Solve several coverity defects, including one memory leak and possible use of negative index into array.