From 30d65cd9f11256ff4dd05ec629dcbcf96b034304 Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Thu, 10 Apr 2025 19:57:07 +0200 Subject: [PATCH] kdrive: 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/kdrive/ephyr/ephyr.c | 4 +--- hw/kdrive/ephyr/ephyr_glamor.c | 5 ++--- hw/kdrive/ephyr/hostx.c | 3 +-- hw/kdrive/src/kxv.c | 4 ++-- 4 files changed, 6 insertions(+), 10 deletions(-) diff --git a/hw/kdrive/ephyr/ephyr.c b/hw/kdrive/ephyr/ephyr.c index 4e0086d042..311489a0b3 100644 --- a/hw/kdrive/ephyr/ephyr.c +++ b/hw/kdrive/ephyr/ephyr.c @@ -76,9 +76,7 @@ ephyrInitialize(KdCardInfo * card, EphyrPriv * priv) Bool ephyrCardInit(KdCardInfo * card) { - EphyrPriv *priv; - - priv = (EphyrPriv *) malloc(sizeof(EphyrPriv)); + EphyrPriv *priv = calloc(1, sizeof(EphyrPriv)); if (!priv) return FALSE; diff --git a/hw/kdrive/ephyr/ephyr_glamor.c b/hw/kdrive/ephyr/ephyr_glamor.c index 772b730525..5573327f0f 100644 --- a/hw/kdrive/ephyr/ephyr_glamor.c +++ b/hw/kdrive/ephyr/ephyr_glamor.c @@ -144,11 +144,10 @@ ephyr_glamor_build_glsl_prog(GLuint vs, GLuint fs) glLinkProgram(prog); glGetProgramiv(prog, GL_LINK_STATUS, &ok); if (!ok) { - GLchar *info; GLint size; glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &size); - info = malloc(size); + GLchar *info = calloc(1, size); glGetProgramInfoLog(prog, size, NULL, info); ErrorF("Failed to link: %s\n", info); @@ -352,7 +351,7 @@ ephyr_glamor_screen_init(xcb_window_t win, xcb_visualid_t vid) glamor = calloc(1, sizeof(struct ephyr_glamor)); if (!glamor) { - FatalError("malloc"); + FatalError("calloc"); return NULL; } diff --git a/hw/kdrive/ephyr/hostx.c b/hw/kdrive/ephyr/hostx.c index f1d6a0a81d..7fcca5ce0d 100644 --- a/hw/kdrive/ephyr/hostx.c +++ b/hw/kdrive/ephyr/hostx.c @@ -533,7 +533,6 @@ hostx_init(void) uint32_t pixel; int index; char *tmpstr; - char *class_hint; size_t class_len; xcb_screen_t *xscreen; xcb_rectangle_t rect = { 0, 0, 1, 1 }; @@ -658,7 +657,7 @@ hostx_init(void) if (tmpstr && (!ephyrResNameFromCmd)) ephyrResName = tmpstr; class_len = strlen(ephyrResName) + 1 + strlen("Xephyr") + 1; - class_hint = malloc(class_len); + char *class_hint = calloc(1, class_len); if (class_hint) { strcpy(class_hint, ephyrResName); strcpy(class_hint + strlen(ephyrResName) + 1, "Xephyr"); diff --git a/hw/kdrive/src/kxv.c b/hw/kdrive/src/kxv.c index 1e0366be2e..2dea819a51 100644 --- a/hw/kdrive/src/kxv.c +++ b/hw/kdrive/src/kxv.c @@ -135,7 +135,7 @@ KdXVScreenInit(ScreenPtr pScreen, KdVideoAdaptorPtr adaptors, int num) KdXvScreenKey = XvGetScreenKey(); PortResource = XvGetRTPort(); - ScreenPriv = malloc(sizeof(KdXVScreenRec)); + ScreenPriv = calloc(1, sizeof(KdXVScreenRec)); dixSetPrivate(&pScreen->devPrivates, &KdXVScreenPrivateKey, ScreenPriv); if (!ScreenPriv) @@ -723,7 +723,7 @@ KdXVEnlistPortInWindow(WindowPtr pWin, XvPortRecPrivatePtr portPriv) } if (!winPriv) { - winPriv = malloc(sizeof(KdXVWindowRec)); + winPriv = calloc(1, sizeof(KdXVWindowRec)); if (!winPriv) return BadAlloc; winPriv->PortRec = portPriv;