From 55032e9d19c9c0ed41795f460805cecd064c1f29 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 | 6 ++---- hw/kdrive/src/kxv.c | 4 ++-- 4 files changed, 7 insertions(+), 12 deletions(-) diff --git a/hw/kdrive/ephyr/ephyr.c b/hw/kdrive/ephyr/ephyr.c index 069270219..eb3817bc4 100644 --- a/hw/kdrive/ephyr/ephyr.c +++ b/hw/kdrive/ephyr/ephyr.c @@ -75,9 +75,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 772b73052..5573327f0 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 33dcccf8f..9c4fc6cae 100644 --- a/hw/kdrive/ephyr/hostx.c +++ b/hw/kdrive/ephyr/hostx.c @@ -241,7 +241,6 @@ hostx_get_output_geometry(const char *output, int *width, int *height) { int i, name_len = 0, output_found = FALSE; - char *name = NULL; xcb_generic_error_t *error; xcb_randr_query_version_cookie_t version_c; xcb_randr_query_version_reply_t *version_r; @@ -300,7 +299,7 @@ hostx_get_output_geometry(const char *output, /* Get output name */ name_len = xcb_randr_get_output_info_name_length(output_info_r); - name = malloc(name_len + 1); + char *name = calloc(1, name_len + 1); strncpy(name, (char*)xcb_randr_get_output_info_name(output_info_r), name_len); name[name_len] = '\0'; @@ -524,7 +523,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 }; @@ -649,7 +647,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 3077009a6..8e650fcb1 100644 --- a/hw/kdrive/src/kxv.c +++ b/hw/kdrive/src/kxv.c @@ -133,7 +133,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) @@ -721,7 +721,7 @@ KdXVEnlistPortInWindow(WindowPtr pWin, XvPortRecPrivatePtr portPriv) } if (!winPriv) { - winPriv = malloc(sizeof(KdXVWindowRec)); + winPriv = calloc(1, sizeof(KdXVWindowRec)); if (!winPriv) return BadAlloc; winPriv->PortRec = portPriv;