Fix bugs found by address sanitizer.