From c727be4df96d2f01af2fe1905c567c5a16fe1ae8 Mon Sep 17 00:00:00 2001 From: mugitya03 Date: Sun, 9 Mar 2025 16:47:27 -0400 Subject: [PATCH] doc: free lib pointer before function return (#4720) In function main, the pointer lib allocated at line 7 is passed as an argument to functions uv_dlopen at line 10, uv_dlerror at lines 11 and 17, and uv_dlsym at line 16, but it is never freed before the function returns at line 24. This results in a memory leak bug. --- docs/code/plugin/main.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/docs/code/plugin/main.c b/docs/code/plugin/main.c index 06e581e6..01335bbc 100644 --- a/docs/code/plugin/main.c +++ b/docs/code/plugin/main.c @@ -18,22 +18,21 @@ int main(int argc, char **argv) { return 0; } - uv_lib_t *lib = (uv_lib_t*) malloc(sizeof(uv_lib_t)); + uv_lib_t lib; while (--argc) { fprintf(stderr, "Loading %s\n", argv[argc]); - if (uv_dlopen(argv[argc], lib)) { - fprintf(stderr, "Error: %s\n", uv_dlerror(lib)); + if (uv_dlopen(argv[argc], &lib)) { + fprintf(stderr, "Error: %s\n", uv_dlerror(&lib)); continue; } init_plugin_function init_plugin; - if (uv_dlsym(lib, "initialize", (void **) &init_plugin)) { - fprintf(stderr, "dlsym error: %s\n", uv_dlerror(lib)); + if (uv_dlsym(&lib, "initialize", (void **) &init_plugin)) { + fprintf(stderr, "dlsym error: %s\n", uv_dlerror(&lib)); continue; } init_plugin(); } - return 0; }