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 <info@metux.net>
This commit is contained in:
Enrico Weigelt, metux IT consult 2025-04-10 19:18:24 +02:00
parent 085919667b
commit 55544ff85f
6 changed files with 11 additions and 19 deletions

View File

@ -2335,12 +2335,10 @@ DeliverGestureEventToOwner(DeviceIntPtr dev, GestureInfoPtr gi, InternalEvent *e
int int
InitProximityClassDeviceStruct(DeviceIntPtr dev) InitProximityClassDeviceStruct(DeviceIntPtr dev)
{ {
ProximityClassPtr proxc;
BUG_RETURN_VAL(dev == NULL, FALSE); BUG_RETURN_VAL(dev == NULL, FALSE);
BUG_RETURN_VAL(dev->proximity != NULL, FALSE); BUG_RETURN_VAL(dev->proximity != NULL, FALSE);
proxc = (ProximityClassPtr) malloc(sizeof(ProximityClassRec)); ProximityClassPtr proxc = calloc(1, sizeof(ProximityClassRec));
if (!proxc) if (!proxc)
return FALSE; return FALSE;
proxc->sourceid = dev->id; proxc->sourceid = dev->id;

View File

@ -168,7 +168,7 @@ int
ProcXGetDeviceControl(ClientPtr client) ProcXGetDeviceControl(ClientPtr client)
{ {
int rc, total_length = 0; int rc, total_length = 0;
char *buf, *savbuf; char *savbuf;
DeviceIntPtr dev; DeviceIntPtr dev;
xGetDeviceControlReply rep; xGetDeviceControlReply rep;
@ -206,7 +206,7 @@ ProcXGetDeviceControl(ClientPtr client)
return BadValue; return BadValue;
} }
buf = (char *) malloc(total_length); char *buf = calloc(1, total_length);
if (!buf) if (!buf)
return BadAlloc; return BadAlloc;
savbuf = buf; savbuf = buf;

View File

@ -261,7 +261,7 @@ int
ProcXGetFeedbackControl(ClientPtr client) ProcXGetFeedbackControl(ClientPtr client)
{ {
int rc, total_length = 0; int rc, total_length = 0;
char *buf, *savbuf; char *savbuf;
DeviceIntPtr dev; DeviceIntPtr dev;
KbdFeedbackPtr k; KbdFeedbackPtr k;
PtrFeedbackPtr p; PtrFeedbackPtr p;
@ -315,7 +315,7 @@ ProcXGetFeedbackControl(ClientPtr client)
if (total_length == 0) if (total_length == 0)
return BadMatch; return BadMatch;
buf = (char *) malloc(total_length); char *buf = (char *) calloc(1, total_length);
if (!buf) if (!buf)
return BadAlloc; return BadAlloc;
savbuf = buf; savbuf = buf;

View File

@ -131,7 +131,7 @@ ProcXGetSelectedExtensionEvents(ClientPtr client)
total_length = (rep.all_clients_count + rep.this_client_count) * total_length = (rep.all_clients_count + rep.this_client_count) *
sizeof(XEventClass); sizeof(XEventClass);
rep.length = bytes_to_int32(total_length); rep.length = bytes_to_int32(total_length);
buf = (XEventClass *) malloc(total_length); buf = calloc(1, total_length);
tclient = buf; tclient = buf;
aclient = buf + rep.this_client_count; aclient = buf + rep.this_client_count;

View File

@ -103,9 +103,7 @@ typedef struct _BarrierScreen {
static struct PointerBarrierDevice *AllocBarrierDevice(void) static struct PointerBarrierDevice *AllocBarrierDevice(void)
{ {
struct PointerBarrierDevice *pbd = NULL; struct PointerBarrierDevice *pbd = calloc(1, sizeof(struct PointerBarrierDevice));
pbd = malloc(sizeof(struct PointerBarrierDevice));
if (!pbd) if (!pbd)
return NULL; return NULL;
@ -558,15 +556,13 @@ CreatePointerBarrierClient(ClientPtr client,
ScreenPtr screen; ScreenPtr screen;
BarrierScreenPtr cs; BarrierScreenPtr cs;
int err; int err;
int size;
int i; int i;
struct PointerBarrierClient *ret;
CARD16 *in_devices; CARD16 *in_devices;
DeviceIntPtr dev; DeviceIntPtr dev;
size = sizeof(*ret) + sizeof(DeviceIntPtr) * stuff->num_devices; const int size = sizeof(struct PointerBarrierClient)
ret = malloc(size); + sizeof(DeviceIntPtr) * stuff->num_devices;
struct PointerBarrierClient *ret = calloc(1, size);
if (!ret) { if (!ret) {
return BadAlloc; return BadAlloc;
} }

View File

@ -575,9 +575,7 @@ XIUnregisterPropertyHandler(DeviceIntPtr dev, long id)
static XIPropertyPtr static XIPropertyPtr
XICreateDeviceProperty(Atom property) XICreateDeviceProperty(Atom property)
{ {
XIPropertyPtr prop; XIPropertyPtr prop = calloc(1, sizeof(XIPropertyRec));
prop = (XIPropertyPtr) malloc(sizeof(XIPropertyRec));
if (!prop) if (!prop)
return NULL; return NULL;