[Xcb] mem leak with ecore_xcb

Barton C Massey bart at cs.pdx.edu
Wed Apr 11 16:37:39 PDT 2007


In message <20070411191203.GA17223 at code-monkey.de> you wrote:
> I don't see a fix in git.
> 
> The attached patch probably fixes the leak Vincent is seeing, I didn't
> test it though.

Your fix breaks (hypothetical) external callers of
XauFileName() that expect the previously-implemented
behavior.  (But no one would really call this thing, right?)
I've attached a rebased version of the patch I posted
previously.  I don't think I have commit permission on
libXau; I'll try to fix that now.  In the meantime, this
should be a solid kludge for the memory leak.

Sorry about all the confusion around this patch.

	Bart

-------------- next part --------------
cleaned up XauFileName() memory handling; split out XauOpenAuth()

---
commit e5f44ceac946184d0d6f1099ec85564b9ad58935
tree 00d76da4df1de24aaab70eb6afe309147ceb479a
parent 45d137fdbb9b21513aff68e945e19cdfdcbc28b1
author Bart Massey <bart at bart.cs.pdx.edu> Wed, 11 Apr 2007 16:32:45 -0700
committer Bart Massey <bart at bart.cs.pdx.edu> Wed, 11 Apr 2007 16:32:45 -0700

 AuFileName.c        |   15 ++++++++++++---
 AuGetAddr.c         |   11 ++---------
 AuGetBest.c         |    9 +--------
 Makefile.am         |    5 ++++-
 Xau.man             |   19 ++++++++++++++++---
 include/X11/Xauth.h |    4 ++++
 6 files changed, 39 insertions(+), 24 deletions(-)

diff --git a/AuFileName.c b/AuFileName.c
index b21b048..fb9b122 100644
--- a/AuFileName.c
+++ b/AuFileName.c
@@ -34,12 +34,13 @@ in this Software without prior written authorization from The Open Group.
 #include <X11/Xos.h>
 #include <stdlib.h>
 
+static char	*buf;
+
 char *
 XauFileName (void)
 {
     const char *slashDotXauthority = "/.Xauthority";
     char    *name;
-    static char	*buf;
     static int	bsize;
 #ifdef WIN32
     char    dir[128];
@@ -62,8 +63,7 @@ XauFileName (void)
     }
     size = strlen (name) + strlen(&slashDotXauthority[1]) + 2;
     if (size > bsize) {
-	if (buf)
-	    free (buf);
+        XauFreeFileName();
 	buf = malloc ((unsigned) size);
 	if (!buf)
 	    return NULL;
@@ -73,3 +73,12 @@ XauFileName (void)
     strcat (buf, slashDotXauthority + (name[1] == '\0' ? 1 : 0));
     return buf;
 }
+
+void
+XauFreeFileName (void)
+{
+  if (buf) {
+    free (buf);
+    buf = 0;
+  }
+}
diff --git a/AuGetAddr.c b/AuGetAddr.c
index 6275531..32c8f34 100644
--- a/AuGetAddr.c
+++ b/AuGetAddr.c
@@ -65,18 +65,11 @@ unsigned short	name_length,
 #endif
 _Xconst char*	name)
 {
-    FILE    *auth_file;
-    char    *auth_name;
+    FILE    *auth_file = XauOpenAuth();
     Xauth   *entry;
 
-    auth_name = XauFileName ();
-    if (!auth_name)
-	return NULL;
-    if (access (auth_name, R_OK) != 0)		/* checks REAL id */
-	return NULL;
-    auth_file = fopen (auth_name, "rb");
     if (!auth_file)
-	return NULL;
+        return NULL;
     for (;;) {
 	entry = XauReadAuth (auth_file);
 	if (!entry)
diff --git a/AuGetBest.c b/AuGetBest.c
index ae2b748..68d9aa1 100644
--- a/AuGetBest.c
+++ b/AuGetBest.c
@@ -70,8 +70,7 @@ XauGetBestAuthByAddr (
     char**		types,
     _Xconst int*	type_lengths)
 {
-    FILE    *auth_file;
-    char    *auth_name;
+    FILE    *auth_file = XauOpenAuth();
     Xauth   *entry;
     Xauth   *best;
     int	    best_type;
@@ -81,12 +80,6 @@ XauGetBestAuthByAddr (
     unsigned short	fully_qual_address_length;
 #endif
 
-    auth_name = XauFileName ();
-    if (!auth_name)
-	return NULL;
-    if (access (auth_name, R_OK) != 0)		/* checks REAL id */
-	return NULL;
-    auth_file = fopen (auth_name, "rb");
     if (!auth_file)
 	return NULL;
 
diff --git a/Makefile.am b/Makefile.am
index b597622..42f4c31 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -14,6 +14,7 @@ libXau_la_SOURCES = \
 	AuGetAddr.c \
 	AuGetBest.c \
 	AuLock.c \
+	AuOpenAuth.c \
 	AuRead.c \
 	AuUnlock.c \
 	AuWrite.c
@@ -65,13 +66,15 @@ EXTRA_DIST = xau.pc.in $(libman_PRE) ChangeLog
 
 Xau_shadows =                               \
     XauFileName                             \
+    XauFreeFileName                         \
     XauReadAuth                             \
     XauLockAuth                             \
     XauUnlockAuth                           \
     XauWriteAuth                            \
     XauDisposeAuth                          \
     XauGetAuthByAddr                        \
-    XauGetBestAuthByAddr
+    XauGetBestAuthByAddr                    \
+    XauOpenAuth
 
 shadows.DONE:
 	-rm -f $(Xau_shadows:=. at LIB_MAN_SUFFIX@)
diff --git a/Xau.man b/Xau.man
index a336523..928c7fa 100644
--- a/Xau.man
+++ b/Xau.man
@@ -30,7 +30,8 @@
 .\"
 .TH Xau 3 __xorgversion__
 .SH NAME
-Xau library: XauFileName, XauReadAuth, XauLockAuth, XauUnlockAuth,
+Xau library: XauFileName, XauFreeFileName, XauOpenAuth,
+XauReadAuth, XauLockAuth, XauUnlockAuth,
 XauWriteAuth, XauDisposeAuth,
 XauGetAuthByAddr, XauGetBestAuthByAddr \- X authority database routines
 .SH SYNOPSIS
@@ -53,6 +54,10 @@ typedef struct xauth {
 .HP
 char *XauFileName (void); 
 .HP
+void XauFreeFileName (void); 
+.HP
+FILE *XauOpenAuth (void);
+.HP
 Xauth *XauReadAuth (FILE *\fIauth_file\fP\^); 
 .HP
 int XauWriteAuth (FILE *\fIauth_file\fP, Xauth *\fIauth\fP\^); 
@@ -77,8 +82,16 @@ int XauDisposeAuth (Xauth *\fIauth\fP\^);
 .PP
 \fBXauFileName\fP generates the default authorization file name by first
 checking the XAUTHORITY environment variable if set, else it returns
-$HOME/.Xauthority.  This name is statically allocated and should
-not be freed.
+$HOME/.Xauthority.    The routine returns NULL on error.
+The returned name is a static pointer to heap-allocated memory, and
+should be freed by a call to \fBXauFreeFileName\fP when no
+longer needed.
+.PP
+\fBXauFreeFileName\fP frees the filename allocated by the
+most recent call to \fBXauFileName\fP, if any.
+.PP
+\fBXauOpenAuth\fP opens the authorization file and returns a
+pointer to it, or NULL on error.
 .PP
 \fBXauReadAuth\fP reads the next entry from \fIauth_file\fP.  The entry is
 \fBnot\fP statically allocated and should be freed by calling
diff --git a/include/X11/Xauth.h b/include/X11/Xauth.h
index 181618a..d2552fc 100644
--- a/include/X11/Xauth.h
+++ b/include/X11/Xauth.h
@@ -61,6 +61,10 @@ _XFUNCPROTOBEGIN
 
 char *XauFileName(void);
 
+void XauFreeFileName(void);
+
+FILE* XauOpenAuth(void);
+
 Xauth *XauReadAuth(
 FILE*	/* auth_file */
 );


More information about the xorg mailing list