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

Check return value of SystemFunction036 #34429

Merged
merged 1 commit into from
Jan 19, 2020
Merged
Show file tree
Hide file tree
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
22 changes: 12 additions & 10 deletions base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -519,16 +519,18 @@ end

# generate a random string from random bytes
function _rand_string()
nchars = 10
A = Vector{UInt8}(undef, nchars)
ccall((:SystemFunction036, :Advapi32), stdcall, UInt8, (Ptr{Cvoid}, UInt32), A, sizeof(A))

slug = Base.StringVector(10)
chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
for i = 1:nchars
slug[i] = chars[(A[i] % length(chars)) + 1]
end
return name = String(slug)
nchars = 10
A = Vector{UInt8}(undef, nchars)
windowserror("SystemFunction036 (RtlGenRandom)", 0 == ccall(
(:SystemFunction036, :Advapi32), stdcall, UInt8, (Ptr{Cvoid}, UInt32),
A, sizeof(A)))

slug = Base.StringVector(10)
chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
for i = 1:nchars
slug[i] = chars[(A[i] % length(chars)) + 1]
end
return name = String(slug)
end

function tempname(parent::AbstractString=tempdir(); cleanup::Bool=true)
Expand Down
5 changes: 3 additions & 2 deletions stdlib/Random/src/RNGs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ end # os-test
for T in (Bool, BitInteger_types...)
if Sys.iswindows()
@eval function rand!(rd::RandomDevice, A::Array{$T}, ::SamplerType{$T})
ccall((:SystemFunction036, :Advapi32), stdcall, UInt8, (Ptr{Cvoid}, UInt32),
A, sizeof(A))
Base.windowserror("SystemFunction036 (RtlGenRandom)", 0 == ccall(
(:SystemFunction036, :Advapi32), stdcall, UInt8, (Ptr{Cvoid}, UInt32),
A, sizeof(A)))
A
end
else
Expand Down