From 87c5b5bf42c584d381e643aedec6feabff80ea79 Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Thu, 10 Apr 2025 20:18:42 +0200 Subject: [PATCH] xquartz: 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/xquartz/GL/capabilities.c | 2 +- hw/xquartz/GL/glcontextmodes.c | 17 ++++------------- hw/xquartz/GL/indirect.c | 5 +---- hw/xquartz/applewm.c | 13 ++++++------- hw/xquartz/darwin.c | 3 +-- hw/xquartz/keysym2ucs.c | 2 +- hw/xquartz/mach-startup/bundle-main.c | 15 ++++++--------- hw/xquartz/quartz.c | 2 +- hw/xquartz/quartzStartup.c | 2 +- hw/xquartz/xpr/dri.c | 5 ++--- hw/xquartz/xpr/x-list.c | 2 +- hw/xquartz/xpr/xprCursor.c | 4 ++-- hw/xquartz/xpr/xprScreen.c | 4 ++-- 13 files changed, 29 insertions(+), 47 deletions(-) diff --git a/hw/xquartz/GL/capabilities.c b/hw/xquartz/GL/capabilities.c index cc116b14b..027b5d58c 100644 --- a/hw/xquartz/GL/capabilities.c +++ b/hw/xquartz/GL/capabilities.c @@ -549,7 +549,7 @@ getGlCapabilities(struct glCapabilities *cap) continue; } - conf = malloc(sizeof(*conf)); + conf = calloc(1, sizeof(*conf)); if (NULL == conf) { FatalError("Unable to allocate memory for OpenGL capabilities\n"); } diff --git a/hw/xquartz/GL/glcontextmodes.c b/hw/xquartz/GL/glcontextmodes.c index 6e7f49af4..e4fb415c8 100644 --- a/hw/xquartz/GL/glcontextmodes.c +++ b/hw/xquartz/GL/glcontextmodes.c @@ -44,21 +44,12 @@ #if defined(IN_MINI_GLX) #include #include -#define _mesa_malloc(b) malloc(b) -#define _mesa_free(m) free(m) -#define _mesa_memset memset #else #ifdef XFree86Server #include #include -#define _mesa_malloc(b) malloc(b) -#define _mesa_free(m) free(m) -#define _mesa_memset memset #else #include -#define _mesa_memset memset -#define _mesa_malloc(b) Xmalloc(b) -#define _mesa_free(m) free(m) #endif /* XFree86Server */ #endif /* !defined(IN_MINI_GLX) */ @@ -129,7 +120,7 @@ _gl_copy_visual_to_context_mode(__GLcontextModes * mode, { __GLcontextModes * const next = mode->next; - (void)_mesa_memset(mode, 0, sizeof(__GLcontextModes)); + (void)memset(mode, 0, sizeof(__GLcontextModes)); mode->next = next; mode->visualID = config->vid; @@ -431,14 +422,14 @@ _gl_context_modes_create(unsigned count, size_t minimum_size) next = &base; for (i = 0; i < count; i++) { - *next = (__GLcontextModes *)_mesa_malloc(size); + *next = calloc(1, size); if (*next == NULL) { _gl_context_modes_destroy(base); base = NULL; break; } - (void)_mesa_memset(*next, 0, size); + (void)memset(*next, 0, size); (*next)->visualID = GLX_DONT_CARE; (*next)->visualType = GLX_DONT_CARE; (*next)->visualRating = GLX_NONE; @@ -476,7 +467,7 @@ _gl_context_modes_destroy(__GLcontextModes * modes) while (modes != NULL) { __GLcontextModes * const next = modes->next; - _mesa_free(modes); + free(modes); modes = next; } } diff --git a/hw/xquartz/GL/indirect.c b/hw/xquartz/GL/indirect.c index 993264bb7..83458cb4b 100644 --- a/hw/xquartz/GL/indirect.c +++ b/hw/xquartz/GL/indirect.c @@ -549,10 +549,7 @@ __glXAquaScreenCreateDrawable(ClientPtr client, XID glxDrawId, __GLXconfig *conf) { - __GLXAquaDrawable *glxPriv; - - glxPriv = malloc(sizeof *glxPriv); - + __GLXAquaDrawable *glxPriv = calloc(1, sizeof *glxPriv); if (glxPriv == NULL) return NULL; diff --git a/hw/xquartz/applewm.c b/hw/xquartz/applewm.c index 41ae6f280..e16412abd 100644 --- a/hw/xquartz/applewm.c +++ b/hw/xquartz/applewm.c @@ -224,7 +224,7 @@ static int ProcAppleWMSelectInput(register ClientPtr client) { REQUEST(xAppleWMSelectInputReq); - WMEventPtr pEvent, pNewEvent, *pHead; + WMEventPtr pEvent, *pHead; XID clientResource; int i; @@ -246,7 +246,7 @@ ProcAppleWMSelectInput(register ClientPtr client) } /* build the entry */ - pNewEvent = (WMEventPtr)malloc(sizeof(WMEventRec)); + WMEventPtr pNewEvent = calloc(1, sizeof(WMEventRec)); if (!pNewEvent) return BadAlloc; pNewEvent->next = 0; @@ -267,7 +267,7 @@ ProcAppleWMSelectInput(register ClientPtr client) * done through the resource database. */ if (i != Success || !pHead) { - pHead = (WMEventPtr *)malloc(sizeof(WMEventPtr)); + pHead = calloc(1, sizeof(WMEventPtr)); if (!pHead || !AddResource(eventResource, EventType, (void *)pHead)) { FreeResource(clientResource, X11_RESTYPE_NONE); @@ -368,16 +368,15 @@ ProcAppleWMReenableUpdate(register ClientPtr client) static int ProcAppleWMSetWindowMenu(register ClientPtr client) { - const char *bytes, **items; - char *shortcuts; + const char *bytes; int max_len, nitems, i, j; REQUEST(xAppleWMSetWindowMenuReq); REQUEST_AT_LEAST_SIZE(xAppleWMSetWindowMenuReq); nitems = stuff->nitems; - items = malloc(sizeof(char *) * nitems); - shortcuts = malloc(sizeof(char) * nitems); + const char **items = calloc(nitems, sizeof(char *)); + char *shortcuts = calloc(nitems, sizeof(char)); if (!items || !shortcuts) { free(items); diff --git a/hw/xquartz/darwin.c b/hw/xquartz/darwin.c index 74d889606..0110c5819 100644 --- a/hw/xquartz/darwin.c +++ b/hw/xquartz/darwin.c @@ -188,7 +188,6 @@ DarwinScreenInit(ScreenPtr pScreen, int argc, char **argv) int dpi; static int foundIndex = 0; Bool ret; - DarwinFramebufferPtr dfb; if (!dixRegisterPrivateKey(&darwinScreenKeyRec, PRIVATE_SCREEN, 0)) return FALSE; @@ -202,7 +201,7 @@ DarwinScreenInit(ScreenPtr pScreen, int argc, char **argv) } // allocate space for private per screen storage - dfb = malloc(sizeof(DarwinFramebufferRec)); + DarwinFramebufferPtr dfb = calloc(1, sizeof(DarwinFramebufferRec)); // SCREEN_PRIV(pScreen) = dfb; dixSetPrivate(&pScreen->devPrivates, darwinScreenKey, dfb); diff --git a/hw/xquartz/keysym2ucs.c b/hw/xquartz/keysym2ucs.c index 12da19d67..f5bf1badd 100644 --- a/hw/xquartz/keysym2ucs.c +++ b/hw/xquartz/keysym2ucs.c @@ -879,7 +879,7 @@ ucs2keysym(long ucs) int mid; if (reverse_keysymtab == NULL) { - reverse_keysymtab = malloc(sizeof(keysymtab)); + reverse_keysymtab = calloc(1, sizeof(keysymtab)); memcpy(reverse_keysymtab, keysymtab, sizeof(keysymtab)); qsort(reverse_keysymtab, diff --git a/hw/xquartz/mach-startup/bundle-main.c b/hw/xquartz/mach-startup/bundle-main.c index 4be365959..7328b965c 100644 --- a/hw/xquartz/mach-startup/bundle-main.c +++ b/hw/xquartz/mach-startup/bundle-main.c @@ -321,11 +321,9 @@ static int launchd_socket_handed_off = 0; kern_return_t do_request_fd_handoff_socket(mach_port_t port, string_t filename) { - socket_handoff_t *handoff_data; - launchd_socket_handed_off = 1; - handoff_data = (socket_handoff_t *)calloc(1, sizeof(socket_handoff_t)); + socket_handoff_t *handoff_data = calloc(1, sizeof(socket_handoff_t)); if (!handoff_data) { ErrorF("X11.app: Error allocating memory for handoff_data\n"); return KERN_FAILURE; @@ -528,7 +526,6 @@ setup_console_redirect(const char *bundle_id) static void setup_env(void) { - char *temp; const char *pds = NULL; const char *disp = getenv("DISPLAY"); size_t len; @@ -559,7 +556,7 @@ setup_env(void) setenv("X11_PREFS_DOMAIN", server_bootstrap_name, 1); len = strlen(server_bootstrap_name); - bundle_id_prefix = malloc(sizeof(char) * (len - 3)); + bundle_id_prefix = calloc(len-3, sizeof(char)); if (!bundle_id_prefix) { ErrorF("X11.app: Memory allocation error.\n"); exit(1); @@ -582,7 +579,7 @@ setup_env(void) "X11.app: Detected old style launchd DISPLAY, please update xinit.\n"); } else { - temp = (char *)malloc(sizeof(char) * len); + char *temp = calloc(len, sizeof(char)); if (!temp) { ErrorF( "X11.app: Memory allocation error creating space for socket name test.\n"); @@ -613,7 +610,7 @@ setup_env(void) ensure_path(X11BINDIR); /* cd $HOME */ - temp = getenv("HOME"); + char *temp = getenv("HOME"); if (temp != NULL && temp[0] != '\0') chdir(temp); } @@ -781,14 +778,14 @@ command_from_prefs(const char *key, const char *default_value) CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication); CFRelease(cfDefaultValue); - command = (char *)malloc(len * sizeof(char)); + command = calloc(len, sizeof(char)); if (!command) goto command_from_prefs_out; strcpy(command, default_value); } else { int len = CFStringGetLength((CFStringRef)PlistRef) + 1; - command = (char *)malloc(len * sizeof(char)); + command = calloc(len, sizeof(char)); if (!command) goto command_from_prefs_out; CFStringGetCString((CFStringRef)PlistRef, command, len, diff --git a/hw/xquartz/quartz.c b/hw/xquartz/quartz.c index da30f1f08..16a392d9f 100644 --- a/hw/xquartz/quartz.c +++ b/hw/xquartz/quartz.c @@ -532,7 +532,7 @@ QuartzCopyDisplayIDs(ScreenPtr pScreen, free(pQuartzScreen->displayIDs); if (displayCount) { size_t size = displayCount * sizeof(CGDirectDisplayID); - pQuartzScreen->displayIDs = malloc(size); + pQuartzScreen->displayIDs = calloc(1, size); memcpy(pQuartzScreen->displayIDs, displayIDs, size); } else { diff --git a/hw/xquartz/quartzStartup.c b/hw/xquartz/quartzStartup.c index 4c346433b..ad62909a8 100644 --- a/hw/xquartz/quartzStartup.c +++ b/hw/xquartz/quartzStartup.c @@ -81,7 +81,7 @@ create_thread(void *func, void *arg) void QuartzInitServer(int argc, char **argv, char **envp) { - struct arg *args = (struct arg *)malloc(sizeof(struct arg)); + struct arg *args = calloc(1, sizeof(struct arg)); if (!args) FatalError("Could not allocate memory.\n"); diff --git a/hw/xquartz/xpr/dri.c b/hw/xquartz/xpr/dri.c index 52a5308b8..c01096194 100644 --- a/hw/xquartz/xpr/dri.c +++ b/hw/xquartz/xpr/dri.c @@ -265,7 +265,7 @@ CreateSurfaceForWindow(ScreenPtr pScreen, WindowPtr pWin, xp_window_changes wc; /* allocate a DRI Window Private record */ - if (!(pDRIDrawablePriv = malloc(sizeof(*pDRIDrawablePriv)))) { + if (!(pDRIDrawablePriv = calloc(1, sizeof(*pDRIDrawablePriv)))) { return NULL; } @@ -681,7 +681,6 @@ DRICreatePixmap(ScreenPtr pScreen, Drawable id, DrawablePtr pDrawable, char *path, size_t pathmax) { - DRIPixmapBufferPtr shared; PixmapPtr pPix; if (pDrawable->type != DRAWABLE_PIXMAP) @@ -689,7 +688,7 @@ DRICreatePixmap(ScreenPtr pScreen, Drawable id, pPix = (PixmapPtr)pDrawable; - shared = malloc(sizeof(*shared)); + DRIPixmapBufferPtr shared = calloc(1, sizeof(*shared)); if (NULL == shared) { FatalError("failed to allocate DRIPixmapBuffer in %s\n", __func__); } diff --git a/hw/xquartz/xpr/x-list.c b/hw/xquartz/xpr/x-list.c index 0250ac549..8ac0a287f 100644 --- a/hw/xquartz/xpr/x-list.c +++ b/hw/xquartz/xpr/x-list.c @@ -90,7 +90,7 @@ X_PFX(list_prepend) (x_list * lst, void *data) { x_list_block *b; int i; - b = malloc(sizeof(x_list_block)); + b = calloc(1, sizeof(x_list_block)); assert(b != NULL); for (i = 0; i < NODES_PER_BLOCK - 1; i++) diff --git a/hw/xquartz/xpr/xprCursor.c b/hw/xquartz/xpr/xprCursor.c index 8207a9811..3ac92435e 100644 --- a/hw/xquartz/xpr/xprCursor.c +++ b/hw/xquartz/xpr/xprCursor.c @@ -93,7 +93,7 @@ load_cursor(CursorPtr src, int screen) const uint32_t *be_data = (uint32_t *)src->bits->argb; unsigned i; rowbytes = src->bits->width * sizeof(CARD32); - data = malloc(rowbytes * src->bits->height); + data = calloc(src->bits->height, rowbytes); free_data = TRUE; if (!data) { FatalError("Failed to allocate memory in %s\n", __func__); @@ -119,7 +119,7 @@ load_cursor(CursorPtr src, int screen) /* round up to 8 pixel boundary so we can convert whole bytes */ rowbytes = ((src->bits->width * 4) + 31) & ~31; - data = malloc(rowbytes * src->bits->height); + data = calloc(src->bits->height, rowbytes); free_data = TRUE; if (!data) { FatalError("Failed to allocate memory in %s\n", __func__); diff --git a/hw/xquartz/xpr/xprScreen.c b/hw/xquartz/xpr/xprScreen.c index f300b28b1..49faffc0e 100644 --- a/hw/xquartz/xpr/xprScreen.c +++ b/hw/xquartz/xpr/xprScreen.c @@ -198,7 +198,7 @@ static void xprAddPseudoramiXScreens(int *x, int *y, int *width, int *height, ScreenPtr pScreen) { - CGDisplayCount i, displayCount; + CGDisplayCount i; CGDirectDisplayID *displayList = NULL; CGRect unionRect = CGRectNull, frame; @@ -224,7 +224,7 @@ xprAddPseudoramiXScreens(int *x, int *y, int *width, int *height, if (CGDisplayIsCaptured(kCGDirectMainDisplay)) displayCount = 1; - displayList = malloc(displayCount * sizeof(CGDirectDisplayID)); + CGDisplayCount displayList = calloc(displayCount, sizeof(CGDirectDisplayID)); if (!displayList) FatalError("Unable to allocate memory for list of displays.\n"); CGGetActiveDisplayList(displayCount, displayList, &displayCount);