rewrite build_address_string() to be safe and sane

Functionality should be unchanged, it's just a bit more long-winded now
with all the length checking and safety.