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