Close the directory handle when we no longer need it

Found by coverity scan
Zero-terminate buffers with data supplied by remote parties

Found by coverity scan