Avoid a crash when attempting to allocate very large matrices.
authorJohn Darrington <john@cellform.com>
Sun, 9 Jun 2024 10:16:38 +0000 (12:16 +0200)
committerJohn Darrington <john@cellform.com>
Sun, 9 Jun 2024 10:21:35 +0000 (12:21 +0200)
Partial fix for bug #65545

src/language/commands/matrix.c
tests/language/commands/matrix.at

index ce93258fb4bd5213bc40894c814aa56312f191c4..acbef18df06dcb3c75d0fa39db891e07de08ae5d 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2021 Free Software Foundation, Inc.
+   Copyright (C) 2021, 2024 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -3501,6 +3501,13 @@ matrix_expr_evaluate_seq (const struct matrix_expr *e,
                 : end <= start && by < 0 ? (start - end - by) / -by
                 : 0);
   gsl_matrix *m = gsl_matrix_alloc (1, n);
+  if (m == NULL)
+    {
+      msg_at (SE, matrix_expr_location (e),
+              _("Matrix cannot be allocated.  Too large?"));
+      return NULL;
+    }
+
   for (long int i = 0; i < n; i++)
     gsl_matrix_set (m, 0, i, start + i * by);
   return m;
index d3cf0e34afb1b1876f1aa8a811f1509162c50ec8..219394af70862966880682a9896f6b15af2cc367 100644 (file)
@@ -5126,4 +5126,21 @@ matrix.sps:7.11: error: RELEASE: Syntax error expecting end of command.
     7 | RELEASE x y.
       |           ^
 ])
-AT_CLEANUP
\ No newline at end of file
+AT_CLEANUP
+
+
+dnl Check that attempting to allocate huge matrices fails gracefully
+AT_SETUP([MATRIX - very large matrices])
+AT_DATA([matrix.sps], [dnl
+MATRIX.
+PRINT 2:230000000000.
+END MATRIX.
+])
+
+AT_CHECK([pspp matrix.sps], [1], [dnl
+matrix.sps:2.7-2.20: error: MATRIX: Matrix cannot be allocated.  Too large?
+    2 | PRINT 2:230000000000.
+      |       ^~~~~~~~~~~~~~
+])
+
+AT_CLEANUP