fix(windows): preserve mkstemp errors through cleanup

Signed-off-by: News <knewstimek@users.noreply.github.com>
This commit is contained in:
News
2026-09-26 20:30:02 +09:00
parent 64c23fab6c
commit ed991154f2
+6 -3
View File
@@ -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