Author: pschweitzer Date: Fri Sep 4 16:41:51 2015 New Revision: 69005 URL: http://svn.reactos.org/svn/reactos?rev=69005&view=rev Log: [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 Modified: trunk/reactos/lib/sdk/crt/math/rand.c Modified: trunk/reactos/lib/sdk/crt/math/rand.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/math/rand.c?rev=69005&r1=69004&r2=69005&view=diff ============================================================================== --- trunk/reactos/lib/sdk/crt/math/rand.c [iso-8859-1] (original) +++ trunk/reactos/lib/sdk/crt/math/rand.c [iso-8859-1] Fri Sep 4 16:41:51 2015 @@ -33,17 +33,41 @@ int CDECL rand_s(unsigned int *pval) { BOOLEAN (WINAPI *pSystemFunction036)(PVOID, ULONG); // RtlGenRandom - HINSTANCE hadvapi32 = LoadLibraryA("advapi32.dll"); - int ret = 0; - pSystemFunction036 = (void*)GetProcAddress(hadvapi32, "SystemFunction036"); -#if 1 - if (!pval || (pSystemFunction036 && !pSystemFunction036(pval, sizeof(*pval)))) + HINSTANCE hadvapi32; + + if (!pval) { _invalid_parameter(NULL,_CRT_WIDE("rand_s"),_CRT_WIDE(__FILE__),__LINE__, 0); *_errno() = EINVAL; - ret = EINVAL; + return EINVAL; } -#endif - if(hadvapi32) FreeLibrary(hadvapi32); - return ret; + + *pval = 0; + 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; }