From: John Darrington Date: Sun, 9 Jun 2024 10:16:38 +0000 (+0200) Subject: Avoid a crash when attempting to allocate very large matrices. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b5b48a5bb17704e432d5f75bbb2c0ffad72aef8e;p=pspp Avoid a crash when attempting to allocate very large matrices. Partial fix for bug #65545 --- diff --git a/src/language/commands/matrix.c b/src/language/commands/matrix.c index ce93258fb4..acbef18df0 100644 --- a/src/language/commands/matrix.c +++ b/src/language/commands/matrix.c @@ -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; diff --git a/tests/language/commands/matrix.at b/tests/language/commands/matrix.at index d3cf0e34af..219394af70 100644 --- a/tests/language/commands/matrix.at +++ b/tests/language/commands/matrix.at @@ -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