diff --git a/libfreerdp/codec/test/TestFreeRDPCodecPlanar.c b/libfreerdp/codec/test/TestFreeRDPCodecPlanar.c index 4f692df68..772665ec2 100644 --- a/libfreerdp/codec/test/TestFreeRDPCodecPlanar.c +++ b/libfreerdp/codec/test/TestFreeRDPCodecPlanar.c @@ -5757,7 +5757,7 @@ static BOOL FuzzPlanar(void) ", nDstHeight=%" PRIu32 ", nDstStep=%" PRIu32 ", total size=%" PRIuz "\n", FreeRDPGetColorFormatName(DstFormat), nXDst, nYDst, nDstWidth, nDstHeight, nDstStep, sizeof(dstData)); - freerdp_planar_switch_bgr(planar, rand() % 2); + freerdp_planar_switch_bgr(planar, prand(2) % 2); planar_decompress(planar, data, dataSize, prand(4096), prand(4096), dstData, DstFormat, nDstStep, nXDst, nYDst, nDstWidth, nDstHeight, prand(2)); } diff --git a/libfreerdp/gdi/test/TestGdiCreate.c b/libfreerdp/gdi/test/TestGdiCreate.c index 01c113f81..4b042b561 100644 --- a/libfreerdp/gdi/test/TestGdiCreate.c +++ b/libfreerdp/gdi/test/TestGdiCreate.c @@ -7,6 +7,7 @@ #include #include +#include #include "line.h" #include "brush.h" @@ -324,6 +325,13 @@ fail: return rc; } +static BYTE prand(void) +{ + BYTE tmp = 0; + winpr_RAND(&tmp, sizeof(tmp)); + return tmp; +} + static BOOL test_gdi_GetPixel(void) { BOOL rc = TRUE; @@ -360,7 +368,7 @@ static BOOL test_gdi_GetPixel(void) { UINT32 pixel = 0; const UINT32 color = - FreeRDPGetColor(hBitmap->format, rand(), rand(), rand(), rand()); + FreeRDPGetColor(hBitmap->format, prand(), prand(), prand(), prand()); FreeRDPWriteColor(&hBitmap->data[i * hBitmap->scanline + j * bpp], hBitmap->format, color); pixel = gdi_GetPixel(hdc, j, i); @@ -412,7 +420,7 @@ static BOOL test_gdi_SetPixel(void) { UINT32 pixel = 0; const UINT32 color = - FreeRDPGetColor(hBitmap->format, rand(), rand(), rand(), rand()); + FreeRDPGetColor(hBitmap->format, prand(), prand(), prand(), prand()); gdi_SetPixel(hdc, j, i, color); pixel = FreeRDPReadColor(&hBitmap->data[i * hBitmap->scanline + j * bpp], hBitmap->format); diff --git a/libfreerdp/primitives/test/TestPrimitivesYUV.c b/libfreerdp/primitives/test/TestPrimitivesYUV.c index c07d9d70f..5308b6b0c 100644 --- a/libfreerdp/primitives/test/TestPrimitivesYUV.c +++ b/libfreerdp/primitives/test/TestPrimitivesYUV.c @@ -687,6 +687,15 @@ static BOOL compare_yuv420(BYTE** planesA, BYTE** planesB, UINT32 width, UINT32 return rc; } +static UINT32 prand(UINT32 max) +{ + UINT32 tmp = 0; + if (max <= 1) + return 1; + winpr_RAND(&tmp, sizeof(tmp)); + return tmp % (max - 1) + 1; +} + static BOOL TestPrimitiveRgbToLumaChroma(primitives_t* prims, prim_size_t roi, UINT32 version) { BOOL res = FALSE; @@ -772,10 +781,10 @@ static BOOL TestPrimitiveRgbToLumaChroma(primitives_t* prims, prim_size_t roi, U for (UINT32 x = 0; x < roi.width; x++) { #if 1 - line[x * 4 + 0] = rand(); - line[x * 4 + 1] = rand(); - line[x * 4 + 2] = rand(); - line[x * 4 + 3] = rand(); + line[x * 4 + 0] = prand(UINT8_MAX); + line[x * 4 + 1] = prand(UINT8_MAX); + line[x * 4 + 2] = prand(UINT8_MAX); + line[x * 4 + 3] = prand(UINT8_MAX); #else line[x * 4 + 0] = (y * roi.width + x) * 16 + 5; line[x * 4 + 1] = (y * roi.width + x) * 16 + 7; diff --git a/winpr/libwinpr/error/test/TestErrorSetLastError.c b/winpr/libwinpr/error/test/TestErrorSetLastError.c index a3d57af02..cb1ad87c6 100644 --- a/winpr/libwinpr/error/test/TestErrorSetLastError.c +++ b/winpr/libwinpr/error/test/TestErrorSetLastError.c @@ -19,6 +19,7 @@ */ #include +#include #include #include #include @@ -31,6 +32,15 @@ static int status = 0; static LONG* pLoopCount = NULL; static BOOL bStopTest = FALSE; +static UINT32 prand(UINT32 max) +{ + UINT32 tmp = 0; + if (max <= 1) + return 1; + winpr_RAND(&tmp, sizeof(tmp)); + return tmp % (max - 1) + 1; +} + static DWORD WINAPI test_error_thread(LPVOID arg) { int id = 0; @@ -41,7 +51,7 @@ static DWORD WINAPI test_error_thread(LPVOID arg) do { - dwErrorSet = (DWORD)abs(rand()) + 1; + dwErrorSet = prand(UINT32_MAX - 1) + 1; SetLastError(dwErrorSet); if ((dwErrorGet = GetLastError()) != dwErrorSet) { diff --git a/winpr/libwinpr/path/test/TestPathMakePath.c b/winpr/libwinpr/path/test/TestPathMakePath.c index af4f3fff1..36fbaaf05 100644 --- a/winpr/libwinpr/path/test/TestPathMakePath.c +++ b/winpr/libwinpr/path/test/TestPathMakePath.c @@ -3,9 +3,19 @@ #include #include +#include #include #include +static UINT32 prand(UINT32 max) +{ + UINT32 tmp = 0; + if (max <= 1) + return 1; + winpr_RAND(&tmp, sizeof(tmp)); + return tmp % (max - 1) + 1; +} + int TestPathMakePath(int argc, char* argv[]) { size_t baseLen = 0; @@ -26,11 +36,10 @@ int TestPathMakePath(int argc, char* argv[]) } baseLen = strlen(base); - srand(time(NULL)); for (int x = 0; x < 5; x++) { - (void)sprintf_s(tmp, ARRAYSIZE(tmp), "%08X", rand()); + (void)sprintf_s(tmp, ARRAYSIZE(tmp), "%08" PRIX32, prand(UINT32_MAX)); path = GetCombinedPath(base, tmp); free(base); diff --git a/winpr/libwinpr/synch/test/TestSynchBarrier.c b/winpr/libwinpr/synch/test/TestSynchBarrier.c index 5bca01964..bbbc99310 100644 --- a/winpr/libwinpr/synch/test/TestSynchBarrier.c +++ b/winpr/libwinpr/synch/test/TestSynchBarrier.c @@ -1,5 +1,6 @@ #include +#include #include #include #include @@ -22,6 +23,15 @@ struct test_params DWORD flags; }; +static UINT32 prand(UINT32 max) +{ + UINT32 tmp = 0; + if (max <= 1) + return 1; + winpr_RAND(&tmp, sizeof(tmp)); + return tmp % (max - 1) + 1; +} + static DWORD WINAPI test_synch_barrier_thread(LPVOID lpParam) { BOOL status = FALSE; @@ -43,7 +53,7 @@ static DWORD WINAPI test_synch_barrier_thread(LPVOID lpParam) for (DWORD i = 0; i < p->loops && gErrorCount == 0; i++) { /* simulate different execution times before the barrier */ - Sleep(1 + abs((rand() % MAX_SLEEP_MS))); + Sleep(1 + prand(MAX_SLEEP_MS)); status = EnterSynchronizationBarrier(&gBarrier, p->flags); // printf("Thread #%03u status: %s\n", tnum, status ? "TRUE" : "FALSE"); diff --git a/winpr/libwinpr/synch/test/TestSynchCritical.c b/winpr/libwinpr/synch/test/TestSynchCritical.c index d78e01cf7..800622ff5 100644 --- a/winpr/libwinpr/synch/test/TestSynchCritical.c +++ b/winpr/libwinpr/synch/test/TestSynchCritical.c @@ -1,6 +1,7 @@ #include #include +#include #include #include #include @@ -40,6 +41,15 @@ static BOOL TestSynchCritical_TriggerAndCheckRaceCondition(HANDLE OwningThread, return TRUE; } +static UINT32 prand(UINT32 max) +{ + UINT32 tmp = 0; + if (max <= 1) + return 1; + winpr_RAND(&tmp, sizeof(tmp)); + return tmp % (max - 1) + 1; +} + /* this thread function shall increment the global dwTestValue until the PBOOL passsed in arg is * FALSE */ static DWORD WINAPI TestSynchCritical_Test1(LPVOID arg) @@ -59,14 +69,14 @@ static DWORD WINAPI TestSynchCritical_Test1(LPVOID arg) return 1; /* add some random recursion level */ - int j = rand() % 5; - for (int i = 0; i < j; i++) + UINT32 j = prand(5); + for (UINT32 i = 0; i < j; i++) { if (!TestSynchCritical_TriggerAndCheckRaceCondition(hThread, rc++)) return 2; EnterCriticalSection(&critical); } - for (int i = 0; i < j; i++) + for (UINT32 i = 0; i < j; i++) { if (!TestSynchCritical_TriggerAndCheckRaceCondition(hThread, rc--)) return 2; diff --git a/winpr/libwinpr/synch/test/TestSynchInit.c b/winpr/libwinpr/synch/test/TestSynchInit.c index 582a5cdf4..2baee47a9 100644 --- a/winpr/libwinpr/synch/test/TestSynchInit.c +++ b/winpr/libwinpr/synch/test/TestSynchInit.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -15,6 +16,15 @@ static LONG* pTestThreadFunctionCalls = NULL; static LONG* pTestOnceFunctionCalls = NULL; static LONG* pInitOnceExecuteOnceCalls = NULL; +static UINT32 prand(UINT32 max) +{ + UINT32 tmp = 0; + if (max <= 1) + return 1; + winpr_RAND(&tmp, sizeof(tmp)); + return tmp % (max - 1) + 1; +} + static BOOL CALLBACK TestOnceFunction(PINIT_ONCE once, PVOID param, PVOID* context) { LONG calls = InterlockedIncrement(pTestOnceFunctionCalls) - 1; @@ -24,7 +34,7 @@ static BOOL CALLBACK TestOnceFunction(PINIT_ONCE once, PVOID param, PVOID* conte WINPR_UNUSED(context); /* simulate execution time */ - Sleep(30 + rand() % 40); + Sleep(30 + prand(40)); if (calls < TEST_NUM_FAILURES) { diff --git a/winpr/libwinpr/synch/test/TestSynchMultipleThreads.c b/winpr/libwinpr/synch/test/TestSynchMultipleThreads.c index 7218b6482..34820f148 100644 --- a/winpr/libwinpr/synch/test/TestSynchMultipleThreads.c +++ b/winpr/libwinpr/synch/test/TestSynchMultipleThreads.c @@ -2,14 +2,24 @@ #include #include +#include #include #include #define THREADS 8 +static UINT32 prand(UINT32 max) +{ + UINT32 tmp = 0; + if (max <= 1) + return 1; + winpr_RAND(&tmp, sizeof(tmp)); + return tmp % (max - 1) + 1; +} + static DWORD WINAPI test_thread(LPVOID arg) { - long timeout = 50 + (rand() % 100); + UINT32 timeout = 50 + prand(100); WINPR_UNUSED(arg); Sleep(timeout); ExitThread(0);