From 17fa2a0d5d4d0556bf8dfa77609220c6feb98bda Mon Sep 17 00:00:00 2001 From: David Robillard Date: Thu, 16 Dec 2021 15:45:20 -0500 Subject: X11: Fix potential crash in puglGrabFocus() This is really a mistake in user code, but things shouldn't crash in general. So, this commit fixes the crash and adds some documentation so that developers hopefully don't try to grab focus before it makes sense. The case that was previously a crash will now gracefully fail, that is, the focus will not be (and can not be) grabbed. --- src/x11.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/x11.c b/src/x11.c index 455ee20..1c022e6 100644 --- a/src/x11.c +++ b/src/x11.c @@ -795,7 +795,16 @@ translateEvent(PuglView* const view, XEvent xevent) PuglStatus puglGrabFocus(PuglView* const view) { - PuglInternals* const impl = view->impl; + PuglInternals* const impl = view->impl; + XWindowAttributes attrs = {0}; + + if (!impl->win || !XGetWindowAttributes(impl->display, impl->win, &attrs)) { + return PUGL_UNKNOWN_ERROR; + } + + if (attrs.map_state != IsViewable) { + return PUGL_FAILURE; + } XSetInputFocus(impl->display, impl->win, RevertToNone, CurrentTime); return PUGL_SUCCESS; -- cgit v1.2.1