From de086efd414b809e40e269ac9d93bfe0c56cfb60 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Fri, 3 Mar 2023 08:46:00 +0000 Subject: [PATCH] MDL-77462 core_question: avoid passing null to preg_match --- lib/questionlib.php | 2 +- lib/tests/questionlib_test.php | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/questionlib.php b/lib/questionlib.php index d78a84da98e..65e88c94f7a 100644 --- a/lib/questionlib.php +++ b/lib/questionlib.php @@ -1982,7 +1982,7 @@ function core_question_find_next_unused_idnumber(?string $oldidnumber, int $cate global $DB; // The the old idnumber is not of the right form, bail now. - if (!preg_match('~\d+$~', $oldidnumber, $matches)) { + if ($oldidnumber === null || !preg_match('~\d+$~', $oldidnumber, $matches)) { return null; } diff --git a/lib/tests/questionlib_test.php b/lib/tests/questionlib_test.php index 53c3f17849d..1b93d96d59d 100644 --- a/lib/tests/questionlib_test.php +++ b/lib/tests/questionlib_test.php @@ -1998,6 +1998,7 @@ class questionlib_test extends \advanced_testcase { */ public function find_next_unused_idnumber_cases(): array { return [ + [null, null], ['id', null], ['id1a', null], ['id001', 'id002'], @@ -2020,10 +2021,10 @@ class questionlib_test extends \advanced_testcase { * Test core_question_find_next_unused_idnumber in the case when there are no other questions. * * @dataProvider find_next_unused_idnumber_cases - * @param string $oldidnumber value to pass to core_question_find_next_unused_idnumber. + * @param string|null $oldidnumber value to pass to core_question_find_next_unused_idnumber. * @param string|null $expectednewidnumber expected result. */ - public function test_core_question_find_next_unused_idnumber(string $oldidnumber, ?string $expectednewidnumber) { + public function test_core_question_find_next_unused_idnumber(?string $oldidnumber, ?string $expectednewidnumber) { $this->assertSame($expectednewidnumber, core_question_find_next_unused_idnumber($oldidnumber, 0)); }