improve Path::makeUnique when mkstemp/mktemp are not available

patch by Lasse Kärkkäinen in PR7404.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@108110 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner
2010-07-12 00:09:55 +00:00
parent 7791080151
commit 1f01109254

View File

@ -888,14 +888,19 @@ Path::makeUnique(bool reuse_current, std::string* ErrMsg) {
#else #else
// Okay, looks like we have to do it all by our lonesome. // Okay, looks like we have to do it all by our lonesome.
static unsigned FCounter = 0; static unsigned FCounter = 0;
unsigned offset = path.size() + 1; // Try to initialize with unique value.
while ( FCounter < 999999 && exists()) { if (FCounter == 0) FCounter = ((unsigned)getpid() & 0xFFFF) << 8;
sprintf(FNBuffer+offset,"%06u",++FCounter); char* pos = strstr(FNBuffer, "XXXXXX");
path = FNBuffer; do {
} if (++FCounter > 0xFFFFFF) {
if (FCounter > 999999)
return MakeErrMsg(ErrMsg, return MakeErrMsg(ErrMsg,
path + ": can't make unique filename: too many files"); path + ": can't make unique filename: too many files");
}
sprintf(pos, "%06X", FCounter);
path = FNBuffer;
} while (exists());
// POSSIBLE SECURITY BUG: An attacker can easily guess the name and exploit
// LLVM.
#endif #endif
return false; return false;
} }