From 55544ff85f697c2defa63e1092a532583e823027 Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Thu, 10 Apr 2025 19:18:24 +0200 Subject: [PATCH] Xi: 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 --- Xi/exevents.c | 4 +--- Xi/getdctl.c | 4 ++-- Xi/getfctl.c | 4 ++-- Xi/getselev.c | 2 +- Xi/xibarriers.c | 12 ++++-------- Xi/xiproperty.c | 4 +--- 6 files changed, 11 insertions(+), 19 deletions(-) diff --git a/Xi/exevents.c b/Xi/exevents.c index dd0e6e1f7..a8e2336a5 100644 --- a/Xi/exevents.c +++ b/Xi/exevents.c @@ -2335,12 +2335,10 @@ DeliverGestureEventToOwner(DeviceIntPtr dev, GestureInfoPtr gi, InternalEvent *e int InitProximityClassDeviceStruct(DeviceIntPtr dev) { - ProximityClassPtr proxc; - BUG_RETURN_VAL(dev == NULL, FALSE); BUG_RETURN_VAL(dev->proximity != NULL, FALSE); - proxc = (ProximityClassPtr) malloc(sizeof(ProximityClassRec)); + ProximityClassPtr proxc = calloc(1, sizeof(ProximityClassRec)); if (!proxc) return FALSE; proxc->sourceid = dev->id; diff --git a/Xi/getdctl.c b/Xi/getdctl.c index 07b6bec7e..cd17d13fc 100644 --- a/Xi/getdctl.c +++ b/Xi/getdctl.c @@ -168,7 +168,7 @@ int ProcXGetDeviceControl(ClientPtr client) { int rc, total_length = 0; - char *buf, *savbuf; + char *savbuf; DeviceIntPtr dev; xGetDeviceControlReply rep; @@ -206,7 +206,7 @@ ProcXGetDeviceControl(ClientPtr client) return BadValue; } - buf = (char *) malloc(total_length); + char *buf = calloc(1, total_length); if (!buf) return BadAlloc; savbuf = buf; diff --git a/Xi/getfctl.c b/Xi/getfctl.c index 562cd5260..be0456c18 100644 --- a/Xi/getfctl.c +++ b/Xi/getfctl.c @@ -261,7 +261,7 @@ int ProcXGetFeedbackControl(ClientPtr client) { int rc, total_length = 0; - char *buf, *savbuf; + char *savbuf; DeviceIntPtr dev; KbdFeedbackPtr k; PtrFeedbackPtr p; @@ -315,7 +315,7 @@ ProcXGetFeedbackControl(ClientPtr client) if (total_length == 0) return BadMatch; - buf = (char *) malloc(total_length); + char *buf = (char *) calloc(1, total_length); if (!buf) return BadAlloc; savbuf = buf; diff --git a/Xi/getselev.c b/Xi/getselev.c index 0abf9faa9..f7d6670a0 100644 --- a/Xi/getselev.c +++ b/Xi/getselev.c @@ -131,7 +131,7 @@ ProcXGetSelectedExtensionEvents(ClientPtr client) total_length = (rep.all_clients_count + rep.this_client_count) * sizeof(XEventClass); rep.length = bytes_to_int32(total_length); - buf = (XEventClass *) malloc(total_length); + buf = calloc(1, total_length); tclient = buf; aclient = buf + rep.this_client_count; diff --git a/Xi/xibarriers.c b/Xi/xibarriers.c index 3a125fe76..f7ad4276a 100644 --- a/Xi/xibarriers.c +++ b/Xi/xibarriers.c @@ -103,9 +103,7 @@ typedef struct _BarrierScreen { static struct PointerBarrierDevice *AllocBarrierDevice(void) { - struct PointerBarrierDevice *pbd = NULL; - - pbd = malloc(sizeof(struct PointerBarrierDevice)); + struct PointerBarrierDevice *pbd = calloc(1, sizeof(struct PointerBarrierDevice)); if (!pbd) return NULL; @@ -558,15 +556,13 @@ CreatePointerBarrierClient(ClientPtr client, ScreenPtr screen; BarrierScreenPtr cs; int err; - int size; int i; - struct PointerBarrierClient *ret; CARD16 *in_devices; DeviceIntPtr dev; - size = sizeof(*ret) + sizeof(DeviceIntPtr) * stuff->num_devices; - ret = malloc(size); - + const int size = sizeof(struct PointerBarrierClient) + + sizeof(DeviceIntPtr) * stuff->num_devices; + struct PointerBarrierClient *ret = calloc(1, size); if (!ret) { return BadAlloc; } diff --git a/Xi/xiproperty.c b/Xi/xiproperty.c index 8db6b03d1..bd9979884 100644 --- a/Xi/xiproperty.c +++ b/Xi/xiproperty.c @@ -575,9 +575,7 @@ XIUnregisterPropertyHandler(DeviceIntPtr dev, long id) static XIPropertyPtr XICreateDeviceProperty(Atom property) { - XIPropertyPtr prop; - - prop = (XIPropertyPtr) malloc(sizeof(XIPropertyRec)); + XIPropertyPtr prop = calloc(1, sizeof(XIPropertyRec)); if (!prop) return NULL;