This is a backport of [[https://github.com/bitcoin/bitcoin/pull/23437 | core#23437]]
https://github.com/bitcoin/bitcoin/pull/23437/commits/9360778d6e12fd16d44b2d9162628e5036e50dad<F5>
The fuzzer part of the first commit and the second commit are not applicable, due to missing fuzzer backports.
Depends on D12236