From d199dcbe4c6a2db3c54033332b41b5c2778d1780 Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Thu, 10 Apr 2025 18:33:17 +0200 Subject: [PATCH] xkb: use calloc() instead of malloc() Using calloc() instead of malloc() as preventive measure, so there never can be any hidden bugs or leaks due uninitialized memory. The extra cost of using this compiler intrinsic should be practically impossible to measure - in many cases a good compiler can even deduce if certain areas really don't need to be zero'd (because they're written to right after allocation) and create more efficient machine code. The code pathes in question are pretty cold anyways, so it's probably not worth even thinking about potential extra runtime costs. Signed-off-by: Enrico Weigelt, metux IT consult --- xkb/maprules.c | 2 +- xkb/xkb.c | 5 ++--- xkb/xkbInit.c | 3 +-- xkb/xkbUtils.c | 8 ++++---- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/xkb/maprules.c b/xkb/maprules.c index f23f8a166..10cd8754e 100644 --- a/xkb/maprules.c +++ b/xkb/maprules.c @@ -793,7 +793,7 @@ XkbRF_SubstituteVars(char *name, XkbRF_MultiDefsPtr mdefs) } str = index(&str[0], '%'); } - name = malloc(len + 1); + name = calloc(1, len + 1); str = orig; outstr = name; while (*str != '\0') { diff --git a/xkb/xkb.c b/xkb/xkb.c index bad6b0a2f..001e5928f 100644 --- a/xkb/xkb.c +++ b/xkb/xkb.c @@ -5146,7 +5146,7 @@ _GetCountedString(char **wire_inout, ClientPtr client, char **str) if (client->req_len < bytes_to_int32(next - (char *) client->requestBuffer)) return BadValue; - *str = malloc(len + 1); + *str = calloc(1, len + 1); if (!*str) return BadAlloc; memcpy(*str, &wire[2], len); @@ -6510,7 +6510,6 @@ ProcXkbGetDeviceInfo(ClientPtr client) unsigned length, nameLen; CARD16 ledClass, ledID; unsigned wanted; - char *str; REQUEST(xkbGetDeviceInfoReq); REQUEST_SIZE_MATCH(xkbGetDeviceInfoReq); @@ -6612,7 +6611,7 @@ ProcXkbGetDeviceInfo(ClientPtr client) } WriteToClient(client, SIZEOF(xkbGetDeviceInfoReply), &rep); - str = malloc(nameLen); + char *str = calloc(1, nameLen); if (!str) return BadAlloc; XkbWriteCountedString(str, dev->name, client->swapped); diff --git a/xkb/xkbInit.c b/xkb/xkbInit.c index d3259225f..5a6bde92f 100644 --- a/xkb/xkbInit.c +++ b/xkb/xkbInit.c @@ -140,7 +140,6 @@ XkbWriteRulesProp(void) { int len, out; Atom name; - char *pval; len = (XkbRulesUsed ? strlen(XkbRulesUsed) : 0); len += (XkbModelUsed ? strlen(XkbModelUsed) : 0); @@ -158,7 +157,7 @@ XkbWriteRulesProp(void) ErrorF("[xkb] Atom error: %s not created\n", _XKB_RF_NAMES_PROP_ATOM); return TRUE; } - pval = (char *) malloc(len); + char *pval = calloc(1, len); if (!pval) { ErrorF("[xkb] Allocation error: %s proprerty not created\n", _XKB_RF_NAMES_PROP_ATOM); diff --git a/xkb/xkbUtils.c b/xkb/xkbUtils.c index 136688850..4b2cba12b 100644 --- a/xkb/xkbUtils.c +++ b/xkb/xkbUtils.c @@ -1032,7 +1032,7 @@ _XkbCopyClientMap(XkbDescPtr src, XkbDescPtr dst) } else if (!dtype->num_levels || !dtype->level_names || i >= dst->map->num_types) { - tmp = malloc(stype->num_levels * sizeof(Atom)); + tmp = calloc(stype->num_levels, sizeof(Atom)); if (!tmp) continue; dtype->level_names = tmp; @@ -1880,7 +1880,7 @@ _XkbCopyGeom(XkbDescPtr src, XkbDescPtr dst) /* font */ if (src->geom->label_font) { if (!dst->geom->label_font) { - tmp = malloc(strlen(src->geom->label_font) + 1); + tmp = calloc(1, strlen(src->geom->label_font) + 1); if (!tmp) return FALSE; dst->geom->label_font = tmp; @@ -1928,7 +1928,7 @@ _XkbCopyIndicators(XkbDescPtr src, XkbDescPtr dst) /* indicators */ if (src->indicators) { if (!dst->indicators) { - dst->indicators = malloc(sizeof(XkbIndicatorRec)); + dst->indicators = calloc(1, sizeof(XkbIndicatorRec)); if (!dst->indicators) return FALSE; } @@ -1947,7 +1947,7 @@ _XkbCopyControls(XkbDescPtr src, XkbDescPtr dst) /* controls */ if (src->ctrls) { if (!dst->ctrls) { - dst->ctrls = malloc(sizeof(XkbControlsRec)); + dst->ctrls = calloc(1, sizeof(XkbControlsRec)); if (!dst->ctrls) return FALSE; }