]> git.eshelyaron.com Git - emacs.git/commitdiff
Don't use 'args-out-of-range' error for too-small buffers.
authorPhilipp Stephani <p.stephani2@gmail.com>
Tue, 25 Mar 2025 01:50:37 +0000 (02:50 +0100)
committerEshel Yaron <me@eshelyaron.com>
Tue, 25 Mar 2025 18:19:47 +0000 (19:19 +0100)
'args-out-of-range' means that some index argument isn't valid for a
given sequence/range, which isn't the case here.  Instead, define a new
error symbol to mean "user-supplied buffer is too small."  Since we
never specified nor tested which error symbol was signalled in this
case, changing it shouldn't cause severe breakages.

* src/emacs-module.c (module_buffer_too_small): New helper function.
(module_copy_string_contents, module_extract_big_integer): Use it.
(syms_of_module): Define 'buffer-too-small' error symbol.

(cherry picked from commit 96a1a07fb1f9d8f3f41f3893ed1b624246a76c43)

src/emacs-module.c

index ab6b900df8d4a70517931dc3b99227c259d79ddf..a8386856da7dfd2e4fa73304b191e7453d7cf9e5 100644 (file)
@@ -224,6 +224,14 @@ module_decode_utf_8 (const char *str, ptrdiff_t len)
   return s;
 }
 
+/* Signal an error of type `buffer-too-small'.  */
+static void
+module_buffer_too_small (ptrdiff_t actual, ptrdiff_t required)
+{
+  xsignal2 (Qbuffer_too_small, INT_TO_INTEGER (actual),
+           INT_TO_INTEGER (required));
+}
+
 \f
 /* Convenience macros for non-local exit handling.  */
 
@@ -817,9 +825,7 @@ module_copy_string_contents (emacs_env *env, emacs_value value, char *buf,
     {
       ptrdiff_t actual = *len;
       *len = required_buf_size;
-      args_out_of_range_3 (INT_TO_INTEGER (actual),
-                           INT_TO_INTEGER (required_buf_size),
-                           INT_TO_INTEGER (PTRDIFF_MAX));
+      module_buffer_too_small (actual, required_buf_size);
     }
 
   *len = required_buf_size;
@@ -1108,10 +1114,8 @@ module_extract_big_integer (emacs_env *env, emacs_value arg, int *sign,
         {
           ptrdiff_t actual = *count;
           *count = required;
-          args_out_of_range_3 (INT_TO_INTEGER (actual),
-                               INT_TO_INTEGER (required),
-                               INT_TO_INTEGER (module_bignum_count_max));
-        }
+         module_buffer_too_small (actual, required);
+       }
       /* Set u = abs(x).  See https://stackoverflow.com/a/17313717. */
       if (0 < x)
         u = (EMACS_UINT) x;
@@ -1144,8 +1148,7 @@ module_extract_big_integer (emacs_env *env, emacs_value arg, int *sign,
     {
       ptrdiff_t actual = *count;
       *count = required;
-      args_out_of_range_3 (INT_TO_INTEGER (actual), INT_TO_INTEGER (required),
-                           INT_TO_INTEGER (module_bignum_count_max));
+      module_buffer_too_small (actual, required);
     }
   size_t written;
   mpz_export (magnitude, &written, order, size, endian, nails, *x);
@@ -1771,6 +1774,12 @@ syms_of_module (void)
   Fput (Qinvalid_arity, Qerror_message,
         build_string ("Invalid function arity"));
 
+  DEFSYM (Qbuffer_too_small, "buffer-too-small");
+  Fput (Qbuffer_too_small, Qerror_conditions,
+       list2 (Qbuffer_too_small, Qerror));
+  Fput (Qbuffer_too_small, Qerror_message,
+        build_unibyte_string ("Memory buffer too small"));
+
   DEFSYM (Qmodule_function_p, "module-function-p");
   DEFSYM (Qunicode_string_p, "unicode-string-p");