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 <info@metux.net>
This commit is contained in:
Enrico Weigelt, metux IT consult 2025-04-10 18:33:17 +02:00
parent 4de5adef96
commit d199dcbe4c
4 changed files with 8 additions and 10 deletions

View File

@ -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') {

View File

@ -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);

View File

@ -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);

View File

@ -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;
}