This is an archive of the discontinued LLVM Phabricator instance.

[libcxx] [test] Fix back-to-back use of get_temp_file_name() on Windows
ClosedPublic

Authored by mstorsjo on Mar 28 2022, 2:03 PM.

Details

Summary

On non-Windows platforms, get_temp_file_name() uses mkstemp(),
which picks a unique name and creates a file atomically. The
Windows implementation uses _mktemp_s(), which doesn't create the
file. The documentation of _mktemp_s() also says that by design,
the function uses the same pattern within a process, as long as that
file doesn't exist.

Thus previously, two consecutive calls to get_temp_file_name()
on Windows returned the same file name.

Try to create the suggested temp file with _O_EXCL (marking the file name as already used for future calls to _mktemp_s) and retry if we weren't able to exclusively create the file.

This fixes the test failures on Windows observed in D122257.

Diff Detail

Event Timeline

mstorsjo created this revision.Mar 28 2022, 2:03 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 28 2022, 2:03 PM
mstorsjo requested review of this revision.Mar 28 2022, 2:03 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 28 2022, 2:03 PM
Herald added a reviewer: Restricted Project. · View Herald Transcript

Ping @ldionne. This would avoid needing exceptions/xfails for windows in D122257.

mstorsjo updated this revision to Diff 421916.Apr 11 2022, 7:05 AM

Made the temp file creation atomic by creating the suggested temp file with _open(_O_CREAT | _O_EXCL).

mstorsjo edited the summary of this revision. (Show Details)Apr 11 2022, 7:06 AM
ldionne accepted this revision.Apr 11 2022, 8:32 AM
This revision is now accepted and ready to land.Apr 11 2022, 8:32 AM