[#2212] CID25860:

refactor to avoid TOCTOU