Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #17898(randomPathName called twice in a row can return the same string on windows) #18729

Merged
merged 2 commits into from
Aug 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions lib/std/tempfiles.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ See also:
* `mkstemp` (posix), refs https://man7.org/linux/man-pages/man3/mkstemp.3.html
]#

import os, random
import os, random, std/monotimes


const
Expand Down Expand Up @@ -96,11 +96,25 @@ proc safeOpen(filename: string): File =
discard posix.close(fileHandle) # TODO handles failure when closing file
raiseOSError(osLastError(), filename)


type
NimTempPathState = object
state: Rand
isInit: bool

var nimTempPathState {.threadvar.}: NimTempPathState

template randomPathName(length: Natural): string =
var res = newString(length)
var state = initRand()
if not nimTempPathState.isInit:
var time = getMonoTime().ticks
when compileOption("threads"):
time = time xor int64(getThreadId())
nimTempPathState.isInit = true
nimTempPathState.state = initRand(time)

for i in 0 ..< length:
res[i] = state.sample(letters)
res[i] = nimTempPathState.state.sample(letters)
res

proc getTempDirImpl(dir: string): string {.inline.} =
Expand Down