Re: [MemLeak] in dln.c

From: nobu.nokada@...
Date: 2002-08-31 00:01:00 UTC
List: ruby-core #398
Hi,

At Fri, 30 Aug 2002 23:38:27 +0900,
Michal Rokos wrote:
>      if ((init_fct = (void(*)())GetProcAddress(handle, buf)) == NULL) {
> -	rb_loaderror("%s - %s\n%s", dln_strerror(), buf, file);
> +	rb_loaderror("%s - %s\n%s", dln_strerror(), buf, file); /* FIXME: MEMLEAK for buf */
>      }
>      free(buf);

This looks tiresome.

Which do you prefer?

(1) copy it to ALLOCAed string if necessary.

diff -u -2 -p -r1.38 dln.c
--- dln.c	25 Jun 2002 09:44:57 -0000	1.38
+++ dln.c	30 Aug 2002 23:56:30 -0000
@@ -1236,4 +1236,6 @@ aix_loaderror(const char *pathname)
 #endif
 
+#define ASTRDUP(s) do {char *tmp = strcpy(ALLOCA_N(char, strlen(s) + 1)), free(s), (s) = tmp} while (0)
+
 void*
 dln_load(file)
@@ -1265,4 +1267,5 @@ dln_load(file)
 
     if ((init_fct = (void(*)())GetProcAddress(handle, buf)) == NULL) {
+	ASTRDUP(buf);
 	rb_loaderror("%s - %s\n%s", dln_strerror(), buf, file);
     }
@@ -1473,5 +1476,5 @@ dln_load(file)
 	char errmsg[] = "Internal of BeOS version. %.200s (symbol_name = %s)";
 	unload_add_on(img_id);
-	free(buf);
+	ASTRDUP(buf);
 	rb_loaderror(errmsg, strerror(err_stat), buf);
       }


(2) always copy it to ALLOCAed string and remove all free(buf)
    lines.

diff -u -2 -p -r1.38 dln.c
--- dln.c	25 Jun 2002 09:44:57 -0000	1.38
+++ dln.c	30 Aug 2002 23:39:55 -0000
@@ -105,5 +105,5 @@ int eaccess();
 
 static void
-init_funcname(buf, file)
+init_funcname_len(buf, file)
     char **buf;
     char *file;
@@ -132,5 +132,12 @@ init_funcname(buf, file)
 	}
     }
+    return p - *buf;
 }
+#define init_funcname(buf, file) do {			\
+    int len = init_funcname_len(buf, file);		\
+    char *tmp = strcpy(ALLOCA(char, len+1), (*buf));	\
+    free(*buf);						\
+    *(buf) = tmp;					\
+} while (0)
 
 #ifdef USE_DLN_A_OUT


(3) make init_funcname() to return VALUE and caller to hold it,
    and remove all free(buf) lines.

diff -u -2 -p -r1.38 dln.c
--- dln.c	25 Jun 2002 09:44:57 -0000	1.38
+++ dln.c	30 Aug 2002 23:42:40 -0000
@@ -104,6 +104,6 @@ int eaccess();
 #endif
 
-static void
-init_funcname(buf, file)
+static VALUE
+init_funcname(file)
     char **buf;
     char *file;
@@ -132,4 +132,5 @@ init_funcname(buf, file)
 	}
     }
+    return Data_Wrap_Struct(rb_cData, 0, -1, *buf);
 }
 

-- 
Nobu Nakada

In This Thread