From e29b6eb5d2f81298a1d214cf1c3dc0653d6b900b Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sun, 8 May 2022 08:51:45 +0100 Subject: [PATCH] Memory handling fixes when taking demo screenshots. save_screenshot() returns a dynamically allocated error message in case of failure, and Coverity complained of a memory leak when it was ignored in putty.c. The memory leak is trivial, because we were about to terminate the process with an error anyway. But it's a good point that I forgot to report the error! Not critical enough to fix on 0.77 (where Coverity found it), but we might as well make it look sensible on main. --- windows/dialog.c | 6 ++++-- windows/putty.c | 6 +++++- windows/puttygen.c | 6 ++++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/windows/dialog.c b/windows/dialog.c index 747b5336..f65c8a61 100644 --- a/windows/dialog.c +++ b/windows/dialog.c @@ -621,11 +621,13 @@ static INT_PTR GenericMainDlgProc(HWND hwnd, UINT msg, WPARAM wParam, if (dialog_box_demo_screenshot_filename && (UINT_PTR)wParam == DEMO_SCREENSHOT_TIMER_ID) { KillTimer(hwnd, DEMO_SCREENSHOT_TIMER_ID); - const char *err = save_screenshot( + char *err = save_screenshot( hwnd, dialog_box_demo_screenshot_filename); - if (err) + if (err) { MessageBox(hwnd, err, "Demo screenshot failure", MB_OK | MB_ICONERROR); + sfree(err); + } ShinyEndDialog(hwnd, 0); } return 0; diff --git a/windows/putty.c b/windows/putty.c index 53c8b4a5..823d0a30 100644 --- a/windows/putty.c +++ b/windows/putty.c @@ -180,7 +180,11 @@ const wchar_t *get_app_user_model_id(void) static void demo_terminal_screenshot(void *ctx, unsigned long now) { HWND hwnd = (HWND)ctx; - save_screenshot(hwnd, terminal_demo_screenshot_filename); + char *err = save_screenshot(hwnd, terminal_demo_screenshot_filename); + if (err) { + MessageBox(hwnd, err, "Demo screenshot failure", MB_OK | MB_ICONERROR); + sfree(err); + } cleanup_exit(0); } diff --git a/windows/puttygen.c b/windows/puttygen.c index 5beaed12..ea3557b0 100644 --- a/windows/puttygen.c +++ b/windows/puttygen.c @@ -1592,10 +1592,12 @@ static INT_PTR CALLBACK MainDlgProc(HWND hwnd, UINT msg, case WM_TIMER: if ((UINT_PTR)wParam == DEMO_SCREENSHOT_TIMER_ID) { KillTimer(hwnd, DEMO_SCREENSHOT_TIMER_ID); - const char *err = save_screenshot(hwnd, demo_screenshot_filename); - if (err) + char *err = save_screenshot(hwnd, demo_screenshot_filename); + if (err) { MessageBox(hwnd, err, "Demo screenshot failure", MB_OK | MB_ICONERROR); + sfree(err); + } EndDialog(hwnd, 0); } return 0;