From ed991154f212928df63ce5a5cd1159cfc8196f00 Mon Sep 17 00:00:00 2001 From: News Date: Sat, 26 Sep 2026 20:30:02 +0900 Subject: [PATCH] fix(windows): preserve mkstemp errors through cleanup Signed-off-by: News --- src/foundation/compat.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/foundation/compat.c b/src/foundation/compat.c index fb3caca87..6642147f5 100644 --- a/src/foundation/compat.c +++ b/src/foundation/compat.c @@ -285,10 +285,11 @@ int cbm_mkstemp(char *tmpl) { return CBM_NOT_FOUND; } static const wchar_t hex[] = L"0123456789abcdef"; + int saved_errno = EEXIST; for (int attempt = 0; attempt < 128; attempt++) { unsigned int random_bits = 0; if (!cbm_secure_random(&random_bits, sizeof(random_bits))) { - errno = EIO; + saved_errno = EIO; break; } for (int digit = 0; digit < 6; digit++) { @@ -299,10 +300,11 @@ int cbm_mkstemp(char *tmpl) { if (!expanded || !wide_open || strlen(expanded) >= sizeof(buf)) { free(expanded); free(wide_open); - errno = ENAMETOOLONG; + saved_errno = ENAMETOOLONG; break; } int fd = _wopen(wide_open, _O_CREAT | _O_EXCL | _O_RDWR | _O_BINARY, _S_IREAD | _S_IWRITE); + saved_errno = errno; free(wide_open); if (fd >= 0) { strcpy(tmpl, expanded); @@ -311,11 +313,12 @@ int cbm_mkstemp(char *tmpl) { return fd; } free(expanded); - if (errno != EEXIST) { + if (saved_errno != EEXIST) { break; } } free(wide_template); + errno = saved_errno; return CBM_NOT_FOUND; } #endif