Subject: Re: bug#36740: 27.0.50; apparently buggy code in ccl.c (lookup-integer-constant)

From: Pip Cet <[email-masked]>
Date: Sat, 20 Jul 2019 20:18:21 +0000

On Sat, Jul 20, 2019 at 7:00 PM Andy Moreton <[email-masked]> wrote:
> > Patch attached. It'd be nice to have tests for this, of course, but
> > it'd be easier for someone who understands CCL to do...
>
> Are the tests in test/lisp/international/ccl-tests.el sufficient ? If
> not, please etend them to cover this case.

I'm trying, but it seems like the CCL code is somewhat broken:
`ccl-embed-symbol', as it is now, can never succeed, because it passes
a cons cell rather than a fixnum to (ultimately) logand.

The attached patch tries to fix that issue and adds a test. I've also
noticed I cannot use C-g to interrupt a CCL loop, is that intentional?
From dbad96e07aaa5038198047a0d2e4102c820f0b69 Mon Sep 17 00:00:00 2001
From: Pip Cet <[email-masked]>
Date: Sat, 20 Jul 2019 14:47:39 +0000
Subject: [PATCH] Fix return value for CCL opcode lookup-integer

* src/ccl.c (ccl_driver): Fix LookupIntConstTbl return value.
* test/lisp/international/ccl-tests.el (ccl-hash-table): Add test.
* lisp/international/ccl.el (ccl-fixnum): Don't pass non-numbers
to logand.
---
 lisp/international/ccl.el            |  4 +++-
 src/ccl.c                            |  2 +-
 test/lisp/international/ccl-tests.el | 14 ++++++++++++++
 3 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/lisp/international/ccl.el b/lisp/international/ccl.el
index 51626f5161..025cb50c06 100644
--- a/lisp/international/ccl.el
+++ b/lisp/international/ccl.el
@@ -190,7 +190,9 @@ ccl-current-ic
 ;; codeword to 28bits, and then sign extends the result to a fixnum.
 (defun ccl-fixnum (code)
   "Convert a CCL code word to a fixnum value."
-  (- (logxor (logand code #x0fffffff) #x08000000) #x08000000))
+  (if (integerp code)
+      (- (logxor (logand code #x0fffffff) #x08000000) #x08000000)
+    code))
 
 (defun ccl-embed-data (data &optional ic)
   "Embed integer DATA in `ccl-program-vector' at `ccl-current-ic' and
diff --git a/src/ccl.c b/src/ccl.c
index ff42c6f25f..da2559e0b1 100644
--- a/src/ccl.c
+++ b/src/ccl.c
@@ -1299,7 +1299,7 @@ #define EXCMD (field1 >> 6)
 		    if (! (IN_INT_RANGE (eop) && CHARACTERP (opl)))
 		      CCL_INVALID_CMD;
 		    reg[RRR] = charset_unicode;
-		    reg[rrr] = eop;
+		    reg[rrr] = XFIXNUM (opl);
 		    reg[7] = 1; /* r7 true for success */
 		  }
 		else
diff --git a/test/lisp/international/ccl-tests.el b/test/lisp/international/ccl-tests.el
index 69e3930d42..2de4d040c2 100644
--- a/test/lisp/international/ccl-tests.el
+++ b/test/lisp/international/ccl-tests.el
@@ -227,3 +227,17 @@ ccl-dump-midi
   (with-temp-buffer
     (ccl-dump prog-midi-code)
     (should (equal (buffer-string) prog-midi-dump))))
+
+(ert-deftest ccl-hash-table ()
+  (let ((sym (gensym))
+        (table (make-hash-table :test 'eq)))
+    (puthash 16 17 table)
+    (puthash 17 16 table)
+    (define-translation-hash-table sym table)
+    (let* ((prog `(2
+                   ((loop
+                     (lookup-integer ,sym r0 r1)))))
+           (compiled (ccl-compile prog))
+           (registers [17 0 0 0 0 0 0 0]))
+      (ccl-execute compiled registers)
+      (should (equal registers [2 16 0 0 0 0 0 1])))))
-- 
2.22.0