Fix error introduced in yesterday's commit.

Check for atom is invalid, since it's an iterator, not a pointer.
Make sure to allow for NULL termination.

Bug found via Coverity.
Fix some minor potential memory leaks.

Bugs found via Coverity.
Ensure strings are null terminated with strncpy.

Bug found via Coverity.