Subject: Re: bug#24041: 25.1.50; xwidget + -nw mode gives segfault

From: Robert Cochran <[email-masked]>
Date: Sun, 21 Aug 2016 19:12:41 -0700

Eli Zaretskii <[email-masked]> writes:

>> (Is that even possible? Start a graphical frame from a tty Emacs?)
>
> Of course, it is.  emacsclient can do that.  Assuming Emacs was built
> with GUI support, of course.  We have this feature for several years
> now.
>

Indeed it can. Never tried it before, and kinda assumed it wouldn't
work. Silly me.

Anyways, I have a patch, below, that's my first stab at solving the
problem. All it does is call `check_x_display_info` with the current
frame and allows any resulting error signals to propagate back up.

Probably not the most elegant solution, but I'm not entirely clear what
can and can't be done from within the Emacs C core. Suggestions are very
welcome.

-----

>From 82532f657f7acc824745406e8917a1a4f49723d9 Mon Sep 17 00:00:00 2001
From: Robert Cochran <[email-masked]>
Date: Sun, 21 Aug 2016 19:01:14 -0700
Subject: [PATCH] Signal an error when a TTY frame tries to create an xwidget

* src/xwidget.c (make-xwidget): signal an error if current frame is a
  TTY frame.

Trying to use a TTY frame to create an xwidget can cause a
segfault. Check before hand that the frame is an X frame via
check_x_display_info, which will signal an error if it is not. This
fixes Bug#24041.
---
 src/xwidget.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/src/xwidget.c b/src/xwidget.c
index f5f4da0..0edfacc 100644
--- a/src/xwidget.c
+++ b/src/xwidget.c
@@ -99,8 +99,15 @@ Returns the newly constructed xwidget, or nil if construction fails.  */)
   CHECK_NATNUM (width);
   CHECK_NATNUM (height);
 
-  struct xwidget *xw = allocate_xwidget ();
+  struct xwidget *xw;
   Lisp_Object val;
+
+  /* Ensure that the current frame is an X frame before we try
+     creating any xwidgets. If it isn't, check_x_display_info will
+     signal an error.  */
+  check_x_display_info (Fselected_frame ());
+
+  xw = allocate_xwidget ();
   xw->type = type;
   xw->title = title;
   xw->buffer = NILP (buffer) ? Fcurrent_buffer () : Fget_buffer_create (buffer);
-- 
2.7.4

-----

HTH,
-- 
~Robert Cochran

GPG Fingerprint - E778 2DD4 FEA6 6A68 6F26  AD2D E5C3 EB36 4886 8871