From 4099bd4e4582223f45c1250ddb6bf3e232d27ed0 Mon Sep 17 00:00:00 2001 From: akallabeth Date: Tue, 2 Jul 2024 12:22:14 +0200 Subject: [PATCH] [warnings] integer casts/checks --- channels/parallel/client/parallel_main.c | 2 +- channels/rdpgfx/client/rdpgfx_main.c | 5 ++++ client/SDL/common/aad/wrapper/webview.h | 2 +- uwac/libuwac/uwac-clipboard.c | 24 +++++++++---------- uwac/libuwac/uwac-window.c | 24 ++++++++++++------- winpr/libwinpr/utils/collections/StreamPool.c | 7 +++--- 6 files changed, 38 insertions(+), 26 deletions(-) diff --git a/channels/parallel/client/parallel_main.c b/channels/parallel/client/parallel_main.c index 8f4d3c77a..ce574b87b 100644 --- a/channels/parallel/client/parallel_main.c +++ b/channels/parallel/client/parallel_main.c @@ -231,7 +231,7 @@ static UINT parallel_process_irp_write(PARALLEL_DEVICE* parallel, IRP* irp) { status = write(parallel->file, ptr, len); - if (status < 0) + if ((status < 0) || (status > len)) { irp->IoStatus = STATUS_UNSUCCESSFUL; Length = 0; diff --git a/channels/rdpgfx/client/rdpgfx_main.c b/channels/rdpgfx/client/rdpgfx_main.c index a63a6fe38..1b4e040fe 100644 --- a/channels/rdpgfx/client/rdpgfx_main.c +++ b/channels/rdpgfx/client/rdpgfx_main.c @@ -874,6 +874,11 @@ static UINT rdpgfx_send_cache_offer(RDPGFX_PLUGIN* gfx) } count = persistent_cache_get_count(persistent); + if (count < 0) + { + error = ERROR_INVALID_DATA; + goto fail; + } if (count >= RDPGFX_CACHE_ENTRY_MAX_COUNT) count = RDPGFX_CACHE_ENTRY_MAX_COUNT - 1; diff --git a/client/SDL/common/aad/wrapper/webview.h b/client/SDL/common/aad/wrapper/webview.h index 795f05e99..f348ef012 100644 --- a/client/SDL/common/aad/wrapper/webview.h +++ b/client/SDL/common/aad/wrapper/webview.h @@ -527,7 +527,7 @@ namespace webview int n = json_unescape(value, value_sz, nullptr); if (n > 0) { - char* decoded = new char[n + 1]; + char* decoded = new char[1ull + n]; json_unescape(value, value_sz, decoded); std::string result(decoded, n); delete[] decoded; diff --git a/uwac/libuwac/uwac-clipboard.c b/uwac/libuwac/uwac-clipboard.c index 216d70a23..727761dca 100644 --- a/uwac/libuwac/uwac-clipboard.c +++ b/uwac/libuwac/uwac-clipboard.c @@ -230,7 +230,7 @@ void* UwacClipboardDataGet(UwacSeat* seat, const char* mime, size_t* size) size_t alloc = 0; size_t pos = 0; char* data = NULL; - int pipefd[2]; + int pipefd[2] = { 0 }; if (!seat || !mime || !size || !seat->offer) return NULL; @@ -250,26 +250,21 @@ void* UwacClipboardDataGet(UwacSeat* seat, const char* mime, size_t* size) alloc += 1024; tmp = xrealloc(data, alloc); if (!tmp) - { - free(data); - close(pipefd[0]); - return NULL; - } + goto fail; data = tmp; + + if (pos > alloc) + goto fail; + r = read(pipefd[0], &data[pos], alloc - pos); if (r > 0) pos += r; if (r < 0) - { - free(data); - close(pipefd[0]); - return NULL; - } + goto fail; } while (r > 0); close(pipefd[0]); - close(pipefd[1]); if (alloc > 0) { @@ -277,4 +272,9 @@ void* UwacClipboardDataGet(UwacSeat* seat, const char* mime, size_t* size) *size = pos + 1; } return data; + +fail: + free(data); + close(pipefd[0]); + return NULL; } diff --git a/uwac/libuwac/uwac-window.c b/uwac/libuwac/uwac-window.c index 7a3696f51..b4664ded1 100644 --- a/uwac/libuwac/uwac-window.c +++ b/uwac/libuwac/uwac-window.c @@ -78,8 +78,8 @@ static void UwacWindowDestroyBuffers(UwacWindow* w) w->buffers = NULL; } -static int UwacWindowShmAllocBuffers(UwacWindow* w, int nbuffers, int allocSize, uint32_t width, - uint32_t height, enum wl_shm_format format); +static int UwacWindowShmAllocBuffers(UwacWindow* w, int64_t nbuffers, int64_t allocSize, + uint32_t width, uint32_t height, enum wl_shm_format format); static void xdg_handle_toplevel_configure(void* data, struct xdg_toplevel* xdg_toplevel, int32_t width, int32_t height, struct wl_array* states) @@ -316,22 +316,28 @@ static void shell_popup_done(void* data, struct wl_shell_surface* surface) static const struct wl_shell_surface_listener shell_listener = { shell_ping, shell_configure, shell_popup_done }; -int UwacWindowShmAllocBuffers(UwacWindow* w, int nbuffers, int allocSize, uint32_t width, +int UwacWindowShmAllocBuffers(UwacWindow* w, int64_t nbuffers, int64_t allocSize, uint32_t width, uint32_t height, enum wl_shm_format format) { int ret = UWAC_SUCCESS; - UwacBuffer* newBuffers = NULL; int fd = 0; void* data = NULL; struct wl_shm_pool* pool = NULL; - size_t pagesize = sysconf(_SC_PAGESIZE); - newBuffers = xrealloc(w->buffers, (w->nbuffers + nbuffers) * sizeof(UwacBuffer)); - - if (!newBuffers) + int64_t pagesize = sysconf(_SC_PAGESIZE); + if (pagesize <= 0) return UWAC_ERROR_NOMEMORY; /* round up to a multiple of PAGESIZE to page align data for each buffer */ - allocSize = (allocSize + pagesize - 1) & ~(pagesize - 1); + UINT64 test = (0ull + allocSize + pagesize - 1ull) & ~(pagesize - 1); + if (test > INT64_MAX) + return UWAC_ERROR_NOMEMORY; + + allocSize = (int64_t)test; + + UwacBuffer* newBuffers = xrealloc(w->buffers, (w->nbuffers + nbuffers) * sizeof(UwacBuffer)); + + if (!newBuffers) + return UWAC_ERROR_NOMEMORY; w->buffers = newBuffers; memset(w->buffers + w->nbuffers, 0, sizeof(UwacBuffer) * nbuffers); diff --git a/winpr/libwinpr/utils/collections/StreamPool.c b/winpr/libwinpr/utils/collections/StreamPool.c index 3ff113af8..c447c8cbe 100644 --- a/winpr/libwinpr/utils/collections/StreamPool.c +++ b/winpr/libwinpr/utils/collections/StreamPool.c @@ -117,7 +117,8 @@ static void StreamPool_ShiftUsed(wStreamPool* pool, size_t index, INT64 count) else if (count < 0) { const size_t pcount = (size_t)-count; - if ((pool->uSize - index - pcount) > 0) + const size_t off = index + pcount; + if (pool->uSize > off) { MoveMemory(&pool->uArray[index], &pool->uArray[index + pcount], (pool->uSize - index - pcount) * sizeof(wStream*)); @@ -169,8 +170,8 @@ static void StreamPool_ShiftAvailable(wStreamPool* pool, size_t index, INT64 cou else if (count < 0) { const size_t pcount = (size_t)-count; - - if ((pool->aSize - index - pcount) > 0) + const size_t off = index + pcount; + if (pool->aSize > off) { MoveMemory(&pool->aArray[index], &pool->aArray[index + pcount], (pool->aSize - index - pcount) * sizeof(wStream*));