Skip to content

Commit e5864fb

Browse files
committed
fixup! refactor package version metric creation
Fix memory access error - if package with the same name already exists in txn->php_packages, nr_php_packages_add_package will return a pointer to that package and not the one passed in.
1 parent 7106bad commit e5864fb

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

axiom/nr_php_packages.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,15 @@ nr_php_packages_t* nr_php_packages_create() {
8888
return h;
8989
}
9090

91-
void nr_php_packages_add_package(nr_php_packages_t* h, nr_php_package_t* p) {
91+
nr_php_package_t* nr_php_packages_add_package(nr_php_packages_t* h,
92+
nr_php_package_t* p) {
9293
nr_php_package_t* package;
9394
if (NULL == h) {
94-
return;
95+
return NULL;
9596
}
9697

9798
if (NULL == p || NULL == p->package_name || NULL == p->package_version) {
98-
return;
99+
return NULL;
99100
}
100101

101102
// If package with the same key already exists, we will check if the value is
@@ -108,10 +109,11 @@ void nr_php_packages_add_package(nr_php_packages_t* h, nr_php_package_t* p) {
108109
package->package_version = nr_strdup(p->package_version);
109110
}
110111
nr_php_package_destroy(p);
111-
return;
112+
return package;
112113
}
113114

114115
nr_hashmap_set(h->data, p->package_name, nr_strlen(p->package_name), p);
116+
return p;
115117
}
116118

117119
char* nr_php_package_to_json(nr_php_package_t* package) {

axiom/nr_php_packages.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ extern nr_php_packages_t* nr_php_packages_create(void);
8787
* 2. A pointer to the php package that needs to be added to the
8888
* collection
8989
*
90-
* Returns : Nothing
90+
* Returns : pointer to added package on success or NULL otherwise.
9191
*/
92-
extern void nr_php_packages_add_package(nr_php_packages_t* h,
93-
nr_php_package_t* p);
92+
extern nr_php_package_t* nr_php_packages_add_package(nr_php_packages_t* h,
93+
nr_php_package_t* p);
9494

9595
/*
9696
* Purpose : Destroy/free the collection

axiom/nr_txn.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3510,8 +3510,7 @@ nr_php_package_t* nr_txn_add_php_package_from_source(
35103510
}
35113511

35123512
p = nr_php_package_create_with_source(package_name, package_version, source);
3513-
nr_php_packages_add_package(txn->php_packages, p);
3514-
return p;
3513+
return nr_php_packages_add_package(txn->php_packages, p);
35153514
}
35163515

35173516
nr_php_package_t* nr_txn_add_php_package(nrtxn_t* txn,

axiom/tests/test_txn.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8597,6 +8597,8 @@ static void test_nr_txn_add_php_package(void) {
85978597
char* package_name4 = "Wordpress";
85988598
char* package_version4 = PHP_PACKAGE_VERSION_UNKNOWN;
85998599
nrtxn_t* txn = new_txn(0);
8600+
nr_php_package_t* p1 = NULL;
8601+
nr_php_package_t* p2 = NULL;
86008602

86018603
/*
86028604
* NULL parameters: ensure it does not crash
@@ -8621,6 +8623,13 @@ static void test_nr_txn_add_php_package(void) {
86218623

86228624
nr_free(json);
86238625
nr_txn_destroy(&txn);
8626+
8627+
txn = new_txn(0);
8628+
p1 = nr_txn_add_php_package(txn, package_name1, package_version1);
8629+
p2 = nr_txn_add_php_package(txn, package_name1, package_version2);
8630+
tlib_pass_if_ptr_equal(
8631+
"same package name, different version, add returns same pointer", p1, p2);
8632+
nr_txn_destroy(&txn);
86248633
}
86258634

86268635
tlib_parallel_info_t parallel_info

0 commit comments

Comments
 (0)