Skip to content

Commit 1f92ebe

Browse files
committed
fix memory management issues
1 parent d6a1277 commit 1f92ebe

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

agent/php_execute.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,32 +1042,35 @@ static void nr_get_composer_package_information() {
10421042
}
10431043
}
10441044

1045-
static bool nr_execute_autoload_is_composer(const char* filename, const size_t filename_len) {
1045+
static bool nr_execute_autoload_is_composer(const char* filename) {
10461046
#define COMPOSER_MAGIC_FILE "composer/autoload_real.php"
1047+
#define COMPOSER_MAGIC_FILE_LEN (sizeof(COMPOSER_MAGIC_FILE) - 1)
10471048
char* vendor_path = NULL; // result of dirname(filename)
10481049
char* composer_magic_file = NULL; // vendor_path + COMPOSER_MAGIC_FILE
10491050
char* cp = NULL;
10501051
bool composer_detected = false;
10511052

10521053
// vendor_path = dirname(filename):
1053-
// 1. allocate space on stack
1054-
vendor_path = nr_alloca(filename_len+1);
1055-
// 2. copy filename to vendor_path
1056-
nr_strcpy(vendor_path, filename);
1057-
// 3. // find last occurence of '/' in vendor_path
1054+
// 1. copy filename to vendor_path
1055+
vendor_path = nr_strdup(filename);
1056+
// 2. // find last occurence of '/' in vendor_path
10581057
cp = nr_strrchr(vendor_path, '/');
1059-
// 4. replace '/' with '\0' to get the directory path
1058+
// 3. replace '/' with '\0' to get the directory path
10601059
*cp = '\0';
10611060

10621061
// composer_magic_file = vendor_path + "/composer/autoload_real.php"
1063-
composer_magic_file = nr_str_append(vendor_path, COMPOSER_MAGIC_FILE, "/");
1062+
cp = composer_magic_file = nr_malloc(nr_strlen(vendor_path) + COMPOSER_MAGIC_FILE_LEN + 2);
1063+
cp = nr_strcpy(cp, vendor_path);
1064+
*cp++ = '/';
1065+
cp = nr_strcpy(cp, COMPOSER_MAGIC_FILE);
10641066

10651067
if (0 == nr_access(composer_magic_file, F_OK | R_OK)) {
10661068
nrl_debug(NRL_INSTRUMENT, "detected composer with %s", composer_magic_file);
10671069
composer_detected = true;
10681070
nr_fw_support_add_library_supportability_metric(NRPRG(txn), "Composer");
10691071
}
10701072

1073+
nr_free(vendor_path);
10711074
nr_free(composer_magic_file);
10721075
return composer_detected;
10731076
}
@@ -1085,7 +1088,7 @@ static void nr_execute_handle_autoload(const char* filename, const size_t filena
10851088
NRPRG(txn)->composer_info.autoload_detected = true;
10861089
nr_fw_support_add_library_supportability_metric(NRPRG(txn), "Autoloader");
10871090

1088-
NRPRG(txn)->composer_info.composer_detected = nr_execute_autoload_is_composer(filename, filename_len);
1091+
NRPRG(txn)->composer_info.composer_detected = nr_execute_autoload_is_composer(filename);
10891092
}
10901093
}
10911094

0 commit comments

Comments
 (0)