mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Check the reading size range
This commit is contained in:
parent
e5792e7d55
commit
66b0847602
1 changed files with 19 additions and 4 deletions
23
random.c
23
random.c
|
@ -538,6 +538,11 @@ fill_random_bytes_syscall(void *buf, size_t size, int unused)
|
|||
#endif
|
||||
}
|
||||
#elif defined(_WIN32)
|
||||
|
||||
#ifndef DWORD_MAX
|
||||
# define DWORD_MAX (~(DWORD)0UL)
|
||||
#endif
|
||||
|
||||
# if defined(CRYPT_VERIFYCONTEXT)
|
||||
STATIC_ASSERT(sizeof_HCRYPTPROV, sizeof(HCRYPTPROV) == sizeof(size_t));
|
||||
|
||||
|
@ -580,7 +585,12 @@ fill_random_bytes_crypt(void *seed, size_t size)
|
|||
}
|
||||
}
|
||||
if (prov == INVALID_HCRYPTPROV) return -1;
|
||||
CryptGenRandom(prov, size, seed);
|
||||
while (size > 0) {
|
||||
DWORD n = (size > (size_t)DWORD_MAX) ? DWORD_MAX : (DWORD)size;
|
||||
if (!CryptGenRandom(prov, n, seed)) return -1;
|
||||
seed = (char *)seed + n;
|
||||
size -= n;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
# else
|
||||
|
@ -590,9 +600,14 @@ fill_random_bytes_crypt(void *seed, size_t size)
|
|||
static int
|
||||
fill_random_bytes_bcrypt(void *seed, size_t size)
|
||||
{
|
||||
if (!BCryptGenRandom(NULL, seed, size, BCRYPT_USE_SYSTEM_PREFERRED_RNG))
|
||||
return 0;
|
||||
return -1;
|
||||
while (size > 0) {
|
||||
ULONG n = (size > (size_t)ULONG_MAX) ? LONG_MAX : (ULONG)size;
|
||||
if (BCryptGenRandom(NULL, seed, n, BCRYPT_USE_SYSTEM_PREFERRED_RNG))
|
||||
return -1;
|
||||
seed = (char *)seed + n;
|
||||
size -= n;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
|
|
Loading…
Add table
Reference in a new issue