[PATCH 3/5] libXi: Use single error path in XQueryDeviceState

Pauli Nieminen ext-pauli.nieminen at nokia.com
Mon Jun 14 01:47:33 PDT 2010


This reduces code duplication and fixes possible leak of data. data would
leak if allocation of state fails.

Signed-off-by: Pauli Nieminen <ext-pauli.nieminen at nokia.com>
---
 src/XQueryDv.c |   24 +++++++++---------------
 1 files changed, 9 insertions(+), 15 deletions(-)

diff --git a/src/XQueryDv.c b/src/XQueryDv.c
index d9495c2..637d5cf 100644
--- a/src/XQueryDv.c
+++ b/src/XQueryDv.c
@@ -69,7 +69,7 @@ XQueryDeviceState(
     xQueryDeviceStateReply rep;
     XDeviceState *state = NULL;
     XInputClass *any, *Any;
-    char *data;
+    char *data = NULL;
     XExtDisplayInfo *info = XInput_find_display(dpy);
 
     LockDisplay(dpy);
@@ -81,20 +81,15 @@ XQueryDeviceState(
     req->ReqType = X_QueryDeviceState;
     req->deviceid = dev->device_id;
 
-    if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
-	UnlockDisplay(dpy);
-	SyncHandle();
-	return (XDeviceState *) NULL;
-    }
+    if (!_XReply(dpy, (xReply *) & rep, 0, xFalse))
+        goto out;
 
     rlen = rep.length << 2;
     if (rlen > 0) {
 	data = Xmalloc(rlen);
 	if (!data) {
 	    _XEatData(dpy, (unsigned long)rlen);
-	    UnlockDisplay(dpy);
-	    SyncHandle();
-	    return ((XDeviceState *) NULL);
+	    goto out;
 	}
 	_XRead(dpy, data, rlen);
 
@@ -117,11 +112,9 @@ XQueryDeviceState(
 	    any = (XInputClass *) ((char *)any + any->length);
 	}
 	state = (XDeviceState *) Xmalloc(size + sizeof(XDeviceState));
-	if (!state) {
-	    UnlockDisplay(dpy);
-	    SyncHandle();
-	    return ((XDeviceState *) NULL);
-	}
+	if (!state)
+            goto out;
+
 	state->device_id = dev->device_id;
 	state->num_classes = rep.num_classes;
 	state->data = (XInputClass *) (state + 1);
@@ -175,8 +168,9 @@ XQueryDeviceState(
 	    }
 	    any = (XInputClass *) ((char *)any + any->length);
 	}
-	Xfree(data);
     }
+out:
+    Xfree(data);
 
     UnlockDisplay(dpy);
     SyncHandle();
-- 
1.6.3.3



More information about the xorg-devel mailing list