Check for NULL file descriptors before close()

I have no idea why this is happening, but I get some SEGVs when trying
to run some code under the test harness.  I figured I'd just be
explicit and test for NULL pointers.  This appears to have made the
failures go away, but they were sporadic so who knows.