fix various leaks, unchecked returned values and possible strings not being terminated with the help of coverity analysis tool