GNU bug report logs - #12051
24.1; rcirc-send-message doesn't take multibyte into account.

Previous Next

Package: emacs;

Reported by: Li Ian-Xue <b4283 <at> bephor.org>

Date: Thu, 26 Jul 2012 01:17:02 UTC

Severity: normal

Found in version 24.1

Done: Leo <sdl.web <at> gmail.com>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: help-debbugs <at> gnu.org (GNU bug Tracking System)
To: Leo <sdl.web <at> gmail.com>
Cc: tracker <at> debbugs.gnu.org
Subject: bug#12051: closed (24.1; rcirc-send-message doesn't take
 multibyte into account.)
Date: Tue, 14 Aug 2012 13:21:01 +0000
[Message part 1 (text/plain, inline)]
Your message dated Tue, 14 Aug 2012 21:11:15 +0800
with message-id <m1sjbp62ks.fsf <at> gmail.com>
and subject line Re: bug#12051: 24.1; rcirc-send-message doesn't take multibyte into account.
has caused the debbugs.gnu.org bug report #12051,
regarding 24.1; rcirc-send-message doesn't take multibyte into account.
to be marked as done.

(If you believe you have received this mail in error, please contact
help-debbugs <at> gnu.org.)


-- 
12051: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=12051
GNU Bug Tracking System
Contact help-debbugs <at> gnu.org with problems
[Message part 2 (message/rfc822, inline)]
From: Li Ian-Xue <b4283 <at> bephor.org>
To: bug-gnu-emacs <at> gnu.org
Subject: 24.1; rcirc-send-message doesn't take multibyte into account.
Date: Thu, 26 Jul 2012 00:18:29 +0800
[Message part 3 (text/plain, inline)]
Hello developers,

I discovered recently that the irc client `rcirc', although has an
max-message-length set, but it simply uses (length str) for detecting
the output length, which is not desirable for multibyte users because
usually our characters encode to more than one byte, and this causes an
error that the client actually sends out more bytes than the standard
has required (512 bytes to my understanding).

This limit is easily reached since chinese characters are usually
encoded with 3 bytes for one character.

By this error, if the server truncates the result string simply by
bytes, then it's known to cause the string to become entirely scrambles
for xchat.

I'm attaching a patch to perform an binary search for multibyte strings,
and this patch should not have any penalties for original ascii users
since it begins with a (multibyte-string-p) to decide which style to use.

[rcirc-fix-multibyte-overflow.patch (text/x-patch, inline)]
--- rcirc.el	2012-07-25 23:52:41.813226461 +0800
+++ rcirc-1.el	2012-07-25 23:55:20.813220626 +0800
@@ -792,21 +792,40 @@
 (defvar rcirc-max-message-length 420
   "Messages longer than this value will be split.")
 
+(defun rcirc-multibyte-position-at-byte (str bytes)
+  (if (multibyte-string-p str)
+      (rcirc-multibyte-position-at-byte-1 str bytes 0 0)
+      bytes))
+
+(defun rcirc-multibyte-position-at-byte-1 (str bytes now-chars now-bytes)
+  (let ((len (length str)))
+    (if (<= len 1)
+        now-chars
+      (let* ((half-len (/ len 2))
+             (lstr (substring str 0 half-len))
+             (rstr (substring str half-len len))
+             (now-bytes-1 (+ now-bytes (string-bytes lstr))))
+        (if (> now-bytes-1 bytes)
+            (rcirc-multibyte-position-at-byte-1 lstr bytes now-chars now-bytes)
+          (rcirc-multibyte-position-at-byte-1 rstr bytes (+ half-len now-chars) now-bytes-1))))))
+
 (defun rcirc-send-message (process target message &optional noticep silent)
   "Send TARGET associated with PROCESS a privmsg with text MESSAGE.
 If NOTICEP is non-nil, send a notice instead of privmsg.
 If SILENT is non-nil, do not print the message in any irc buffer."
   ;; max message length is 512 including CRLF
   (let* ((response (if noticep "NOTICE" "PRIVMSG"))
-         (oversize (> (length message) rcirc-max-message-length))
+         (oversize (> (string-bytes message) rcirc-max-message-length))
+         (adjusted-pos (if oversize
+                            (rcirc-multibyte-position-at-byte message rcirc-max-message-length)))
          (text (if oversize
-                   (substring message 0 rcirc-max-message-length)
+                   (substring message 0 adjusted-pos)
                  message))
          (text (if (string= text "")
                    " "
                  text))
          (more (if oversize
-                   (substring message rcirc-max-message-length))))
+                   (substring message adjusted-pos))))
     (rcirc-get-buffer-create process target)
     (rcirc-send-string process (concat response " " target " :" text))
     (unless silent
[Message part 5 (message/rfc822, inline)]
From: Leo <sdl.web <at> gmail.com>
To: 12051-done <at> debbugs.gnu.org
Subject: Re: bug#12051: 24.1;
	rcirc-send-message doesn't take multibyte into account.
Date: Tue, 14 Aug 2012 21:11:15 +0800
Fixed in emacs 24.2


This bug report was last modified 12 years and 284 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.