Fix Clang+UBSan build
This addresses a lot of build and runtime errors flagged by Clang 13 with the UBSan sanitizer enabled.
It does NOT cover the undefined behaviour flagged by the bug below.
Test Plan
Build Krita with Clang and ECM_ENABLE_SANITIZERS
set in CMake to "undefined".
Formalities Checklist
-
I confirmed this builds. -
I confirmed Krita ran and the relevant functions work. -
I tested the relevant unit tests and can confirm they are not broken. (If not possible, don't hesitate to ask for help!) -
I made sure my commits build individually and have good descriptions as per KDE guidelines. -
I made sure my code conforms to the standards set in the HACKING file. -
I can confirm the code is licensed and attributed appropriately, and that unattributed code is mine, as per KDE Licensing Policy.