From 0848b191515201534a7ea0f42581037f0cc818c7 Mon Sep 17 00:00:00 2001 From: Keno Fischer Date: Sat, 18 Jan 2020 13:29:24 -0500 Subject: [PATCH] Check return value of SystemFunction036 In both places we call it. Also tabs-to-spaces for the new code to match the formatting of the rest of the code. --- base/file.jl | 22 ++++++++++++---------- stdlib/Random/src/RNGs.jl | 5 +++-- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/base/file.jl b/base/file.jl index 6d4fca5fda8c5..8fec68f4d7e1c 100644 --- a/base/file.jl +++ b/base/file.jl @@ -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) diff --git a/stdlib/Random/src/RNGs.jl b/stdlib/Random/src/RNGs.jl index 9b1d2fce6ac1e..22271ba6109ba 100644 --- a/stdlib/Random/src/RNGs.jl +++ b/stdlib/Random/src/RNGs.jl @@ -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