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 <alan.coopersmith@oracle.com>
Part-of: <https://gitlab.freedesktop.org/xorg/driver/xf86-video-nested/-/merge_requests/7>
This commit is contained in:
Alan Coopersmith
2024-10-26 09:50:58 -07:00
parent 86b6dc3bb6
commit 03dab66493

View File

@@ -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) {