This MR fixes an amount of issues found through application of ASAN, UBSAN and building through clang-16.