Compare commits

...
5 Commits
Author SHA1 Message Date
Haroldo Santos 558e67f303 WINGs: do not call memcpy() for empty data in WMCreateDataWithBytes()
WMCreateDataWithBytes() always copies 'length' bytes from 'bytes', even
when the length is zero. A caller creating an empty WMData naturally
passes a NULL pointer in that case, but passing NULL to memcpy() is
undefined behavior in C, even with a zero size, and static analyzers
report it.

Only copy when there is something to copy. The result is unchanged: the
data is created with the requested capacity and a length of zero.

Signed-off-by: Haroldo Santos <haroldo.santos@gmail.com>
2026-10-21 12:20:34 +01:00
Haroldo Santos 9c2b6865be 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 <haroldo.santos@gmail.com>
2026-10-21 12:20:34 +01:00
Haroldo Santos 0dd8b6a099 wrlib: check RCreateImage() result in RMakeTiledImage()
When the requested size is larger than the tile, RMakeTiledImage()
creates a new image with RCreateImage() and immediately writes to
image->data. RCreateImage() returns NULL when the allocation fails or
when the dimensions exceed its limit (20000 pixels), so a very large
background texture, or memory pressure, makes the caller crash instead
of getting an error.

The other branches of this function already return NULL on failure
(RCloneImage() and RGetSubImage()), and the callers handle a NULL
result, so return NULL here as well.

Signed-off-by: Haroldo Santos <haroldo.santos@gmail.com>
2026-10-21 12:20:34 +01:00
Haroldo Santos c0104d56bf wrlib: do not use the image cache when its allocation fails
init_cache() allocates RImageCache with malloc() for RImageCacheSize
entries. When the allocation fails it prints a warning and returns, but
leaves RImageCacheSize at its positive value.

RLoadImage() decides whether to use the cache by testing
RImageCacheSize > 0, so after such a failure every image load walks
RImageCache[i] through a NULL pointer and crashes. RReleaseCache()
likewise loops over the missing entries.

Reset RImageCacheSize to 0 when the allocation fails, so images are
simply loaded without caching, as already happens when the cache is
disabled by configuration.

Signed-off-by: Haroldo Santos <haroldo.santos@gmail.com>
2026-10-21 12:20:34 +01:00
Haroldo Santos 56ec56136f wmaker: fix NULL dereference in window shortcut with no focused window
The WKBD_WINDOW1..10 handler in dispatchWKBDCommand() receives 'wwin'
from handleKeyPress(), which takes it from scr->focused_window. That is
NULL when no window is focused, for instance on an empty workspace or
right after the last window was closed.

If the shortcut slot is empty and some windows are currently selected,
the last branch of the handler reads wwin->flags.selected without
checking wwin, so pressing the shortcut crashes the window manager.

Check that 'wwin' is not NULL before using it, as the other cases of
this function already do. Found with the clang static analyzer.

Signed-off-by: Haroldo Santos <haroldo.santos@gmail.com>
2026-10-21 12:20:34 +01:00
5 changed files with 10 additions and 5 deletions

No files matched your search

+1
View File
@@ -71,6 +71,7 @@ WMData *WMCreateDataWithBytes(const void *bytes, unsigned length)
aData = WMCreateDataWithCapacity(length);
aData->length = length;
if (length > 0)
memcpy(aData->bytes, bytes, length);
return aData;
+1 -1
View File
@@ -1931,7 +1931,7 @@ static void dispatchWKBDCommand(int command, WScreen *scr, WWindow *wwin, XEvent
} else if (scr->selected_windows && WMGetArrayItemCount(scr->selected_windows)) {
if (wwin->flags.selected && scr->selected_windows) {
if (wwin && wwin->flags.selected && scr->selected_windows) {
if (scr->shortcutWindows[widx]) {
WMFreeArray(scr->shortcutWindows[widx]);
}
+5 -4
View File
@@ -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);
}
+1
View File
@@ -130,6 +130,7 @@ static void init_cache(void)
RImageCache = malloc(sizeof(RCachedImage) * RImageCacheSize);
if (RImageCache == NULL) {
fprintf(stderr, _("wrlib: out of memory for image cache\n"));
RImageCacheSize = 0;
return;
}
memset(RImageCache, 0, sizeof(RCachedImage) * RImageCacheSize);
+2
View File
@@ -578,6 +578,8 @@ RImage *RMakeTiledImage(RImage * tile, unsigned width, unsigned height)
int has_alpha = HAS_ALPHA(tile);
image = RCreateImage(width, height, has_alpha);
if (!image)
return NULL;
d = image->data;
s = tile->data;