From b1aed042d7cd27bd8705d5524ec0c0a930d546fd Mon Sep 17 00:00:00 2001 From: Fernando da Silva Sousa Date: Thu, 22 Oct 2020 14:44:33 -0300 Subject: Adds backtrace for illegal arguments supplied --- lib/mysql.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/mysql.php b/lib/mysql.php index a0ebb5a..6473868 100644 --- a/lib/mysql.php +++ b/lib/mysql.php @@ -734,16 +734,24 @@ namespace Dshafik { public static function checkValidResult($result, $function) { if (!($result instanceof \mysqli_result)) { + $type = strtolower(gettype($result)); + $file = ""; + $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); + if (isset($backtrace[1], $backtrace[1]['file'], $backtrace[1]['line'])) { + $caller = $backtrace[1]; + $file = $caller['file'] . ':' . $caller['line']; + } + if ($function !== 'mysql_fetch_object') { trigger_error( - $function . '() expects parameter 1 to be resource, ' . strtolower(gettype($result)) . ' given', + "$function() expects parameter 1 to be resource, $type given on $file", E_USER_WARNING ); } if ($function === 'mysql_fetch_object') { trigger_error( - $function . '(): supplied argument is not a valid MySQL result resource', + "$function(): supplied argument is not a valid MySQL result resource on $file", E_USER_WARNING ); } -- cgit v1.1 From 645b30664769c2ad4478a43d0849ffff9669c892 Mon Sep 17 00:00:00 2001 From: Fernando da Silva Sousa Date: Fri, 23 Oct 2020 18:20:42 -0300 Subject: Fix possibility of empty backtrace caused by traces without file and line --- lib/mysql.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/mysql.php b/lib/mysql.php index 6473868..1ebc5e5 100644 --- a/lib/mysql.php +++ b/lib/mysql.php @@ -737,10 +737,20 @@ namespace Dshafik { $type = strtolower(gettype($result)); $file = ""; $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); - if (isset($backtrace[1], $backtrace[1]['file'], $backtrace[1]['line'])) { - $caller = $backtrace[1]; - $file = $caller['file'] . ':' . $caller['line']; - } + $backtraceIndex = 0; + + /** + * Iterate through backtrace until find an backtrace with origin + * Some methods may not leave file and line metadata like call_user_func_array and __call + */ + do { + $currentBacktrace = $backtrace[$backtraceIndex]; + $callerHasFileAndLine = isset($currentBacktrace['file'], $currentBacktrace['line']); + + if ($callerHasFileAndLine && $currentBacktrace['file'] != __FILE__) { + $file = $currentBacktrace['file'] . ':' . $currentBacktrace['line']; + } + } while ($backtraceIndex++ < count($backtrace) && $file == ""); if ($function !== 'mysql_fetch_object') { trigger_error( -- cgit v1.1 From fd7dc379295f8f1abf0db870528502bca147412d Mon Sep 17 00:00:00 2001 From: Fernando da Silva Sousa Date: Fri, 23 Oct 2020 18:21:29 -0300 Subject: Add asserts about invalid results backtracing correctly --- tests/MySqlShimTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/MySqlShimTest.php b/tests/MySqlShimTest.php index ea9b2c3..97d7599 100644 --- a/tests/MySqlShimTest.php +++ b/tests/MySqlShimTest.php @@ -688,8 +688,10 @@ class MySqlShimTest extends \PHPUnit\Framework\TestCase if ($args !== array()) { array_unshift($args, null); + $this->expectWarningMessageMatches('@' . __FILE__ . ':' . (__LINE__ + 1) . '@'); call_user_func_array($function, $args); } + $this->expectWarningMessageMatches('@' . __FILE__ . ':' . (__LINE__ + 1) . '@'); call_user_func($function, null); } -- cgit v1.1 From 8d07253156c93e36a807f6247de5a18b72045603 Mon Sep 17 00:00:00 2001 From: Fernando da Silva Sousa Date: Fri, 23 Oct 2020 18:22:03 -0300 Subject: Test check_valid_result function backtrace individually --- tests/MySqlShimTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/MySqlShimTest.php b/tests/MySqlShimTest.php index 97d7599..1a63aad 100644 --- a/tests/MySqlShimTest.php +++ b/tests/MySqlShimTest.php @@ -695,6 +695,13 @@ class MySqlShimTest extends \PHPUnit\Framework\TestCase call_user_func($function, null); } + public function test_mysql_check_valid_result_backtraces_here() + { + $this->expectWarning(); + $this->expectWarningMessage(__FUNCTION__ . "() expects parameter 1 to be resource, boolean given on " . __FILE__ . ':' . (__LINE__ + 1)); + \Dshafik\MySQL::checkValidResult(false, __FUNCTION__); + } + /** * @dataProvider mysql_fetch_DataProvider */ -- cgit v1.1 From 709444274c9e87383c3728ee347607e4ae6c7906 Mon Sep 17 00:00:00 2001 From: Fernando da Silva Sousa Date: Fri, 23 Oct 2020 18:25:46 -0300 Subject: Fix grammar on comment --- lib/mysql.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mysql.php b/lib/mysql.php index 1ebc5e5..d05d7a8 100644 --- a/lib/mysql.php +++ b/lib/mysql.php @@ -740,7 +740,7 @@ namespace Dshafik { $backtraceIndex = 0; /** - * Iterate through backtrace until find an backtrace with origin + * Iterate through backtrace until finding a backtrace with an origin * Some methods may not leave file and line metadata like call_user_func_array and __call */ do { -- cgit v1.1