Subject: bug#70073: 29.3; Emacs-PGTK crashes upon marking some text inside emacs and then middle clicking outside of Emacs

From: "Abraham S.A.H." <[email-masked]>
Date: Fri, 29 Mar 2024 15:52:12 +0100 (CET)

## System Information:

* Kernel: 6.8.2-2
* DE: Gnome 46.0
* WM: Mutter (Wayland)
* gnome-shell: 46.0
* wayland: 1.22.0
* mutter: 46.0
* wl-clipboard: 2.2.1
* weston: 13.0.0
* sway: 1.9-e93da93
* emacs package: emacs-wayland 29.3-1.1 (archlinux)

## Steps to reproduce the issue under Gnome:

1. Invoke `emacs -Q` in any terminal emulator to start Emacs along with
   its GUI frame.
2. Mark/highlight some area of text with your mouse pointer. OR use
   C-Space and keyboard movement shortcuts to do the same.
3. Open any other application. (gnome-text-editor, for example)
4. Use your middle mouse button in an editable text area of the second
   application's frame.
5. Emacs crashes.

NOTE1: Emacs doesn't crash if you just middle click in any 
       *non-textual/non-editable* area, outside Emacs.
NOTE2: Middle clicking *inside* any of Emacs' own buffers DOES work
       properly without any crash or noticeable problem.
NOTE3: The problem is reproducible both when marking with mouse cursor
       or when marking with keyboard shortcuts.
NOTE4: Using another shortcut that pastes the Primary selection of the
       wayland, instead of mouse's middle click, also reproduces the 
       same issue. E.g. S-Insert of WezTerm.
NOTE5: Marking a text outside of Emacs and middle clicking inside
       Emacs works flawlessly.

## State of the issue under Sway:

The issue is completely reproducable under sway with the same steps as
above, with the same symptoms and the same segmentation fault error.

## State of the issue under Weston:

The issue is not reproducable under Weston but the functionality is
also lost.  Weston doesn't support primary selection.

## Tried Workarounds:

### Workaround number 1:
Disable the primary selection.  In gnome you can disable 
primary selection.  Brings users to the same state of under 
Weston.  Emacs will not crash, Primary selection works inside 
Emacs, but not outside of Emacs.

Emacs still crashes if user uses the middle click inside applications
that try to pull primary selection with middle click.

### Workaround number 2:
Set variable `select-active-regions` to `nil`.	Disables the
functionality and of course removes the issue.

## Terminal log of Emacs after crash:

~~~~~~~ Start of the terminal log

Fatal error 11: Segmentation fault
Backtrace:
emacs(+0x16bc62)[0x5f8b0fcddc62]
emacs(+0x20243)[0x5f8b0fb92243]
emacs(+0x21d44)[0x5f8b0fb93d44]
/usr/lib/libc.so.6(+0x41b30)[0x735b53041b30]
emacs(+0x3027c9)[0x5f8b0fe747c9]
emacs(+0x14950d)[0x5f8b0fcbb50d]
emacs(+0x14a631)[0x5f8b0fcbc631]
emacs(+0x275406)[0x5f8b0fde7406]
emacs(+0x44808)[0x5f8b0fbb6808]
emacs(+0x152ea1)[0x5f8b0fcc4ea1]
emacs(+0x317837)[0x5f8b0fe89837]
emacs(+0x1465ec)[0x5f8b0fcb85ec]
emacs(+0x1fd94f)[0x5f8b0fd6f94f]
emacs(+0x143fae)[0x5f8b0fcb5fae]
emacs(+0x1fd8a5)[0x5f8b0fd6f8a5]
emacs(+0x145dc9)[0x5f8b0fcb7dc9]
emacs(+0x34bf40)[0x5f8b0febdf40]
emacs(+0x146300)[0x5f8b0fcb8300]
emacs(+0x380d3)[0x5f8b0fbaa0d3]
/usr/lib/libc.so.6(+0x26010)[0x735b53026010]
/usr/lib/libc.so.6(__libc_start_main+0x8a)[0x735b530260ca]
emacs(+0x38445)[0x5f8b0fbaa445]
fish: Job 1, 'emacs --debug-init' terminated by signal SIGSEGV (Address
boundary error)

~~~~~~~ End of the log

In GNU Emacs 29.3 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.41,
cairo version 1.18.0)
Repository revision: 4b1bcc2ad665ccd3fe1d4b8a206f9bf3a8bc2c98
Repository branch: HEAD
System Description: Arch Linux

Configured using:
'configure --with-pgtk --with-native-compilation=aot --sysconfdir=/etc
--prefix=/usr --libexecdir=/usr/lib --with-tree-sitter
--localstatedir=/var --with-cairo --disable-build-details
--with-harfbuzz --with-libsystemd --with-modules
'CFLAGS=-march=x86-64-v3 -mtune=haswell -O3 -pipe -fno-plt -fexceptions
-mpclmul -Wp,-D_FORTIFY_SOURCE=3 -Wformat -Werror=format-security
-fstack-clash-protection -fcf-protection -g1
-ffile-prefix-map=/startdir/src=/usr/src/debug/emacs -flto=auto
-falign-functions=32' 'LDFLAGS=-Wl,-O1 -Wl,--sort-common
-Wl,--as-needed -Wl,-z,relro -Wl,-z,now -Wl,-z,pack-relative-relocs
-flto=auto -falign-functions=32' 'CXXFLAGS=-march=x86-64-v3
-mtune=haswell -O3 -pipe -fno-plt -fexceptions -mpclmul
-Wp,-D_FORTIFY_SOURCE=3 -Wformat -Werror=format-security
-fstack-clash-protection -fcf-protection -Wp,-D_GLIBCXX_ASSERTIONS -g1
-ffile-prefix-map=/startdir/src=/usr/src/debug/emacs -flto=auto
-falign-functions=32''

Configured features:
ACL CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GPM GSETTINGS HARFBUZZ JPEG
JSON LCMS2 LIBOTF LIBSYSTEMD LIBXML2 MODULES NATIVE_COMP NOTIFY INOTIFY
PDUMPER PGTK PNG RSVG SECCOMP SOUND SQLITE3 THREADS TIFF
TOOLKIT_SCROLL_BARS TREE_SITTER WEBP XIM GTK3 ZLIB

Important settings:
  value of $LC_COLLATE: en_GB.UTF-8
  value of $LC_CTYPE: en_GB.UTF-8
  value of $LC_MESSAGES: en_GB.UTF-8
  value of $LC_MONETARY: fa_IR.UTF-8
  value of $LC_NUMERIC: en_GB.UTF-8
  value of $LC_TIME: en_DK.UTF-8
  value of $LANG: en_GB.UTF-8
  locale-coding-system: utf-8-unix

Major mode: Lisp Interaction

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  show-paren-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  line-number-mode: t
  indent-tabs-mode: t
  transient-mark-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message mailcap yank-media puny dired
dired-loaddefs rfc822 mml mml-sec password-cache epa derived epg rfc6068
epg-config gnus-util text-property-search time-date mm-decode mm-bodies
mm-encode mail-parse rfc2231 mailabbrev gmm-utils mailheader cl-loaddefs
comp comp-cstr warnings icons subr-x rx cl-seq cl-macs gv cl-extra
help-mode bytecomp byte-compile cl-lib sendmail rfc2047 rfc2045
ietf-drums mm-util mail-prsvr mail-utils rmc iso-transl tooltip cconv
eldoc paren electric uniquify ediff-hook vc-hooks lisp-float-type
elisp-mode mwheel term/pgtk-win pgtk-win term/common-win pgtk-dnd
tool-bar dnd fontset image regexp-opt fringe tabulated-list replace
newcomment text-mode lisp-mode prog-mode register page tab-bar menu-bar
rfn-eshadow isearch easymenu timer select scroll-bar mouse jit-lock
font-lock syntax font-core term/tty-colors frame minibuffer nadvice seq
simple cl-generic indonesian philippine cham georgian utf-8-lang
misc-lang vietnamese tibetan thai tai-viet lao korean japanese eucjp-ms
cp51932 hebrew greek romanian slovak czech european ethiopic indian
cyrillic chinese composite emoji-zwj charscript charprop case-table
epa-hook jka-cmpr-hook help abbrev obarray oclosure cl-preloaded button
loaddefs theme-loaddefs faces cus-face macroexp files window
text-properties overlay sha1 md5 base64 format env code-pages mule
custom widget keymap hashtable-print-readable backquote threads dbusbind
inotify dynamic-setting system-font-setting font-render-setting cairo
gtk pgtk lcms2 multi-tty make-network-process native-compile emacs)

Memory information:
((conses 16 77836 11913)
(symbols 48 7110 0)
(strings 32 19779 1208)
(string-bytes 1 599931)
(vectors 16 15779)
(vector-slots 8 329592 20534)
(floats 8 28 46)
(intervals 56 291 0)
(buffers 984 12))