feature/igc3 57548d73542 2/4: Avoid arithmetic overflow in igc_make_vector

Helmut Eller <[email protected]> Tue, 28 Jul 2026 17:02:05 -0400 (EDT)
Newsgroups gmane.emacs.diffs
Message-ID <[email protected]>
branch: feature/igc3
commit 57548d7354271ee6b640c57a097e3ca617a177d6
Author: Helmut Eller <[email protected]>
Commit: Helmut Eller <[email protected]>

    Avoid arithmetic overflow in igc_make_vector
    
    * src/igc.c (igc_make_vector): Check the 'len' argument before using it
    in unchecked arithmentic.
    (igc_vector_elts_max): New helper.
    * test/src/alloc-tests.el (alloc-tests-large-vector): New test.
---
 src/igc.c               | 11 +++++++++++
 test/src/alloc-tests.el |  5 ++++-
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/src/igc.c b/src/igc.c
index 6b1936e6be6..54c44a9462f 100644
--- a/src/igc.c
+++ b/src/igc.c
@@ -5274,9 +5274,20 @@ igc_make_pseudovector (size_t nwords_mem, size_t nwords_lisp,
   return v;
 }
 
+static size_t
+igc_vector_elts_max (void)
+{
+  size_t nwords_max = ~(~(uint64_t) 0 << IGC_HEADER_NWORDS_BITS);
+  size_t obj_size_max = to_bytes (nwords_max);
+  return (obj_size_max - header_size) / sizeof (Lisp_Object);
+};
+
 struct Lisp_Vector *
 igc_make_vector (ptrdiff_t len)
 {
+  if (len < 0 || (size_t) len > igc_vector_elts_max ())
+    xsignal1 (Qargs_out_of_range, make_int (len));
+
   struct Lisp_Vector *v
     = alloc (header_size + len * word_size, IGC_OBJ_VECTOR);
   v->header.size = len;
diff --git a/test/src/alloc-tests.el b/test/src/alloc-tests.el
index 022cc174d8c..b5ad4ba39ff 100644
--- a/test/src/alloc-tests.el
+++ b/test/src/alloc-tests.el
@@ -52,4 +52,7 @@
     (dotimes (i 4)
       (should (eql (aref x i) (aref y i))))))
 
-;;; alloc-tests.el ends here
+(ert-deftest alloc-tests-large-vector ()
+  (should-error (make-vector most-positive-fixnum nil)))
+
+;;; alloc-tests.el ends here1