diff options
author | Chris Lattner <sabre@nondot.org> | 2010-07-12 00:09:55 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2010-07-12 00:09:55 +0000 |
commit | cda39c4ee487024ae7c3e412844f7f67da702a4d (patch) | |
tree | 353833ce08c700ae07c74c06821b12a1f2f29f68 /llvm/lib/System/Unix | |
parent | 0b7ae20a35187bb53372e2b156ddb72fe8734615 (diff) | |
download | bcm5719-llvm-cda39c4ee487024ae7c3e412844f7f67da702a4d.tar.gz bcm5719-llvm-cda39c4ee487024ae7c3e412844f7f67da702a4d.zip |
improve Path::makeUnique when mkstemp/mktemp are not available
patch by Lasse Kärkkäinen in PR7404.
llvm-svn: 108110
Diffstat (limited to 'llvm/lib/System/Unix')
-rw-r--r-- | llvm/lib/System/Unix/Path.inc | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/llvm/lib/System/Unix/Path.inc b/llvm/lib/System/Unix/Path.inc index 185f7fd66bf..bc104a32a3a 100644 --- a/llvm/lib/System/Unix/Path.inc +++ b/llvm/lib/System/Unix/Path.inc @@ -888,14 +888,19 @@ Path::makeUnique(bool reuse_current, std::string* ErrMsg) { #else // Okay, looks like we have to do it all by our lonesome. static unsigned FCounter = 0; - unsigned offset = path.size() + 1; - while ( FCounter < 999999 && exists()) { - sprintf(FNBuffer+offset,"%06u",++FCounter); + // Try to initialize with unique value. + if (FCounter == 0) FCounter = ((unsigned)getpid() & 0xFFFF) << 8; + char* pos = strstr(FNBuffer, "XXXXXX"); + do { + if (++FCounter > 0xFFFFFF) { + return MakeErrMsg(ErrMsg, + path + ": can't make unique filename: too many files"); + } + sprintf(pos, "%06X", FCounter); path = FNBuffer; - } - if (FCounter > 999999) - return MakeErrMsg(ErrMsg, - path + ": can't make unique filename: too many files"); + } while (exists()); + // POSSIBLE SECURITY BUG: An attacker can easily guess the name and exploit + // LLVM. #endif return false; } |