syncterm-20010320 from FreeBSD ports crashed whenever
something was about to be pasted to syncterm from
rxvt or rxvt-unicode. Pasting from xterm worked fine.

This problem has been reported here
as well:

http://cpugod.synchro.net/msgs/msg.ssjs?msg_sub=syncprog&message=1081

Crash happened because call to XGetWindowProperty in
sdl_con.c:1740 failed with XBadAtom. Apparently,
req->property received over the network was 0.

First, I have added proper error checking, so
that syncterm ignores responses it cannot handle
and properly unblocks waiting thread.

Then, I could see the actual request 
and reponse (as directed to (u)rxvt)
 

	 ............REQUEST: ConvertSelection
	     sequence number: 012c
	      request length: 0006
	           requestor: WIN 0260000d
	           selection: <PRIMARY>
	              target: <STRING>
	            property: <NONE>
	                time: CurrentTime
				 ..............EVENT: SelectionRequest
				     sequence number: 0208
				                time: CurrentTime
				               owner: WIN 02400008
				           requestor: WIN 0260000d
				           selection: <PRIMARY>
				              target: <STRING>
				            property: <NONE>
				 ..............ERROR: Atom
				 ....SYNTHETIC EVENT: SelectionNotify
				     sequence number: 012c
				                time: CurrentTime
				           requestor: WIN 0260000d
				           selection: <PRIMARY>
				              target: <STRING>
				            property: <NONE>

This resulted, in turn, in a crash of rxvt with

urxvt: An X Error occurred, trying to continue after report.
urxvt: X Error of failed request:  BadAtom (invalid Atom parameter)
urxvt: Major opcode of failed request:  18
urxvt: (which is X_ChangeProperty)
urxvt: Atom id in failed request:  0x0
urxvt: Serial number of failed request:  521

The root cause is that sdl_con.c:1695 passes None
as the property. This is not forbidden by the X protocol,
and it means that the other client should support 
property identifier (atom), where clipboard contents
is about to be stored. There are examples of this approach here:

http://michael.toren.net/mirrors/doc/X-copy+paste.txt

ICCCM[1] says, that only obsolete clients should do this, and
t recommends that clients owning selection should use
the target atom as the property name.

xterm is handling this, but rxvt and derivatives do not.

While I have posted bug reports to both rxvt[2] and
urxvt[3] to handle such clients better, I still
think it should be fixed in syncterm.

This archive contains a patch to make SelectionRequest
handling more robust and to change the way
ConvertSelection request is sent.

The diff was made sdl_con.c revision 1.172

After patching the request looks like:

	 ............REQUEST: ConvertSelection
	     sequence number: 0b20
	      request length: 0006
	           requestor: WIN 0100000d
	           selection: <PRIMARY>
	              target: <STRING>
	            property: <STRING>
	                time: CurrentTime
				 ..............EVENT: SelectionRequest
				     sequence number: 01a2
				                time: CurrentTime
				               owner: WIN 02400008
				           requestor: WIN 0100000d
				           selection: <PRIMARY>
				              target: <STRING>
				            property: <STRING>
				 ....SYNTHETIC EVENT: SelectionNotify
				     sequence number: 0b20
				                time: CurrentTime
				           requestor: WIN 0100000d
				           selection: <PRIMARY>
				              target: <STRING>
				            property: <STRING>

Property name is filled in as expected by terminal
emulators and content is returned.

[1] http://tronche.com/gui/x/icccm/sec-2.html#s-2.2

[2] rxvt bug report:

https://sourceforge.net/tracker/?func=detail&aid=3289852&group_id=221&atid=100221

[3] rxvt-unicode bug report:

http://thread.gmane.org/gmane.comp.terminal-emulators.rxvt-unicode.general/958

-- 
Marcin Cieslak
saper@SAPER.INFO
