Backport fix for CVE-2023-2176 (8d037973 and 0e158630) to v6.1