From 96a1a07fb1f9d8f3f41f3893ed1b624246a76c43 Mon Sep 17 00:00:00 2001 From: Philipp Stephani Date: Tue, 25 Mar 2025 02:50:37 +0100 Subject: [PATCH] Don't use 'args-out-of-range' error for too-small buffers. '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. --- src/emacs-module.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/emacs-module.c b/src/emacs-module.c index ab6b900df8d..a8386856da7 100644 --- a/src/emacs-module.c +++ b/src/emacs-module.c @@ -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)); +} + /* 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");