From 2ae5162c1ca61c80af6d8d7adc339173887b0c28 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Deuc=D0=B5?= <shurd@sasktel.net>
Date: Thu, 2 Jan 2025 01:10:14 -0500
Subject: [PATCH] Make all DLL handles globals so Coverity stops complaining.

They're not "leaking" if I want them to liver forever. :D
This does prevent an extra needless load if User.dll though...
but sdlfuncs and win32gdi both still load the same two DLLs.
---
 src/conio/sdlfuncs.c  | 14 ++++++++++----
 src/conio/win32gdi.c  | 11 ++++++++---
 src/conio/x_cio.c     | 22 ++++++++++++++--------
 src/syncterm/libjxl.c |  9 ++++++---
 4 files changed, 38 insertions(+), 18 deletions(-)

diff --git a/src/conio/sdlfuncs.c b/src/conio/sdlfuncs.c
index c510724253..c76beddd42 100644
--- a/src/conio/sdlfuncs.c
+++ b/src/conio/sdlfuncs.c
@@ -21,13 +21,17 @@ static int sdl_funcs_loaded=0;
 
 static void QuitWrap(void);
 
+static dll_handle sdl_dll;
+#ifdef _WIN32
+static dll_handle userDLL;
+static dll_handle shcoreDLL;
+#endif
 int load_sdl_funcs(struct sdlfuncs *sdlf)
 {
-	dll_handle	sdl_dll;
 	const char *libnames[]={"SDL2", "SDL", NULL};
 
 	sdlf->gotfuncs=0;
-	if((sdl_dll=xp_dlopen(libnames,RTLD_LAZY|RTLD_GLOBAL,SDL_PATCHLEVEL))==NULL)
+	if(sdl_dll == NULL && (sdl_dll=xp_dlopen(libnames,RTLD_LAZY|RTLD_GLOBAL,SDL_PATCHLEVEL))==NULL)
 		return(-1);
 
 	if((sdlf->Init=xp_dlsym(sdl_dll, SDL_Init))==NULL) {
@@ -269,7 +273,8 @@ int init_sdl_video(void)
 	HRESULT(WINAPI *SetProcessDpiAwarenessContext)(enum D3_PROCESS_DPI_AWARENESS dpiAwareness) = NULL;
 
 	const char* user32dll[] = {"User32", NULL};
-	dll_handle userDLL = xp_dlopen(user32dll, RTLD_LAZY, 0);
+	if (!userDLL)
+		userDLL = xp_dlopen(user32dll, RTLD_LAZY, 0);
 
 	if (userDLL)
 	{
@@ -279,7 +284,8 @@ int init_sdl_video(void)
 
 
 	const char* shcoredll[] = {"SHCore", NULL};
-	dll_handle shcoreDLL = xp_dlopen(shcoredll, RTLD_LAZY, 0);
+	if (!shcoreDLL)
+		shcoreDLL = xp_dlopen(shcoredll, RTLD_LAZY, 0);
 
 	if (shcoreDLL)
 	{
diff --git a/src/conio/win32gdi.c b/src/conio/win32gdi.c
index 5428214dbb..6eaa62d775 100644
--- a/src/conio/win32gdi.c
+++ b/src/conio/win32gdi.c
@@ -195,6 +195,7 @@ sp_to_codepoint(uint16_t high, uint16_t low)
 	return (high - 0xd800) * 0x400 + (low - 0xdc00) + 0x10000;
 }
 
+static dll_handle userDLL;
 static BOOL
 gdiAdjustWindowRect(LPRECT r, DWORD style, BOOL menu, UINT dpi)
 {
@@ -205,7 +206,8 @@ gdiAdjustWindowRect(LPRECT r, DWORD style, BOOL menu, UINT dpi)
 
 	if (!gotPtr) {
 		const char* user32dll[] = {"User32", NULL};
-		dll_handle userDLL = xp_dlopen(user32dll, RTLD_LAZY, 0);
+		if (!userDLL)
+			userDLL = xp_dlopen(user32dll, RTLD_LAZY, 0);
 
 		if (userDLL) {
 			AWREFD = xp_dlsym(userDLL, AdjustWindowRectExForDpi);
@@ -1244,6 +1246,7 @@ gdi_get_window_info(int *width, int *height, int *xpos, int *ypos)
 	return(1);
 }
 
+static dll_handle shcoreDLL;
 int
 gdi_init(int mode)
 {
@@ -1269,7 +1272,8 @@ gdi_init(int mode)
 	HRESULT(WINAPI *SetProcessDpiAwarenessContext)(enum D3_PROCESS_DPI_AWARENESS dpiAwareness) = NULL;
 
 	const char* user32dll[] = {"User32", NULL};
-	dll_handle userDLL = xp_dlopen(user32dll, RTLD_LAZY, 0);
+	if (!userDLL)
+		userDLL = xp_dlopen(user32dll, RTLD_LAZY, 0);
 
 	if (userDLL)
 	{
@@ -1278,7 +1282,8 @@ gdi_init(int mode)
 	}
 
 	const char* shcoredll[] = {"SHCore", NULL};
-	dll_handle shcoreDLL = xp_dlopen(shcoredll, RTLD_LAZY, 0);
+	if (!shcoreDLL)
+		shcoreDLL = xp_dlopen(shcoredll, RTLD_LAZY, 0);
 
 	if (shcoreDLL)
 	{
diff --git a/src/conio/x_cio.c b/src/conio/x_cio.c
index fd37e0fda8..2bcc7ca298 100644
--- a/src/conio/x_cio.c
+++ b/src/conio/x_cio.c
@@ -182,20 +182,26 @@ void x11_mouse_thread(void *data)
 	}
 }
 
+static dll_handle dl;
+#ifdef WITH_XRENDER
+static dll_handle dl2;
+#endif
+#ifdef WITH_XINERAMA
+static dll_handle dl3;
+#endif
+#ifdef WITH_XRANDR
+static dll_handle dl4;
+#endif
 int x_initciolib(int mode)
 {
-	dll_handle	dl;
 	const char *libnames[]={"X11",NULL};
 #ifdef WITH_XRENDER
-	dll_handle	dl2;
 	const char *libnames2[]={"Xrender",NULL};
 #endif
 #ifdef WITH_XINERAMA
-	dll_handle	dl3;
 	const char *libnames3[]={"Xinerama",NULL};
 #endif
 #ifdef WITH_XRANDR
-	dll_handle	dl4;
 	const char *libnames4[]={"Xrandr",NULL};
 #endif
 	Status (*xit)(void);
@@ -216,7 +222,7 @@ int x_initciolib(int mode)
 		return(-1);
 
 	/* Load X11 functions */
-	if((dl=xp_dlopen(libnames,RTLD_LAZY,7))==NULL)
+	if(dl == NULL && (dl=xp_dlopen(libnames,RTLD_LAZY,7))==NULL)
 		return(-1);
 	if ((_Xdebug = xp_dlsym(dl,_Xdebug))!=NULL)
 		*_Xdebug=1;
@@ -520,7 +526,7 @@ int x_initciolib(int mode)
 	}
 #ifdef WITH_XRENDER
 	xrender_found = true;
-	if ((dl2 = xp_dlopen(libnames2,RTLD_LAZY,1)) == NULL)
+	if (dl2 == NULL && (dl2 = xp_dlopen(libnames2,RTLD_LAZY,1)) == NULL)
 		xrender_found = false;
 	if (xrender_found && ((x11.XRenderFindStandardFormat = xp_dlsym(dl2, XRenderFindStandardFormat)) == NULL)) {
 		xp_dlclose(dl2);
@@ -557,7 +563,7 @@ int x_initciolib(int mode)
 #endif
 #ifdef WITH_XINERAMA
 	xinerama_found = true;
-	if ((dl3 = xp_dlopen(libnames3,RTLD_LAZY,1)) == NULL)
+	if (dl3 == NULL && (dl3 = xp_dlopen(libnames3,RTLD_LAZY,1)) == NULL)
 		xinerama_found = false;
 	if (xinerama_found && ((x11.XineramaQueryVersion = xp_dlsym(dl3, XineramaQueryVersion)) == NULL)) {
 		xp_dlclose(dl3);
@@ -570,7 +576,7 @@ int x_initciolib(int mode)
 #endif
 #ifdef WITH_XRANDR
 	xrandr_found = true;
-	if ((dl4 = xp_dlopen(libnames4,RTLD_LAZY,2)) == NULL)
+	if (dl4 == NULL && (dl4 = xp_dlopen(libnames4,RTLD_LAZY,2)) == NULL)
 		xrandr_found = false;
 	if (xrandr_found && ((x11.XRRQueryVersion = xp_dlsym(dl4, XRRQueryVersion)) == NULL)) {
 		xp_dlclose(dl4);
diff --git a/src/syncterm/libjxl.c b/src/syncterm/libjxl.c
index 718e69e639..2446c8054a 100644
--- a/src/syncterm/libjxl.c
+++ b/src/syncterm/libjxl.c
@@ -36,9 +36,12 @@ bool load_jxl_funcs(void)
 	return true;
 }
 #else
+static dll_handle jxl_dll;
+#ifdef WITH_JPEG_XL_THREADS
+static dll_handle jxlt_dll;
+#endif
 bool load_jxl_funcs(void)
 {
-	dll_handle jxl_dll;
 	const char *libnames[] = {"jxl", NULL};
 
 	if (Jxl.status == JXL_STATUS_OK)
@@ -49,7 +52,7 @@ bool load_jxl_funcs(void)
 		return false;
 
 	Jxl.status = JXL_STATUS_FAILED;
-	if ((jxl_dll = xp_dlopen(libnames, RTLD_LAZY | RTLD_GLOBAL, JPEGXL_MAJOR_VERSION)) == NULL)
+	if (jxl_dll == NULL && (jxl_dll = xp_dlopen(libnames, RTLD_LAZY | RTLD_GLOBAL, JPEGXL_MAJOR_VERSION)) == NULL)
 		return false;
 
 	if ((Jxl.DecoderCloseInput = xp_dlsym(jxl_dll, JxlDecoderCloseInput)) == NULL) {
@@ -109,7 +112,7 @@ bool load_jxl_funcs(void)
 	const char *tlibnames[] = {"jxl_threads", NULL};
 
 	Jxl.status = JXL_STATUS_FAILED;
-	if ((jxlt_dll = xp_dlopen(tlibnames, RTLD_LAZY | RTLD_GLOBAL, JPEGXL_MAJOR_VERSION)) == NULL)
+	if (jxlt_dll == NULL && (jxlt_dll = xp_dlopen(tlibnames, RTLD_LAZY | RTLD_GLOBAL, JPEGXL_MAJOR_VERSION)) == NULL)
 		return true;
 
 	if ((Jxl.ResizableParallelRunner = xp_dlsym(jxlt_dll, JxlResizableParallelRunner)) == NULL) {
-- 
GitLab