From 03dab66493622835a29b873cd63df489f1a96ed9 Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Sat, 26 Oct 2024 09:50:58 -0700 Subject: [PATCH] NestedClientCreateScreen: avoid leaks on failure paths Found by Oracle Parfait 13.3 static analyzer: Memory leak [memory-leak]: Memory leak of pointer pPriv allocated with malloc(168) at line 182 of xlibclient.c in function 'NestedClientCreateScreen'. pPriv allocated at line 177 with malloc(168) Signed-off-by: Alan Coopersmith Part-of: --- src/xlibclient.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/xlibclient.c b/src/xlibclient.c index 7d5a66d..89bd2d7 100644 --- a/src/xlibclient.c +++ b/src/xlibclient.c @@ -174,19 +174,18 @@ NestedClientCreateScreen(int scrnIndex, Bool supported; char windowTitle[32]; - pPriv = malloc(sizeof(struct NestedClientPrivate)); + pPriv = calloc(1, sizeof(struct NestedClientPrivate)); pPriv->scrnIndex = scrnIndex; pPriv->display = XOpenDisplay(displayName); if (!pPriv->display) - return NULL; + goto bail; supported = XkbQueryExtension(pPriv->display, &pPriv->xkb.op, &pPriv->xkb.event, &pPriv->xkb.error, &pPriv->xkb.major, &pPriv->xkb.minor); if (!supported) { xf86DrvMsg(pPriv->scrnIndex, X_ERROR, "The remote server does not support the XKEYBOARD extension.\n"); - XCloseDisplay(pPriv->display); - return NULL; + goto bail; } pPriv->screenNumber = DefaultScreen(pPriv->display); @@ -228,14 +227,14 @@ NestedClientCreateScreen(int scrnIndex, 0 /* XXX: bytes_per_line */); if (!pPriv->img) - return NULL; + goto bail; pPriv->img->data = malloc(pPriv->img->bytes_per_line * pPriv->img->height); pPriv->usingShm = FALSE; } if (!pPriv->img->data) - return NULL; + goto bail; NestedClientHideCursor(pPriv); /* Hide cursor */ @@ -266,6 +265,15 @@ xf86DrvMsg(scrnIndex, X_INFO, "blu_mask: 0x%lx\n", pPriv->img->blue_mask); pPriv->dev = (DeviceIntPtr)NULL; return pPriv; + + bail: + if (pPriv->img) + /* XDestroyImage will free(pPriv->img->data) for us */ + XDestroyImage(pPriv->img); + if (pPriv->display) + XCloseDisplay(pPriv->display); + free(pPriv); + return NULL; } void NestedClientHideCursor(NestedClientPrivatePtr pPriv) {