[PATCH libxkbfile 11/12] Fix memory leaks

Pauli Nieminen ext-pauli.nieminen at nokia.com
Wed Jul 21 05:02:55 PDT 2010


XkbAtomGetString allcoates memory fro the returned string and ownership
is passed to the caller. We have to make sure the memory is freed.

Signed-off-by: Pauli Nieminen <ext-pauli.nieminen at nokia.com>
---
 src/xkmout.c |   42 ++++++++++++++++++++++++++++++++----------
 1 files changed, 32 insertions(+), 10 deletions(-)

diff --git a/src/xkmout.c b/src/xkmout.c
index f32194c..37d444a 100644
--- a/src/xkmout.c
+++ b/src/xkmout.c
@@ -118,11 +118,25 @@ xkmPutCountedString(FILE *file,char *str)
     return xkmPutCountedBytes(file,str,strlen(str));
 }
 
-#define	xkmSizeCountedAtomString(d,a)	\
-	xkmSizeCountedString(XkbAtomGetString((d),(a)))
+static int
+xkmSizeCountedAtomString(Display *d, Atom a)
+{
+	int r;
+	char *atom = XkbAtomGetString(d, a);
+	r = xkmSizeCountedString(atom);
+	_XkbFree(atom);
+	return r;
+}
 
-#define	xkmPutCountedAtomString(d,f,a)	\
-	xkmPutCountedString((f),XkbAtomGetString((d),(a)))
+static int
+xkmPutCountedAtomString(Display *d, FILE *f, Atom a)
+{
+	int r;
+	char *atom = XkbAtomGetString(d, a);
+	r = xkmPutCountedString(f, atom);
+	_XkbFree(atom);
+	return r;
+}
 
 /***====================================================================***/
 
@@ -189,9 +203,7 @@ unsigned	size= 0;
 	size+= xkmPutPadding(file,i);
     for (i=0,bit=1;i<XkbNumVirtualMods;i++,bit<<=1) {
 	if (info->named_vmods&bit) {
-	    register char *name;
-	    name= XkbAtomGetString(dpy,xkb->names->vmods[i]);
-	    size+= xkmPutCountedString(file,name);
+	    size += xkmPutCountedAtomString(dpy, file, xkb->names->vmods[i]);
 	}
     }
     return size;
@@ -244,7 +256,7 @@ unsigned	tmp,size= 0;
     kcName= xkb->names->keycodes;
     start= xkb->names->keys[xkb->min_key_code].name;
     
-    size+= xkmPutCountedString(file,XkbAtomGetString(dpy,kcName));
+    size+= xkmPutCountedAtomString(dpy, file, kcName);
     size+= xkmPutCARD8(file,xkb->min_key_code);
     size+= xkmPutCARD8(file,xkb->max_key_code);
     size+= xkmPutCARD8(file,xkb->names->num_key_aliases);
@@ -284,6 +296,7 @@ char *			name;
     if (xkb->names)	name= XkbAtomGetString(dpy,xkb->names->types);
     else		name= NULL;
     size= xkmSizeCountedString(name);
+    _XkbFree(name);
     size+= 4;	/* room for # of key types + padding */
     for (i=0,type=xkb->map->types;i<xkb->map->num_types;i++,type++) {
 	size+= SIZEOF(xkmKeyTypeDesc);
@@ -326,6 +339,7 @@ char *			name;
     if (xkb->names)	name= XkbAtomGetString(dpy,xkb->names->types);
     else		name= NULL;
     size+= xkmPutCountedString(file,name);
+    _XkbFree(name);
     size+= xkmPutCARD16(file,xkb->map->num_types);
     size+= xkmPutPadding(file,2);
     type= xkb->map->types;
@@ -347,7 +361,7 @@ char *			name;
 	    tmp= fwrite(&wire_entry,SIZEOF(xkmKTMapEntryDesc),1,file);
 	    size+= tmp*SIZEOF(xkmKTMapEntryDesc);
 	}
-	size+= xkmPutCountedString(file,XkbAtomGetString(dpy,type->name));
+	size+= xkmPutCountedAtomString(dpy, file, type->name);
 	if (type->preserve) {
 	    xkmModsDesc	p_entry;
 	    XkbModsPtr	pre;
@@ -361,7 +375,7 @@ char *			name;
 	if (type->level_names!=NULL) {
 	    names= type->level_names;
 	    for (n=0;n<wire.nLevelNames;n++) {
-		size+= xkmPutCountedString(file,XkbAtomGetString(dpy,names[n]));
+		size+= xkmPutCountedAtomString(dpy, file, names[n]);
 	    }
 	}
     }
@@ -410,6 +424,7 @@ Display *	dpy;
     toc->size= 		size+SIZEOF(xkmSectionInfo);
     toc->offset= 	(*offset_inout);
     (*offset_inout)+= 	toc->size;
+    _XkbFree(name);
     return 1;
 }
 
@@ -462,6 +477,7 @@ unsigned		tmp,size=0;
 	    }
 	}
     }
+    _XkbFree(name);
     return size;
 }
 
@@ -489,6 +505,7 @@ char *		name;
 	 name= XkbAtomGetString(dpy,xkb->names->symbols);
     else name= NULL;
     size= xkmSizeCountedString(name);
+    _XkbFree(name);
     size+= 4;	/* min and max keycode, group names mask */
     for (i=0;i<XkbNumKbdGroups;i++) {
 	if (xkb->names && xkb->names->groups[i]!=None)
@@ -509,6 +526,7 @@ char *		name;
 			name= XkbAtomGetString(dpy,type->name);
 			if (name!=NULL)
 			    size+= xkmSizeCountedString(name);
+			_XkbFree(name);
 		    }
 		}
 	    }
@@ -549,6 +567,7 @@ unsigned		tmp,size= 0;
 	 name= XkbAtomGetString(dpy,xkb->names->symbols);
     else name= NULL;
     size+= xkmPutCountedString(file,name);
+    _XkbFree(name);
     for (tmp=i=0;i<XkbNumKbdGroups;i++) {
 	if (xkb->names && xkb->names->groups[i]!=None)
 	    tmp|= (1<<i);
@@ -603,6 +622,7 @@ unsigned		tmp,size= 0;
 	    for (g=0;g<XkbNumKbdGroups;g++) {
 		if (typeName[g]!=NULL)
 		    size+= xkmPutCountedString(file,typeName[g]);
+		_XkbFree(typeName[g]);
 	    }
 	}
 	if (XkbNumGroups(wireMap.num_groups)>0) {
@@ -677,6 +697,7 @@ register unsigned	i,nLEDs;
 		size+= xkmSizeCountedString(name);
 		size+= SIZEOF(xkmIndicatorMapDesc);
 		nLEDs++;
+		_XkbFree(name);
 	    }
 	}
     }
@@ -730,6 +751,7 @@ unsigned		tmp,size= 0;
 		wire.ctrls= map->ctrls;
 		tmp= fwrite(&wire,SIZEOF(xkmIndicatorMapDesc),1,file);
 		size+= tmp*SIZEOF(xkmIndicatorMapDesc);
+		_XkbFree(name);
 	    }
 	}
     }
-- 
1.6.3.3



More information about the xorg-devel mailing list