From c7fd9efc846bfec5cbe381376ab9ca0fa201f261 Mon Sep 17 00:00:00 2001 From: Hitesh Ahuja Date: Fri, 9 Feb 2024 17:10:27 -0800 Subject: [PATCH] chore(agent): ensure zend_eval_string is called only when class or variable exists --- agent/fw_drupal8.c | 43 ++++++++++++++++++++++++++++++---------- agent/fw_wordpress.c | 43 ++++++++++++++++++++++++++++++---------- agent/lib_phpunit.c | 47 ++++++++++++++++++++++++++++++++++---------- 3 files changed, 103 insertions(+), 30 deletions(-) diff --git a/agent/fw_drupal8.c b/agent/fw_drupal8.c index 52f550868..6d08e520d 100644 --- a/agent/fw_drupal8.c +++ b/agent/fw_drupal8.c @@ -534,18 +534,41 @@ NR_PHP_WRAPPER(nr_drupal8_name_the_wt_via_symfony) { NR_PHP_WRAPPER_END void nr_drupal_version() { - char* string = "Drupal::VERSION;"; + char* func_string + = "if (!function_exists('newrelic_drupal_get_version')) {" + " function newrelic_drupal_get_version() {" + " try {" + " if (class_exists('Drupal')) {" + " return Drupal::VERSION;" + " }" + " else {" + " return ' ';" + " }" + " } catch (Exception $e) {" + " }" + " }" + "}"; + zval retval; - int result - = zend_eval_string(string, &retval, "Retrieve Drupal Version"); - - // Add php package to transaction - if (result == SUCCESS) { - if (Z_TYPE(retval) == IS_STRING) { - char* version = Z_STRVAL(retval); - nr_txn_add_php_package(NRPRG(txn), "drupal/core", version); + int result = zend_eval_string(func_string, NULL, + "Define Drupal Get Version Function"); + if (SUCCESS != result) { + return; + } + + zend_function* func = nr_php_find_function("newrelic_drupal_get_version"); + + if (func) { + result = zend_eval_string("newrelic_drupal_get_version();", &retval, + "Get Drupal Version"); + // Add php package to transaction + if (SUCCESS == result) { + if (IS_STRING == Z_TYPE(retval)) { + char* version = Z_STRVAL(retval); + nr_txn_add_php_package(NRPRG(txn), "drupal/core", version); + } + zval_dtor(&retval); } - zval_dtor(&retval); } } diff --git a/agent/fw_wordpress.c b/agent/fw_wordpress.c index 675360a49..142d555e0 100644 --- a/agent/fw_wordpress.c +++ b/agent/fw_wordpress.c @@ -658,18 +658,41 @@ NR_PHP_WRAPPER_END #endif /* PHP 7.4+ */ void nr_wordpress_version() { - char* string = "$GLOBALS['wp_version'];"; + char* func_string + = "if (!function_exists('newrelic_wordpress_get_version')) {" + " function newrelic_wordpress_get_version() {" + " try {" + " if (isset($GLOBALS['wp_version'])) {" + " return $GLOBALS['wp_version'];" + " }" + " else {" + " return ' ';" + " }" + " } catch (Exception $e) {" + " }" + " }" + "}"; + zval retval; - int result = zend_eval_string(string, &retval, - "Retrieve Wordpress Version"); - - // Add php package to transaction - if (result == SUCCESS) { - if (Z_TYPE(retval) == IS_STRING) { - char* version = Z_STRVAL(retval); - nr_txn_add_php_package(NRPRG(txn), "wordpress", version); + int result = zend_eval_string(func_string, NULL, + "Define Wordpress Get Version Function"); + if (SUCCESS != result) { + return; + } + + zend_function* func = nr_php_find_function("newrelic_wordpress_get_version"); + + if (func) { + result = zend_eval_string("newrelic_wordpress_get_version();", &retval, + "Get Wordpress Version"); + // Add php package to transaction + if (SUCCESS == result) { + if (IS_STRING == Z_TYPE(retval)) { + char* version = Z_STRVAL(retval); + nr_txn_add_php_package(NRPRG(txn), "wordpress", version); + } + zval_dtor(&retval); } - zval_dtor(&retval); } } diff --git a/agent/lib_phpunit.c b/agent/lib_phpunit.c index 66702c5e4..55bbc9767 100644 --- a/agent/lib_phpunit.c +++ b/agent/lib_phpunit.c @@ -667,18 +667,45 @@ static int nr_phpunit_are_statuses_valid(TSRMLS_D) { } void nr_phpunit_version() { - char* string = "PHPUnit\\Runner\\Version::id();"; + char* func_string + = "" + "if (!function_exists('newrelic_phpunit_get_version')) {" + " function newrelic_phpunit_get_version() {" + " try {" + " if (class_exists('PHPUnit\\Runner\\Version')) {" + " return PHPUnit\\Runner\\Versions::id();" + " }" + " else if (class_exists('PHPUnit_Runner_Version')) {" + " return PHPUnit_Runner_Versions::id();" + " }" + " else {" + " return ' ';" + " }" + " } catch (Exception $e) {" + " }" + " }" + "}"; + zval retval; - int result - = zend_eval_string(string, &retval, "Retrieve PHPUnit Version"); - - // Add php package to transaction - if (result == SUCCESS) { - if (Z_TYPE(retval) == IS_STRING) { - char* version = Z_STRVAL(retval); - nr_txn_add_php_package(NRPRG(txn), "phpunit/phpunit", version); + int result = zend_eval_string(func_string, NULL, + "Define PHPUnit Get Version Function"); + if (SUCCESS != result) { + return; + } + + zend_function* func = nr_php_find_function("newrelic_phpunit_get_version"); + + if (func) { + result = zend_eval_string("newrelic_phpunit_get_version();", &retval, + "Get PHPUnit Version"); + // Add php package to transaction + if (SUCCESS == result) { + if (IS_STRING == Z_TYPE(retval)) { + char* version = Z_STRVAL(retval); + nr_txn_add_php_package(NRPRG(txn), "phpunit/phpunit", version); + } + zval_dtor(&retval); } - zval_dtor(&retval); } }