From e70e73d9b76d0530d406621156378eb4419dcc50 Mon Sep 17 00:00:00 2001 From: Huong Nguyen Date: Fri, 15 Mar 2024 11:07:51 +0700 Subject: [PATCH] MDL-81246 dml: Check the statement resource before freeing it --- lib/dml/sqlsrv_native_moodle_database.php | 4 +++- lib/dml/sqlsrv_native_moodle_recordset.php | 10 ++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/dml/sqlsrv_native_moodle_database.php b/lib/dml/sqlsrv_native_moodle_database.php index 9afc56b9e8c..a75548eb317 100644 --- a/lib/dml/sqlsrv_native_moodle_database.php +++ b/lib/dml/sqlsrv_native_moodle_database.php @@ -680,9 +680,11 @@ class sqlsrv_native_moodle_database extends moodle_database { * @return bool */ private function free_result($resource) { - if (!is_bool($resource)) { // true/false resources cannot be freed + if (!is_bool($resource) && is_resource($resource)) { + // We need to make sure that the statement resource is in the correct type before freeing it. return sqlsrv_free_stmt($resource); } + return false; } /** diff --git a/lib/dml/sqlsrv_native_moodle_recordset.php b/lib/dml/sqlsrv_native_moodle_recordset.php index 9653a87bec4..aa4a646e1b9 100644 --- a/lib/dml/sqlsrv_native_moodle_recordset.php +++ b/lib/dml/sqlsrv_native_moodle_recordset.php @@ -85,7 +85,10 @@ class sqlsrv_native_moodle_recordset extends moodle_recordset { return false; } if (!$row = sqlsrv_fetch_array($this->rsrc, SQLSRV_FETCH_ASSOC)) { - sqlsrv_free_stmt($this->rsrc); + if (is_resource($this->rsrc)) { + // We need to make sure that the statement resource is in the correct type before freeing it. + sqlsrv_free_stmt($this->rsrc); + } $this->rsrc = null; $this->unregister(); return false; @@ -133,7 +136,10 @@ class sqlsrv_native_moodle_recordset extends moodle_recordset { public function close() { if ($this->rsrc) { - sqlsrv_free_stmt($this->rsrc); + if (is_resource($this->rsrc)) { + // We need to make sure that the statement resource is in the correct type before freeing it. + sqlsrv_free_stmt($this->rsrc); + } $this->rsrc = null; } $this->current = null;