The function `calculate_payload_size` may call itself recursively, which can lead to a stack overflow and trigger a crash. Related: #1081