FIXED: Use more robust Prolog to C string conversion
authorEshel Yaron <me@eshelyaron.com>
Sat, 27 Aug 2022 17:57:16 +0000 (20:57 +0300)
committerEshel Yaron <me@eshelyaron.com>
Sat, 27 Aug 2022 17:57:16 +0000 (20:57 +0300)
sweep.c
sweep.el

diff --git a/sweep.c b/sweep.c
index ff148c71fb465471e0a0ddf9af19c91a33f5fc25..bccb1ad40bbf31b13231292b7670eaad00c22b72 100644 (file)
--- a/sweep.c
+++ b/sweep.c
@@ -102,8 +102,10 @@ term_to_value_string(emacs_env *eenv, term_t t) {
   char * string = NULL;
   emacs_value v = NULL;
   size_t      l = -1;
-  if (PL_get_string_chars(t, &string, &l)) {
+  if (PL_get_nchars(t, &l, &string, CVT_STRING|REP_UTF8)) {
     v = eenv->make_string(eenv, string, l);
+  } else {
+    v = eenv->make_string(eenv, "sweep conversion error", 22);
   }
   return v;
 }
index 88dfe1b0a2abbbea11da5796420534e568c358a0..ed8c7af17a0e2781ec643073eb046fab89128cb2 100644 (file)
--- a/sweep.el
+++ b/sweep.el
 
 ;;; Code:
 
+(defgroup sweep nil
+  "SWI-Prolog Embedded in Emacs."
+  :group 'prolog)
+
 (defvar sweep-install-buffer-name "*Install sweep*"
   "Name of the buffer used for compiling sweep-module.")
 
@@ -88,6 +92,7 @@
                       nil))))))
     (completing-read "Predicate: " col)))
 
+;;;###autoload
 (defun sweep-find-predicate (mfn)
   "Jump to the definiton of the Prolog predicate MFN.
 MFN must be a string of the form \"M:F/N\" where M is a Prolog
@@ -125,12 +130,13 @@ module name, F is a functor name and N is its arity."
                   (let* ((val (cdr (assoc-string key col)))
                          (pat (car val))
                          (des (cdr val)))
-                    (concat (make-string (- 32 (length key)) ? )
+                    (concat (make-string (max 0 (- 32 (length key))) ? )
                             (if des
-                                (concat pat (make-string (- 64 (length pat)) ? ) des)
+                                (concat pat (make-string (max 0 (- 80 (length pat))) ? ) des)
                               pat)))))))
     (completing-read "Module: " col)))
 
+;;;###autoload
 (defun sweep-find-module (mod)
   "Jump to the source file of the Prolog module MOD."
   (interactive (list (sweep-read-module-name)))