From ecc18608adf7460247fb83431e9b5c82c55c97cf Mon Sep 17 00:00:00 2001 From: tsz8899 Date: Sat, 7 Feb 2026 02:37:59 +0800 Subject: [PATCH] [client,windows] Enhance memory safety with NULL checks and resource protection --- client/Windows/wf_client.c | 6 ++++++ client/Windows/wf_gdi.c | 18 +++++++++++++++++- client/Windows/wf_graphics.c | 12 ++++++++---- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/client/Windows/wf_client.c b/client/Windows/wf_client.c index d6756a31d..ca2acdd87 100644 --- a/client/Windows/wf_client.c +++ b/client/Windows/wf_client.c @@ -418,6 +418,12 @@ static BOOL wf_post_connect(freerdp* instance) wf_image_new(wfc, freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth), freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight), format, NULL); + if (!wfc->primary) + { + WLog_ERR(TAG, "Failed to allocate primary surface"); + return FALSE; + } + if (!gdi_init_ex(instance, format, 0, wfc->primary->pdata, NULL)) return FALSE; diff --git a/client/Windows/wf_gdi.c b/client/Windows/wf_gdi.c index 4dc964709..ac5f27b92 100644 --- a/client/Windows/wf_gdi.c +++ b/client/Windows/wf_gdi.c @@ -118,6 +118,8 @@ static wfBitmap* wf_glyph_new(wfContext* wfc, GLYPH_DATA* glyph) { wfBitmap* glyph_bmp; glyph_bmp = wf_image_new(wfc, glyph->cx, glyph->cy, PIXEL_FORMAT_MONO, glyph->aj); + if (!glyph_bmp) + WLog_ERR(TAG, "wf_image_new failed for glyph"); return glyph_bmp; } @@ -131,6 +133,11 @@ static BYTE* wf_glyph_convert(wfContext* wfc, int width, int height, const BYTE* const int src_bytes_per_row = (width + 7) / 8; const int dst_bytes_per_row = src_bytes_per_row + (src_bytes_per_row % 2); BYTE* cdata = (BYTE*)malloc(dst_bytes_per_row * height); + if (!cdata) + { + WLog_ERR(TAG, "malloc failed for cdata buffer"); + return NULL; + } const BYTE* src = data; for (int indexy = 0; indexy < height; indexy++) @@ -654,6 +661,7 @@ static BOOL wf_gdi_line_to(rdpContext* context, const LINE_TO_ORDER* line_to) static BOOL wf_gdi_polyline(rdpContext* context, const POLYLINE_ORDER* polyline) { + BOOL rc = FALSE; int org_rop2; HPEN hpen; HPEN org_hpen; @@ -677,6 +685,11 @@ static BOOL wf_gdi_polyline(rdpContext* context, const POLYLINE_ORDER* polyline) int numPoints; numPoints = polyline->numDeltaEntries + 1; pts = (POINT*)malloc(sizeof(POINT) * numPoints); + if (!pts) + { + WLog_ERR(TAG, "malloc failed for polyline points"); + goto fail; + } pts[0].x = temp.x = polyline->xStart; pts[0].y = temp.y = polyline->yStart; @@ -696,10 +709,13 @@ static BOOL wf_gdi_polyline(rdpContext* context, const POLYLINE_ORDER* polyline) free(pts); } + rc = TRUE; + +fail: SelectObject(wfc->drawing->hdc, org_hpen); wf_set_rop2(wfc->drawing->hdc, org_rop2); DeleteObject(hpen); - return TRUE; + return rc; } static BOOL wf_gdi_memblt(rdpContext* context, MEMBLT_ORDER* memblt) diff --git a/client/Windows/wf_graphics.c b/client/Windows/wf_graphics.c index a60054a50..00fac3035 100644 --- a/client/Windows/wf_graphics.c +++ b/client/Windows/wf_graphics.c @@ -68,10 +68,14 @@ HBITMAP wf_create_dib(wfContext* wfc, UINT32 width, UINT32 height, UINT32 srcFor wfBitmap* wf_image_new(wfContext* wfc, UINT32 width, UINT32 height, UINT32 format, const BYTE* data) { - HDC hdc; - wfBitmap* image; - hdc = GetDC(NULL); - image = (wfBitmap*)malloc(sizeof(wfBitmap)); + wfBitmap* image = (wfBitmap*)malloc(sizeof(wfBitmap)); + if (!image) + { + WLog_ERR(TAG, "malloc failed for wfBitmap"); + return NULL; + } + + HDC hdc = GetDC(NULL); image->hdc = CreateCompatibleDC(hdc); image->bitmap = wf_create_dib(wfc, width, height, format, data, &(image->pdata)); image->org_bitmap = (HBITMAP)SelectObject(image->hdc, image->bitmap);