emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master 3252f31 1/2: Use decode_string_utf_8 in emacs-module.c.


From: Philipp Stephani
Subject: master 3252f31 1/2: Use decode_string_utf_8 in emacs-module.c.
Date: Sun, 12 Jan 2020 18:25:22 -0500 (EST)

branch: master
commit 3252f3149673bbd6919d5d9a5a672e2f3730741d
Author: Philipp Stephani <address@hidden>
Commit: Philipp Stephani <address@hidden>

    Use decode_string_utf_8 in emacs-module.c.
    
    Now that decode_string_utf_8 is available, we can use it to signal
    errors on invalid input.
    
    * src/coding.c (syms_of_coding): Move Qutf_8_string_p from json.c
    since it’s now used outside json.c.
    
    * src/emacs-module.c (module_decode_utf_8): New helper function.
    (module_make_function, module_copy_string_contents): Use it.
---
 src/coding.c       |  2 ++
 src/emacs-module.c | 24 ++++++++++++++++++++++--
 src/json.c         |  1 -
 3 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/src/coding.c b/src/coding.c
index ed755b1..8b54281 100644
--- a/src/coding.c
+++ b/src/coding.c
@@ -11745,6 +11745,8 @@ syms_of_coding (void)
 
   DEFSYM (Qignored, "ignored");
 
+  DEFSYM (Qutf_8_string_p, "utf-8-string-p");
+
   defsubr (&Scoding_system_p);
   defsubr (&Sread_coding_system);
   defsubr (&Sread_non_nil_coding_system);
diff --git a/src/emacs-module.c b/src/emacs-module.c
index f40ca93..60f1641 100644
--- a/src/emacs-module.c
+++ b/src/emacs-module.c
@@ -213,6 +213,25 @@ static bool value_storage_contains_p (const struct 
emacs_value_storage *,
 
 static bool module_assertions = false;
 
+
+/* Small helper functions.  */
+
+/* Interprets the string at STR with length LEN as UTF-8 string.
+   Signals an error if it's not a valid UTF-8 string.  */
+
+static Lisp_Object
+module_decode_utf_8 (const char *str, ptrdiff_t len)
+{
+  /* We set HANDLE-8-BIT and HANDLE-OVER-UNI to nil to signal an error
+     if the argument is not a valid UTF-8 string.  While it isn't
+     documented how make_string and make_function behave in this case,
+     signaling an error is the most defensive and obvious reaction. */
+  Lisp_Object s = decode_string_utf_8 (Qnil, str, len, Qnil, false, Qnil, 
Qnil);
+  CHECK_TYPE (!NILP (s), Qutf_8_string_p, make_string_from_utf8 (str, len));
+  return s;
+}
+
+
 /* Convenience macros for non-local exit handling.  */
 
 /* FIXME: The following implementation for non-local exit handling
@@ -521,7 +540,8 @@ module_make_function (emacs_env *env, ptrdiff_t min_arity, 
ptrdiff_t max_arity,
   function->finalizer = NULL;
 
   if (docstring)
-    function->documentation = build_string_from_utf8 (docstring);
+    function->documentation
+      = module_decode_utf_8 (docstring, strlen (docstring));
 
   Lisp_Object result;
   XSET_MODULE_FUNCTION (result, function);
@@ -694,7 +714,7 @@ module_make_string (emacs_env *env, const char *str, 
ptrdiff_t len)
   MODULE_FUNCTION_BEGIN (NULL);
   if (! (0 <= len && len <= STRING_BYTES_BOUND))
     overflow_error ();
-  Lisp_Object lstr = make_string_from_utf8 (str, len);
+  Lisp_Object lstr = module_decode_utf_8 (str, len);
   return lisp_to_value (env, lstr);
 }
 
diff --git a/src/json.c b/src/json.c
index 2e50ce5..3002767 100644
--- a/src/json.c
+++ b/src/json.c
@@ -1121,7 +1121,6 @@ syms_of_json (void)
 
   DEFSYM (Qstring_without_embedded_nulls_p, "string-without-embedded-nulls-p");
   DEFSYM (Qjson_value_p, "json-value-p");
-  DEFSYM (Qutf_8_string_p, "utf-8-string-p");
 
   DEFSYM (Qjson_error, "json-error");
   DEFSYM (Qjson_out_of_memory, "json-out-of-memory");



reply via email to

[Prev in Thread] Current Thread [Next in Thread]