]> diplodocus.org Git - xorg-xinput/commitdiff
Free strings allocated by GetAtomName instead of letting them leak
authorAlan Coopersmith <alan.coopersmith@oracle.com>
Fri, 1 Jun 2012 05:35:35 +0000 (22:35 -0700)
committerPeter Hutterer <peter.hutterer@who-t.net>
Fri, 1 Jun 2012 06:03:59 +0000 (16:03 +1000)
Fixes errors reported by Parfait 0.5.0.1 bug checking tool:

Error: Memory leak (CWE 401)
   Memory leak of pointer '<unknown>' allocated with XGetAtomName(dpy, info->type)
        at line 122 of src/list.c in function 'print_info'.
          pointer allocated at line 84 with XGetAtomName(dpy, info->type).
          <unknown> leaks when i >= info->num_classes at line 88.
Error: Memory leak (CWE 401)
   Memory leak of pointer '<unknown>' allocated with XGetAtomName(dpy, a)
        at line 160 of src/property.c in function 'print_property'.
          pointer allocated at line 131 with XGetAtomName(dpy, a).
   Memory leak of pointer '<unknown>' allocated with XGetAtomName(dpy, act_type)
        at line 160 of src/property.c in function 'print_property'.
          pointer allocated at line 143 with XGetAtomName(dpy, act_type).
   Memory leak of pointer 'name' allocated with XGetAtomName(dpy, property)
        at line 160 of src/property.c in function 'print_property'.
          'name' allocated at line 61 with XGetAtomName(dpy, property).
Error: Memory leak (CWE 401)
   Memory leak of pointer '<unknown>' allocated with XGetAtomName(dpy, a)
        at line 521 of src/property.c in function 'print_property_xi2'.
          pointer allocated at line 491 with XGetAtomName(dpy, a).
   Memory leak of pointer '<unknown>' allocated with XGetAtomName(dpy, act_type)
        at line 521 of src/property.c in function 'print_property_xi2'.
          pointer allocated at line 504 with XGetAtomName(dpy, act_type).
   Memory leak of pointer 'name' allocated with XGetAtomName(dpy, property)
        at line 521 of src/property.c in function 'print_property_xi2'.
          'name' allocated at line 428 with XGetAtomName(dpy, property).

Confirmed with Solaris Studio runtime checker that "list-props" now has
fewer leaks than before and "watch-props" no longer leaks a string every
time a property changes.

Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
src/list.c
src/property.c

index cc39bd3a9877871d42c88687047fd3d9dd53c2ab..3fa123bdb37a8646e2990d647e5a70274ec28a7a 100644 (file)
@@ -80,8 +80,11 @@ print_info(Display* dpy, XDeviceInfo *info, enum print_format format)
     if (format == FORMAT_SHORT)
         return;
 
     if (format == FORMAT_SHORT)
         return;
 
-    if(info->type != None)
-       printf("\tType is %s\n", XGetAtomName(dpy, info->type));
+    if (info->type != None) {
+       char *type = XGetAtomName(dpy, info->type);
+       printf("\tType is %s\n", type);
+       XFree(type);
+    }
 
     if (info->num_classes > 0) {
        any = (XAnyClassPtr) (info->inputclassinfo);
 
     if (info->num_classes > 0) {
        any = (XAnyClassPtr) (info->inputclassinfo);
index 14e43085dce29e1dc23f90079e9df7f03ce9162d..66a384285b540eff467d3dd7173d5f031aaa845c 100644 (file)
@@ -60,6 +60,7 @@ print_property(Display *dpy, XDevice* dev, Atom property)
 
     name = XGetAtomName(dpy, property);
     printf("\t%s (%ld):\t", name, property);
 
     name = XGetAtomName(dpy, property);
     printf("\t%s (%ld):\t", name, property);
+    XFree(name);
 
     if (XGetDeviceProperty(dpy, dev, property, 0, 1000, False,
                            AnyPropertyType, &act_type, &act_format,
 
     if (XGetDeviceProperty(dpy, dev, property, 0, 1000, False,
                            AnyPropertyType, &act_type, &act_format,
@@ -127,9 +128,9 @@ print_property(Display *dpy, XDevice* dev, Atom property)
                 case XA_ATOM:
                     {
                         Atom a = *(Atom*)ptr;
                 case XA_ATOM:
                     {
                         Atom a = *(Atom*)ptr;
-                        printf("\"%s\" (%d)",
-                                (a) ? XGetAtomName(dpy, a) : "None",
-                                (int)a);
+                        name = (a) ? XGetAtomName(dpy, a) : NULL;
+                        printf("\"%s\" (%d)", name ? name : "None", (int)a);
+                        XFree(name);
                         break;
                     }
                 default:
                         break;
                     }
                 default:
@@ -139,8 +140,9 @@ print_property(Display *dpy, XDevice* dev, Atom property)
                         break;
                     }
 
                         break;
                     }
 
-                    printf("\t... of unknown type '%s'\n",
-                            XGetAtomName(dpy, act_type));
+                    name = XGetAtomName(dpy, act_type);
+                    printf("\t... of unknown type '%s'\n", name);
+                    XFree(name);
                     done = True;
                     break;
             }
                     done = True;
                     break;
             }
@@ -250,6 +252,7 @@ int watch_props(Display *dpy, int argc, char** argv, char* n, char *desc)
 
         name = XGetAtomName(dpy, dpev->atom);
         printf("Property '%s' changed.\n", name);
 
         name = XGetAtomName(dpy, dpev->atom);
         printf("Property '%s' changed.\n", name);
+        XFree(name);
         print_property(dpy, dev, dpev->atom);
     }
 
         print_property(dpy, dev, dpev->atom);
     }
 
@@ -427,6 +430,7 @@ print_property_xi2(Display *dpy, int deviceid, Atom property)
 
     name = XGetAtomName(dpy, property);
     printf("\t%s (%ld):\t", name, property);
 
     name = XGetAtomName(dpy, property);
     printf("\t%s (%ld):\t", name, property);
+    XFree(name);
 
     if (XIGetProperty(dpy, deviceid, property, 0, 1000, False,
                            AnyPropertyType, &act_type, &act_format,
 
     if (XIGetProperty(dpy, deviceid, property, 0, 1000, False,
                            AnyPropertyType, &act_type, &act_format,
@@ -487,9 +491,9 @@ print_property_xi2(Display *dpy, int deviceid, Atom property)
                 case XA_ATOM:
                     {
                         Atom a = *(uint32_t*)ptr;
                 case XA_ATOM:
                     {
                         Atom a = *(uint32_t*)ptr;
-                        printf("\"%s\" (%ld)",
-                                (a) ? XGetAtomName(dpy, a) : "None",
-                                a);
+                        name = (a) ? XGetAtomName(dpy, a) : NULL;
+                        printf("\"%s\" (%ld)", name ? name : "None", a);
+                        XFree(name);
                         break;
                     }
                     break;
                         break;
                     }
                     break;
@@ -500,8 +504,9 @@ print_property_xi2(Display *dpy, int deviceid, Atom property)
                         break;
                     }
 
                         break;
                     }
 
-                    printf("\t... of unknown type %s\n",
-                            XGetAtomName(dpy, act_type));
+                    name = XGetAtomName(dpy, act_type);
+                    printf("\t... of unknown type %s\n", name);
+                    XFree(name);
                     done = True;
                     break;
             }
                     done = True;
                     break;
             }