Skip to content

Commit 830445b

Browse files
committed
ensure passing NULL for is_new_wraprec doesn't blow up
1 parent 1d48125 commit 830445b

File tree

1 file changed

+78
-62
lines changed

1 file changed

+78
-62
lines changed

agent/tests/test_user_instrument_wraprec_hashmap.c

Lines changed: 78 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -23,73 +23,89 @@ static void test_wraprecs_hashmap() {
2323
nruserfn_t *wraprec, *found_wraprec;
2424
zend_string *func_name, *scope_name, *method_name;
2525
bool is_new_wraprec = false;
26+
bool *is_new_wraprec_tests[] = {NULL, &is_new_wraprec};
2627

2728
func_name = zend_string_init(NR_PSTR(FUNCTION_NAME), 0);
2829
scope_name = zend_string_init(NR_PSTR(SCOPE_NAME), 0);
2930
method_name = zend_string_init(NR_PSTR(METHOD_NAME), 0);
3031

31-
// user_instrument_wraprec_hashmap is initialized at minit
32-
// destroy it to test agent's behavior when it is not initialized
33-
nr_php_user_instrument_wraprec_hashmap_destroy();
34-
35-
// Test valid operations before initializing the hashmap
36-
wraprec = nr_php_user_instrument_wraprec_hashmap_add(NR_PSTR(FUNCTION_NAME),
37-
&is_new_wraprec);
38-
tlib_pass_if_null("adding valid function before init", wraprec);
39-
tlib_pass_if_false("adding valid function before init", is_new_wraprec,
40-
"expected false for is_new_wraprec");
41-
wraprec = nr_php_user_instrument_wraprec_hashmap_add(NR_PSTR(SCOPED_METHOD_NAME),
42-
&is_new_wraprec);
43-
tlib_pass_if_null("adding valid method before init", wraprec);
44-
tlib_pass_if_false("adding valid function before init", is_new_wraprec,
45-
"expected false for is_new_wraprec");
46-
47-
// Initialize the hashmap
48-
nr_php_user_instrument_wraprec_hashmap_init();
49-
50-
// Test valid operations after initializing the hashmap
51-
wraprec = nr_php_user_instrument_wraprec_hashmap_add(NR_PSTR(FUNCTION_NAME),
52-
&is_new_wraprec);
53-
tlib_pass_if_not_null("adding valid global function", wraprec);
54-
tlib_pass_if_true("adding valid global function", is_new_wraprec,
55-
"expected true for is_new_wraprec");
56-
57-
found_wraprec = nr_php_user_instrument_wraprec_hashmap_get(func_name, NULL);
58-
tlib_pass_if_ptr_equal("getting valid global function", wraprec, found_wraprec);
59-
60-
wraprec = nr_php_user_instrument_wraprec_hashmap_add(NR_PSTR(FUNCTION_NAME),
61-
&is_new_wraprec);
62-
tlib_pass_if_not_null("adding valid global function one more time", wraprec);
63-
tlib_pass_if_false("adding valid global function one more time", is_new_wraprec,
64-
"expected false for is_new_wraprec");
65-
tlib_pass_if_ptr_equal("getting valid global function one more time", wraprec, found_wraprec);
66-
67-
found_wraprec = nr_php_user_instrument_wraprec_hashmap_get(func_name, scope_name);
68-
tlib_pass_if_null("getting global function with scope", found_wraprec);
69-
70-
wraprec = nr_php_user_instrument_wraprec_hashmap_add(NR_PSTR(SCOPED_METHOD_NAME),
71-
&is_new_wraprec);
72-
tlib_pass_if_not_null("adding valid scoped method", wraprec);
73-
tlib_pass_if_true("adding valid scoped function", is_new_wraprec,
74-
"expected true for is_new_wraprec");
75-
76-
found_wraprec = nr_php_user_instrument_wraprec_hashmap_get(method_name, scope_name);
77-
tlib_pass_if_ptr_equal("getting scoped method", wraprec, found_wraprec);
78-
79-
wraprec = nr_php_user_instrument_wraprec_hashmap_add(NR_PSTR(SCOPED_METHOD_NAME),
80-
&is_new_wraprec);
81-
tlib_pass_if_not_null("adding valid scoped method one more time", wraprec);
82-
tlib_pass_if_false("adding valid scoped method one more time", is_new_wraprec,
83-
"expected false for is_new_wraprec");
84-
tlib_pass_if_ptr_equal("getting valid scoped method one more time", wraprec, found_wraprec);
85-
86-
found_wraprec = nr_php_user_instrument_wraprec_hashmap_get(NULL, scope_name);
87-
tlib_pass_if_null("getting scoped method without method name", found_wraprec);
88-
89-
found_wraprec = nr_php_user_instrument_wraprec_hashmap_get(method_name, NULL);
90-
tlib_pass_if_null("getting scoped method without scope", found_wraprec);
91-
92-
nr_php_user_instrument_wraprec_hashmap_destroy();
32+
for (size_t i = 0; i < sizeof(is_new_wraprec_tests) / sizeof(is_new_wraprec_tests[0]); i++) {
33+
bool *is_new_wraprec_ptr = is_new_wraprec_tests[i];
34+
// user_instrument_wraprec_hashmap is initialized at minit
35+
// destroy it to test agent's behavior when it is not initialized
36+
nr_php_user_instrument_wraprec_hashmap_destroy();
37+
38+
// Test valid operations before initializing the hashmap
39+
wraprec = nr_php_user_instrument_wraprec_hashmap_add(NR_PSTR(FUNCTION_NAME),
40+
is_new_wraprec_ptr);
41+
tlib_pass_if_null("adding valid function before init", wraprec);
42+
if (NULL != is_new_wraprec_ptr) {
43+
tlib_pass_if_false("adding valid function before init", *is_new_wraprec_ptr,
44+
"expected false for is_new_wraprec");
45+
}
46+
wraprec = nr_php_user_instrument_wraprec_hashmap_add(NR_PSTR(SCOPED_METHOD_NAME),
47+
is_new_wraprec_ptr);
48+
tlib_pass_if_null("adding valid method before init", wraprec);
49+
if (NULL != is_new_wraprec_ptr) {
50+
tlib_pass_if_false("adding valid function before init", *is_new_wraprec_ptr,
51+
"expected false for is_new_wraprec");
52+
}
53+
54+
// Initialize the hashmap
55+
nr_php_user_instrument_wraprec_hashmap_init();
56+
57+
// Test valid operations after initializing the hashmap
58+
wraprec = nr_php_user_instrument_wraprec_hashmap_add(NR_PSTR(FUNCTION_NAME),
59+
is_new_wraprec_ptr);
60+
tlib_pass_if_not_null("adding valid global function", wraprec);
61+
if (NULL != is_new_wraprec_ptr) {
62+
tlib_pass_if_true("adding valid global function", *is_new_wraprec_ptr,
63+
"expected true for is_new_wraprec");
64+
}
65+
66+
found_wraprec = nr_php_user_instrument_wraprec_hashmap_get(func_name, NULL);
67+
tlib_pass_if_ptr_equal("getting valid global function", wraprec, found_wraprec);
68+
69+
wraprec = nr_php_user_instrument_wraprec_hashmap_add(NR_PSTR(FUNCTION_NAME),
70+
is_new_wraprec_ptr);
71+
tlib_pass_if_not_null("adding valid global function one more time", wraprec);
72+
if (NULL != is_new_wraprec_ptr) {
73+
tlib_pass_if_false("adding valid global function one more time", *is_new_wraprec_ptr,
74+
"expected false for is_new_wraprec");
75+
}
76+
tlib_pass_if_ptr_equal("getting valid global function one more time", wraprec, found_wraprec);
77+
78+
found_wraprec = nr_php_user_instrument_wraprec_hashmap_get(func_name, scope_name);
79+
tlib_pass_if_null("getting global function with scope", found_wraprec);
80+
81+
wraprec = nr_php_user_instrument_wraprec_hashmap_add(NR_PSTR(SCOPED_METHOD_NAME),
82+
is_new_wraprec_ptr);
83+
tlib_pass_if_not_null("adding valid scoped method", wraprec);
84+
if (NULL != is_new_wraprec_ptr) {
85+
tlib_pass_if_true("adding valid scoped function", *is_new_wraprec_ptr,
86+
"expected true for is_new_wraprec");
87+
}
88+
89+
found_wraprec = nr_php_user_instrument_wraprec_hashmap_get(method_name, scope_name);
90+
tlib_pass_if_ptr_equal("getting scoped method", wraprec, found_wraprec);
91+
92+
wraprec = nr_php_user_instrument_wraprec_hashmap_add(NR_PSTR(SCOPED_METHOD_NAME),
93+
is_new_wraprec_ptr);
94+
tlib_pass_if_not_null("adding valid scoped method one more time", wraprec);
95+
if (NULL != is_new_wraprec_ptr) {
96+
tlib_pass_if_false("adding valid scoped method one more time", *is_new_wraprec_ptr,
97+
"expected false for is_new_wraprec");
98+
}
99+
tlib_pass_if_ptr_equal("getting valid scoped method one more time", wraprec, found_wraprec);
100+
101+
found_wraprec = nr_php_user_instrument_wraprec_hashmap_get(NULL, scope_name);
102+
tlib_pass_if_null("getting scoped method without method name", found_wraprec);
103+
104+
found_wraprec = nr_php_user_instrument_wraprec_hashmap_get(method_name, NULL);
105+
tlib_pass_if_null("getting scoped method without scope", found_wraprec);
106+
107+
nr_php_user_instrument_wraprec_hashmap_destroy();
108+
}
93109

94110
zend_string_free(func_name);
95111
zend_string_free(scope_name);

0 commit comments

Comments
 (0)