mirror of
https://github.com/reactos/reactos.git
synced 2025-08-03 13:45:50 +00:00
[CRT]
Fix rand_s implementation so that it doesn't leak resources, so that it doesn't dereference null pointer, so that it matches MSDN documentation (https://msdn.microsoft.com/fr-fr/library/sxtz2fa8.aspx) CID 716561 CID 716668 svn path=/trunk/; revision=69005
This commit is contained in:
parent
3d95d5dd14
commit
975826e064
1 changed files with 33 additions and 9 deletions
|
@ -33,17 +33,41 @@ srand(unsigned int seed)
|
||||||
int CDECL rand_s(unsigned int *pval)
|
int CDECL rand_s(unsigned int *pval)
|
||||||
{
|
{
|
||||||
BOOLEAN (WINAPI *pSystemFunction036)(PVOID, ULONG); // RtlGenRandom
|
BOOLEAN (WINAPI *pSystemFunction036)(PVOID, ULONG); // RtlGenRandom
|
||||||
HINSTANCE hadvapi32 = LoadLibraryA("advapi32.dll");
|
HINSTANCE hadvapi32;
|
||||||
int ret = 0;
|
|
||||||
pSystemFunction036 = (void*)GetProcAddress(hadvapi32, "SystemFunction036");
|
if (!pval)
|
||||||
#if 1
|
|
||||||
if (!pval || (pSystemFunction036 && !pSystemFunction036(pval, sizeof(*pval))))
|
|
||||||
{
|
{
|
||||||
_invalid_parameter(NULL,_CRT_WIDE("rand_s"),_CRT_WIDE(__FILE__),__LINE__, 0);
|
_invalid_parameter(NULL,_CRT_WIDE("rand_s"),_CRT_WIDE(__FILE__),__LINE__, 0);
|
||||||
*_errno() = EINVAL;
|
*_errno() = EINVAL;
|
||||||
ret = EINVAL;
|
return EINVAL;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
if(hadvapi32) FreeLibrary(hadvapi32);
|
*pval = 0;
|
||||||
return ret;
|
hadvapi32 = LoadLibraryA("advapi32.dll");
|
||||||
|
if (!hadvapi32)
|
||||||
|
{
|
||||||
|
_invalid_parameter(NULL,_CRT_WIDE("rand_s"),_CRT_WIDE(__FILE__),__LINE__, 0);
|
||||||
|
*_errno() = EINVAL;
|
||||||
|
return EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
pSystemFunction036 = (void*)GetProcAddress(hadvapi32, "SystemFunction036");
|
||||||
|
if (!pSystemFunction036)
|
||||||
|
{
|
||||||
|
_invalid_parameter(NULL,_CRT_WIDE("rand_s"),_CRT_WIDE(__FILE__),__LINE__, 0);
|
||||||
|
*_errno() = EINVAL;
|
||||||
|
FreeLibrary(hadvapi32);
|
||||||
|
return EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!pSystemFunction036(pval, sizeof(*pval)))
|
||||||
|
{
|
||||||
|
_invalid_parameter(NULL,_CRT_WIDE("rand_s"),_CRT_WIDE(__FILE__),__LINE__, 0);
|
||||||
|
*_errno() = EINVAL;
|
||||||
|
FreeLibrary(hadvapi32);
|
||||||
|
return EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
FreeLibrary(hadvapi32);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue