Improvements suggested by Valgrind analysis of ztest

A while back, I ran valgrind on ztest, which caught a few possible problems. These patches are the result.