From 8f26a2f6a76cc3c0059d7727686f010fdbedc651 Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Thu, 10 Apr 2025 20:05:27 +0200 Subject: [PATCH] xnest: 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 --- hw/xnest/Font.c | 3 +-- hw/xnest/Screen.c | 7 +++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/hw/xnest/Font.c b/hw/xnest/Font.c index eff88f5d9..602a8aa03 100644 --- a/hw/xnest/Font.c +++ b/hw/xnest/Font.c @@ -37,7 +37,6 @@ int xnestFontPrivateIndex; Bool xnestRealizeFont(ScreenPtr pScreen, FontPtr pFont) { - void *priv; Atom name_atom, value_atom; int nprops; FontPropPtr props; @@ -66,7 +65,7 @@ xnestRealizeFont(ScreenPtr pScreen, FontPtr pFont) if (!name) return FALSE; - priv = (void *) malloc(sizeof(xnestPrivFont)); + void *priv = calloc(1, sizeof(xnestPrivFont)); xfont2_font_set_private(pFont, xnestFontPrivateIndex, priv); xnestFontPriv(pFont)->font_struct = XLoadQueryFont(xnestDisplay, name); diff --git a/hw/xnest/Screen.c b/hw/xnest/Screen.c index bab6096b1..ca0997042 100644 --- a/hw/xnest/Screen.c +++ b/hw/xnest/Screen.c @@ -167,10 +167,10 @@ xnestOpenScreen(ScreenPtr pScreen, int argc, char *argv[]) visuals = xallocarray(xnestNumVisuals, sizeof(VisualRec)); numVisuals = 0; - depths = (DepthPtr) malloc(MAXDEPTH * sizeof(DepthRec)); + depths = calloc(MAXDEPTH, sizeof(DepthRec)); depths[0].depth = 1; depths[0].numVids = 0; - depths[0].vids = (VisualID *) malloc(MAXVISUALSPERDEPTH * sizeof(VisualID)); + depths[0].vids = calloc(MAXVISUALSPERDEPTH, sizeof(VisualID)); numDepths = 1; for (i = 0; i < xnestNumVisuals; i++) { @@ -217,8 +217,7 @@ xnestOpenScreen(ScreenPtr pScreen, int argc, char *argv[]) depthIndex = numDepths; depths[depthIndex].depth = xnestVisuals[i].depth; depths[depthIndex].numVids = 0; - depths[depthIndex].vids = - (VisualID *) malloc(MAXVISUALSPERDEPTH * sizeof(VisualID)); + depths[depthIndex].vids = calloc(MAXVISUALSPERDEPTH, sizeof(VisualID)); numDepths++; } if (depths[depthIndex].numVids >= MAXVISUALSPERDEPTH) {