From 9c2b6865bec865197195245612dd1571a0ddcecb Mon Sep 17 00:00:00 2001 From: Haroldo Santos Date: Sat, 10 Oct 2026 18:07:30 -0400 Subject: [PATCH] wrlib: release context resources on RCreateContext() failure RCreateContext() duplicates the attributes and creates a GC before it validates the visual and sets up the colormap. Its failure paths only called free(context), which leaks the attributes structure and the GC each time a context cannot be created (bad visual ID, colormap setup failure, gray scale allocation failure). Use RDestroyContext() on these paths so that everything allocated so far is released. Since the visual-ID failure happens before any drawable window is created, make RDestroyContext() destroy the drawable only when one exists, instead of calling XDestroyWindow() on None. Signed-off-by: Haroldo Santos --- wrlib/context.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/wrlib/context.c b/wrlib/context.c index 570f0ca5..ec3a3677 100644 --- a/wrlib/context.c +++ b/wrlib/context.c @@ -567,7 +567,7 @@ RContext *RCreateContext(Display * dpy, int screen_number, const RContextAttribu templ.visualid = context->attribs->visualid; vinfo = XGetVisualInfo(context->dpy, VisualIDMask | VisualScreenMask, &templ, &nret); if (!vinfo || nret == 0) { - free(context); + RDestroyContext(context); RErrorCode = RERR_BADVISUALID; return NULL; } @@ -614,13 +614,13 @@ RContext *RCreateContext(Display * dpy, int screen_number, const RContextAttribu if (context->vclass == PseudoColor || context->vclass == StaticColor) { if (!setupPseudoColorColormap(context)) { - free(context); + RDestroyContext(context); return NULL; } } else if (context->vclass == GrayScale || context->vclass == StaticGray) { context->colors = allocateGrayScale(context); if (!context->colors) { - free(context); + RDestroyContext(context); return NULL; } } else if (context->vclass == TrueColor) { @@ -665,7 +665,8 @@ void RDestroyContext(RContext *context) XFreeGC(context->dpy, context->copy_gc); if (context->attribs) { if ((context->attribs->flags & RC_VisualID) && - !(context->attribs->flags & RC_DefaultVisual)) + !(context->attribs->flags & RC_DefaultVisual) && + context->drawable != None) XDestroyWindow(context->dpy, context->drawable); free(context->attribs); }