1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-15 10:30:25 -08:00

Document and enforce some properties for strings created by modules.

When creating multibyte or unibyte strings, we should guarantee the
following invariants:

- When creating empty strings, a NULL data pointer should be allowed.
  This often arises in practice if the string length isn't known in
  advance, and we don't want to unnecessarily trigger undefined
  behavior.  Since functions like memcpy might not accept NULL
  pointers, use the canonical empty string objects in this case.

- Nonzero strings should be guaranteed to be unique and mutable.
  These are the same guarantees expected from Lisp functions such as
  'make-string' or 'unibyte-string'.  On the other hand, empty strings
  might not be unique.

* src/emacs-module.c (module_make_string)
(module_make_unibyte_string): Correctly handle empty strings.

* test/src/emacs-module-resources/mod-test.c (Fmod_test_make_string):
New test function.
(emacs_module_init): Expose it.

* test/src/emacs-module-tests.el (mod-test-make-string/empty)
(mod-test-make-string/nonempty): New unit tests.

* doc/lispref/internals.texi (Module Values): Document properties and
corner cases for strings.
This commit is contained in:
Philipp Stephani 2020-12-12 23:21:18 +01:00
parent 4bf98aecff
commit 52e3ac6303
4 changed files with 58 additions and 5 deletions

View file

@ -1864,7 +1864,10 @@ byte, is @var{len}. The original string in @var{str} can be either an
it can include embedded null bytes, and doesn't have to end in a
terminating null byte at @code{@var{str}[@var{len}]}. The function
raises the @code{overflow-error} error condition if @var{len} is
negative or exceeds the maximum length of an Emacs string.
negative or exceeds the maximum length of an Emacs string. If
@var{len} is zero, then @var{str} can be @code{NULL}, otherwise it
must point to valid memory. For nonzero @var{len}, @code{make_string}
returns unique mutable string objects.
@end deftypefn
@deftypefn Function emacs_value make_unibyte_string (emacs_env *@var{env}, const char *@var{str}, ptrdiff_t @var{len})