]> git.imager.perl.org - imager.git/commitdiff
don't leak in DSO_open() on various failures
authorTony Cook <tony@develop-help.com>
Mon, 25 Feb 2019 23:18:51 +0000 (10:18 +1100)
committerTony Cook <tony@develop-help.com>
Mon, 25 Feb 2019 23:18:51 +0000 (10:18 +1100)
DSO_close() now releases memory allocated for the DSO handle.

CID 185309.

dynaload.c

index faa605daa68b2cee038fa38a7472d6b40c40a0f0..522a495e8acc1cba46c9035689c1b24e63f470e5 100644 (file)
@@ -244,6 +244,7 @@ DSO_open(char* file,char** evalstring) {
   mm_log( (1,"DSO_open: going to dlsym '%s'\n", I_INSTALL_TABLES ));
   if ( (f = (void(*)(void *s,void *u))dlsym(d_handle, I_INSTALL_TABLES)) == NULL) {
     mm_log( (1,"DSO_open: dlsym didn't find '%s': %s.\n",I_INSTALL_TABLES,dlerror()) );
+    dlclose(d_handle);
     return NULL;
   }
 
@@ -257,16 +258,21 @@ DSO_open(char* file,char** evalstring) {
   mm_log( (1,"DSO_open: going to dlsym '%s'\n", I_FUNCTION_LIST ));
   if ( (function_list=(func_ptr *)dlsym(d_handle, I_FUNCTION_LIST)) == NULL) {
     mm_log( (1,"DSO_open: dlsym didn't find '%s': %s.\n",I_FUNCTION_LIST,dlerror()) );
+    dlclose(d_handle);
     return NULL;
   }
-  
-  if ( (dso_handle=(DSO_handle*)malloc(sizeof(DSO_handle))) == NULL) /* checked 17jul05 tonyc */
+
+  /* checked 17jul05 tonyc */
+  if ( (dso_handle=(DSO_handle*)malloc(sizeof(DSO_handle))) == NULL) {
+    dlclose(d_handle);
     return NULL;
+  }
   
   dso_handle->handle=d_handle; /* needed to close again */
   dso_handle->function_list=function_list;
   if ( (dso_handle->filename=(char*)malloc(strlen(file)+1)) == NULL) { /* checked 17jul05 tonyc */
     free(dso_handle); 
+    dlclose(d_handle);
     return NULL;
   }
   strcpy(dso_handle->filename,file);
@@ -278,9 +284,13 @@ DSO_open(char* file,char** evalstring) {
 undef_int
 DSO_close(void *ptr) {
   DSO_handle *handle;
+  undef_int result;
   mm_log((1,"DSO_close(ptr %p)\n",ptr));
   handle=(DSO_handle*) ptr;
-  return !dlclose(handle->handle);
+  result = !dlclose(handle->handle);
+  free(handle->filename);
+  free(handle);
+  return result;
 }
 
 #endif