Fix a pile of coverity warnings in the unit tests

Most of these problems can only trigger when the tests fail, but it's
good hygiene to keep the code warning-free in any case.