From dcefd2156fabac3256228eafbf79b4e499bb852b Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Fri, 26 Jul 2024 14:30:01 +0200 Subject: [PATCH] xkb: split XkbSendIndicatorMap() This function is a funny beast: it assembles and writes out an xkbGetIndicatorMapReply, called in two different cases, ProcXkbGetIndicatorMap() as well as ProcXkbGetKbdByName(). In the latter case the whole reply is contained in another one. That's the reason why it's payload size is computed separately - the caller must know that in order to set up the container's reply size correctly. As preparation for upcoming simplifications of the reply send path, splitting off this function into pieces: XkbAssembleIndicatorMap() just assembles the reply payload, while it's callers now responsible for preparing the request header and writing out both pieces. Signed-off-by: Enrico Weigelt, metux IT consult --- xkb/xkb.c | 143 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 90 insertions(+), 53 deletions(-) diff --git a/xkb/xkb.c b/xkb/xkb.c index 8b7efface..dcf259063 100644 --- a/xkb/xkb.c +++ b/xkb/xkb.c @@ -3169,16 +3169,13 @@ XkbComputeGetIndicatorMapReplySize(XkbIndicatorPtr indicators, return Success; } -static int -XkbSendIndicatorMap(ClientPtr client, - XkbIndicatorPtr indicators, xkbGetIndicatorMapReply rep) +static void +XkbAssembleIndicatorMap(ClientPtr client, + XkbIndicatorPtr indicators, + xkbGetIndicatorMapReply rep, + char *buf) { - int length = rep.length * 4; - - CARD8 *map = calloc(1, length); - if (!map) - return BadAlloc; - + CARD8 *map = (CARD8*)buf; register int i; register unsigned bit; @@ -3203,17 +3200,6 @@ XkbSendIndicatorMap(ClientPtr client, } } } - - if (client->swapped) { - swaps(&rep.sequenceNumber); - swapl(&rep.length); - swapl(&rep.which); - swapl(&rep.realIndicators); - } - WriteToClient(client, sizeof(xkbGetIndicatorMapReply), &rep); - WriteToClient(client, length, map); - free(map); - return Success; } int @@ -3241,7 +3227,25 @@ ProcXkbGetIndicatorMap(ClientPtr client) .which = stuff->which }; XkbComputeGetIndicatorMapReplySize(leds, &rep); - return XkbSendIndicatorMap(client, leds, rep); + + int sz = rep.length * sizeof(CARD32); + char *buf = calloc(1, sz); + if (!buf) + return BadAlloc; + + XkbAssembleIndicatorMap(client, leds, rep, buf); + + if (client->swapped) { + swaps(&rep.sequenceNumber); + swapl(&rep.length); + swapl(&rep.which); + swapl(&rep.realIndicators); + } + + WriteToClient(client, sizeof(xkbGetIndicatorMapReply), &rep); + WriteToClient(client, sz, buf); + free(buf); + return Success; } /** @@ -3788,26 +3792,14 @@ XkbComputeGetNamesReplySize(XkbDescPtr xkb, xkbGetNamesReply * rep) desc += sizeof(CARD32); \ } while (0) -static int -XkbSendNames(ClientPtr client, XkbDescPtr xkb, xkbGetNamesReply rep) +static void +XkbAssembleNames(ClientPtr client, XkbDescPtr xkb, xkbGetNamesReply rep, char *buf) { - register unsigned i, length, which; - char *start; - char *desc; + register unsigned i, which; + char *desc = buf; - length = rep.length * 4; which = rep.which; - if (client->swapped) { - swaps(&rep.sequenceNumber); - swapl(&rep.length); - swapl(&rep.which); - swaps(&rep.virtualMods); - swapl(&rep.indicators); - } - start = desc = calloc(1, length); - if (!start) - return BadAlloc; if (xkb->names) { if (which & XkbKeycodesNameMask) { _ADD_CARD32(xkb->names->keycodes); @@ -3882,15 +3874,6 @@ XkbSendNames(ClientPtr client, XkbDescPtr xkb, xkbGetNamesReply rep) } } } - - if ((desc - start) != (length)) { - ErrorF("[xkb] BOGUS LENGTH in write names, expected %d, got %ld\n", - length, (unsigned long) (desc - start)); - } - WriteToClient(client, sizeof(rep), &rep); - WriteToClient(client, length, start); - free((char *) start); - return Success; } #undef _ADD_CARD32 @@ -3923,7 +3906,26 @@ ProcXkbGetNames(ClientPtr client) .nRadioGroups = xkb->names ? xkb->names->num_rg : 0 }; XkbComputeGetNamesReplySize(xkb, &rep); - return XkbSendNames(client, xkb, rep); + + int sz = rep.length * sizeof(CARD32); + char *payload_buf = calloc(1, sz); + if (!payload_buf) + return BadAlloc; + + XkbAssembleNames(client, xkb, rep, payload_buf); + + if (client->swapped) { + swaps(&rep.sequenceNumber); + swapl(&rep.length); + swapl(&rep.which); + swaps(&rep.virtualMods); + swapl(&rep.indicators); + } + + WriteToClient(client, sizeof(rep), &rep); + WriteToClient(client, sz, payload_buf); + free(payload_buf); + return Success; } /***====================================================================***/ @@ -6048,7 +6050,7 @@ ProcXkbGetKbdByName(ClientPtr client) WriteToClient(client, SIZEOF(xkbGetKbdByNameReply), &rep); if (reported & (XkbGBN_SymbolsMask | XkbGBN_TypesMask)) { - int sz = ((mrep.length * sizeof(CARD32)) - (sizeof(mrep) - sizeof(xGenericReply))); + int sz = (mrep.length * sizeof(CARD32)) - (sizeof(mrep) - sizeof(xGenericReply)); char *buf = calloc(1, sz); if (!buf) return BadAlloc; @@ -6063,7 +6065,7 @@ ProcXkbGetKbdByName(ClientPtr client) swaps(&mrep.totalActs); } WriteToClient(client, sizeof(mrep), &mrep); - WriteToClient(client, sizeof(buf), buf); + WriteToClient(client, sz, buf); free(buf); } @@ -6088,10 +6090,45 @@ ProcXkbGetKbdByName(ClientPtr client) free(buf); } - if (reported & XkbGBN_IndicatorMapMask) - XkbSendIndicatorMap(client, new->indicators, irep); - if (reported & (XkbGBN_KeyNamesMask | XkbGBN_OtherNamesMask)) - XkbSendNames(client, new, nrep); + if (reported & XkbGBN_IndicatorMapMask) { + int sz = irep.length * sizeof(CARD32); + char *buf = calloc(1, sz); + if (!buf) + return BadAlloc; + + XkbAssembleIndicatorMap(client, new->indicators, irep, buf); + + if (client->swapped) { + swaps(&irep.sequenceNumber); + swapl(&irep.length); + swapl(&irep.which); + swapl(&irep.realIndicators); + } + + WriteToClient(client, sizeof(irep), &irep); + WriteToClient(client, sz, buf); + free(buf); + } + + if (reported & (XkbGBN_KeyNamesMask | XkbGBN_OtherNamesMask)) { + int sz = nrep.length * sizeof(CARD32); + char *buf = calloc(1, sz); + + XkbAssembleNames(client, new, nrep, buf); + + if (client->swapped) { + swaps(&nrep.sequenceNumber); + swapl(&nrep.length); + swapl(&nrep.which); + swaps(&nrep.virtualMods); + swapl(&nrep.indicators); + } + + WriteToClient(client, sizeof(nrep), &nrep); + WriteToClient(client, sz, buf); + free(buf); + } + if (reported & XkbGBN_GeometryMask) XkbSendGeometry(client, new->geom, grep);