From 57f69ad77d4bb5cbc29ff8c27f02ae3c3201f850 Mon Sep 17 00:00:00 2001 From: akallabeth Date: Wed, 25 Sep 2024 03:05:21 +0200 Subject: [PATCH] [winpr,synch] fix integer narrow --- winpr/libwinpr/synch/test/TestSynchBarrier.c | 48 ++++++++++---------- winpr/libwinpr/synch/timer.c | 4 +- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/winpr/libwinpr/synch/test/TestSynchBarrier.c b/winpr/libwinpr/synch/test/TestSynchBarrier.c index bbbc99310..835ce7058 100644 --- a/winpr/libwinpr/synch/test/TestSynchBarrier.c +++ b/winpr/libwinpr/synch/test/TestSynchBarrier.c @@ -70,49 +70,47 @@ out: static BOOL TestSynchBarrierWithFlags(DWORD dwFlags, DWORD dwThreads, DWORD dwLoops) { - HANDLE* threads = NULL; - struct test_params p; + BOOL rc = FALSE; DWORD dwStatus = 0; - DWORD expectedTrueCount = 0; - DWORD expectedFalseCount = 0; - p.threadCount = 0; - p.trueCount = 0; - p.falseCount = 0; - p.loops = dwLoops; - p.flags = dwFlags; - expectedTrueCount = dwLoops; - expectedFalseCount = dwLoops * (dwThreads - 1); + + struct test_params p = { + .threadCount = 0, .trueCount = 0, .falseCount = 0, .loops = dwLoops, .flags = dwFlags + }; + DWORD expectedTrueCount = dwLoops; + DWORD expectedFalseCount = dwLoops * (dwThreads - 1); + printf("%s: >> Testing with flags 0x%08" PRIx32 ". Using %" PRIu32 " threads performing %" PRIu32 " loops\n", __func__, dwFlags, dwThreads, dwLoops); - if (!(threads = calloc(dwThreads, sizeof(HANDLE)))) + HANDLE* threads = (HANDLE*)calloc(dwThreads, sizeof(HANDLE)); + if (!threads) { printf("%s: error allocatin thread array memory\n", __func__); return FALSE; } - if (!InitializeSynchronizationBarrier(&gBarrier, dwThreads, -1)) + if (dwThreads > INT32_MAX) + goto fail; + + if (!InitializeSynchronizationBarrier(&gBarrier, (LONG)dwThreads, -1)) { printf("%s: InitializeSynchronizationBarrier failed. GetLastError() = 0x%08x", __func__, GetLastError()); - free(threads); - DeleteSynchronizationBarrier(&gBarrier); - return FALSE; + goto fail; } if (!(gStartEvent = CreateEvent(NULL, TRUE, FALSE, NULL))) { printf("%s: CreateEvent failed with error 0x%08x", __func__, GetLastError()); - free(threads); - DeleteSynchronizationBarrier(&gBarrier); - return FALSE; + goto fail; } DWORD i = 0; for (; i < dwThreads; i++) { - if (!(threads[i] = CreateThread(NULL, 0, test_synch_barrier_thread, &p, 0, NULL))) + threads[i] = CreateThread(NULL, 0, test_synch_barrier_thread, &p, 0, NULL); + if (!threads[i]) { printf("%s: CreateThread failed for thread #%" PRIu32 " with error 0x%08x\n", __func__, i, GetLastError()); @@ -149,8 +147,6 @@ static BOOL TestSynchBarrierWithFlags(DWORD dwFlags, DWORD dwThreads, DWORD dwLo } } - free(threads); - if (!CloseHandle(gStartEvent)) { printf("%s: CloseHandle(gStartEvent) failed with error = 0x%08x)\n", __func__, @@ -158,8 +154,6 @@ static BOOL TestSynchBarrierWithFlags(DWORD dwFlags, DWORD dwThreads, DWORD dwLo InterlockedIncrement(&gErrorCount); } - DeleteSynchronizationBarrier(&gBarrier); - if (p.threadCount != (INT64)dwThreads) InterlockedIncrement(&gErrorCount); @@ -177,13 +171,17 @@ static BOOL TestSynchBarrierWithFlags(DWORD dwFlags, DWORD dwThreads, DWORD dwLo printf("%s: false count: %" PRId32 " (expected %" PRIu32 ")\n", __func__, p.falseCount, expectedFalseCount); + rc = TRUE; +fail: + free((void*)threads); + DeleteSynchronizationBarrier(&gBarrier); if (gErrorCount > 0) { printf("%s: Error test failed with %" PRId32 " reported errors\n", __func__, gErrorCount); return FALSE; } - return TRUE; + return rc; } int TestSynchBarrier(int argc, char* argv[]) diff --git a/winpr/libwinpr/synch/timer.c b/winpr/libwinpr/synch/timer.c index 5c1564ef7..6b40de177 100644 --- a/winpr/libwinpr/synch/timer.c +++ b/winpr/libwinpr/synch/timer.c @@ -515,8 +515,8 @@ BOOL SetWaitableTimer(HANDLE hTimer, const LARGE_INTEGER* lpDueTime, LONG lPerio if (lPeriod > 0) { - timer->timeout.it_interval.tv_sec = (lPeriod / 1000ULL); /* seconds */ - timer->timeout.it_interval.tv_nsec = (1000000ULL * (lPeriod % 1000ULL)); /* nanoseconds */ + timer->timeout.it_interval.tv_sec = (lPeriod / 1000LL); /* seconds */ + timer->timeout.it_interval.tv_nsec = (1000000LL * (lPeriod % 1000LL)); /* nanoseconds */ } if (lpDueTime->QuadPart != 0)