All functions assume that the pointer is never null, so pass by
reference, to avoid accidental segfaults at runtime, or at least make
them more obvious.
Also, remove unused casts in touched lines.
Also, add CHECK_NONFATAL checks, to turn segfault crashes into an
recoverable runtime error with debug information.
This concludes backport of core#29021
https://github.com/bitcoin/bitcoin/pull/29021/commits/fa5989d514d246e56977c528b2dd2abe6dc8efcc
Depends on D18257