From 8b65f2edb407521e0ac0e207e5d5358986bd46da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rami=20Ylim=C3=A4ki?= Date: Tue, 22 Jun 2010 14:57:20 +0300 Subject: [PATCH] record: Prevent a crash on recording client disconnect. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Execute the following steps to reproduce the issue. 1. Run at least two recording clients simultaneously. $ cnee --record --request-range 1-127 & $ cnee --record --request-range 1-127 & 2. Kill the recording clients. $ killall cnee 3. Give X server something to do so that the clients are closed. $ xinput list $ xinput list As a result RecordUninstallHooks accesses NullClient, because RecordAClientStateChange doesn't clean the recording clients up properly. Fix RecordUninstallHooks to fail locally on an assertion instead of much later in privates code, if NullClient is still accessed because of some other bug. Fix RecordAClientStateChange to iterate through all contexts so that modifications of the iterated array during iteration don't cause contexts to be skipped. Signed-off-by: Rami Ylimäki Reviewed-by: Keith Packard Signed-off-by: Keith Packard --- record/record.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/record/record.c b/record/record.c index 06006f76d..6a93d7a5c 100644 --- a/record/record.c +++ b/record/record.c @@ -997,10 +997,11 @@ RecordUninstallHooks(RecordClientsAndProtocolPtr pRCAP, XID oneclient) ClientPtr pClient = clients[CLIENT_ID(client)]; int c; Bool otherRCAPwantsProcVector = FALSE; - RecordClientPrivatePtr pClientPriv = - RecordClientPrivate(pClient); + RecordClientPrivatePtr pClientPriv = NULL; - assert (pClient && RecordClientPrivate(pClient)); + assert (pClient); + pClientPriv = RecordClientPrivate(pClient); + assert (pClientPriv); memcpy(pClientPriv->recordVector, pClientPriv->originalVector, sizeof (pClientPriv->recordVector)); @@ -2813,6 +2814,8 @@ RecordAClientStateChange(CallbackListPtr *pcbl, pointer nulldata, pointer callda NewClientInfoRec *pci = (NewClientInfoRec *)calldata; int i; ClientPtr pClient = pci->client; + RecordContextPtr *ppAllContextsCopy = NULL; + int numContextsCopy = 0; switch (pClient->clientState) { @@ -2834,10 +2837,17 @@ RecordAClientStateChange(CallbackListPtr *pcbl, pointer nulldata, pointer callda case ClientStateGone: case ClientStateRetained: /* client disconnected */ - for (i = 0; i < numContexts; i++) + + /* RecordDisableContext modifies contents of ppAllContexts. */ + numContextsCopy = numContexts; + ppAllContextsCopy = malloc(numContextsCopy * sizeof(RecordContextPtr)); + assert(ppAllContextsCopy); + memcpy(ppAllContextsCopy, ppAllContexts, numContextsCopy * sizeof(RecordContextPtr)); + + for (i = 0; i < numContextsCopy; i++) { RecordClientsAndProtocolPtr pRCAP; - RecordContextPtr pContext = ppAllContexts[i]; + RecordContextPtr pContext = ppAllContextsCopy[i]; int pos; if (pContext->pRecordingClient == pClient) @@ -2851,6 +2861,8 @@ RecordAClientStateChange(CallbackListPtr *pcbl, pointer nulldata, pointer callda RecordDeleteClientFromRCAP(pRCAP, pos); } } + + free(ppAllContextsCopy); break; default: