From f48df3f365177c9a137f6d11f5a9261681469b80 Mon Sep 17 00:00:00 2001 From: Amber Sistla Date: Tue, 14 Jan 2025 12:00:42 -0700 Subject: [PATCH] refactor(agent): avoid strdups by passing in modifiable string --- agent/lib_aws_sdk_php.c | 29 +++++++---------------------- agent/lib_aws_sdk_php.h | 13 ++++++------- 2 files changed, 13 insertions(+), 29 deletions(-) diff --git a/agent/lib_aws_sdk_php.c b/agent/lib_aws_sdk_php.c index 489d17ea5..631fdae77 100644 --- a/agent/lib_aws_sdk_php.c +++ b/agent/lib_aws_sdk_php.c @@ -136,8 +136,7 @@ void nr_lib_aws_sdk_php_sqs_handle(nr_segment_t* segment, AWS_SDK_PHP_SQSCLIENT_QUEUEURL_ARG, NR_EXECUTE_ORIG_ARGS); /* - * nr_lib_aws_sdk_php_sqs_parse_queueurl checks for NULL so safe pass - * command_arg_value directly in. + * nr_lib_aws_sdk_php_sqs_parse_queueurl requires a modifiable string to populate message_params and cloud_attrs. */ nr_lib_aws_sdk_php_sqs_parse_queueurl(command_arg_value, &message_params, &cloud_attrs); @@ -150,24 +149,15 @@ void nr_lib_aws_sdk_php_sqs_handle(nr_segment_t* segment, nr_segment_message_end(&segment, &message_params); nr_free(command_arg_value); - - /* - * These are the message_params params that were strduped in - * nr_lib_aws_sdk_php_sqs_parse_queueurl - */ - nr_free(cloud_attrs.cloud_region); - nr_free(message_params.destination_name); - nr_free(cloud_attrs.cloud_account_id); } void nr_lib_aws_sdk_php_sqs_parse_queueurl( - const char* sqs_queueurl, + char* sqs_queueurl, nr_segment_message_params_t* message_params, nr_segment_cloud_attrs_t* cloud_attrs) { char* region = NULL; char* queue_name = NULL; char* account_id = NULL; - char queueurl[AWS_QUEUEURL_LEN_MAX]; char* queueurl_pointer = NULL; if (NULL == sqs_queueurl || NULL == message_params) { @@ -189,12 +179,7 @@ void nr_lib_aws_sdk_php_sqs_parse_queueurl( * regex, matching a regex, destroying a regex, this method was chosen as a * more performant option because it's a very limited pattern. */ - - if (NULL == nr_strlcpy(queueurl, sqs_queueurl, AWS_QUEUEURL_LEN_MAX)) { - /* Malformed queueurl, we can't decode this. */ - return; - } - queueurl_pointer = queueurl; + queueurl_pointer = sqs_queueurl; /* * Find the pattern of the AWS queueurl that should immediately precede the @@ -210,7 +195,7 @@ void nr_lib_aws_sdk_php_sqs_parse_queueurl( /* * Find the start of the region. It follows the 12 chars of 'https://sqs.' * and continues until the next '.' It is safe to move the pointer along at - * this point since we allocated a sufficiently big buffer. + * this point since we just verified the prefix exists. */ queueurl_pointer += AWS_QUEUEURL_PREFIX_LEN; if (nr_strempty(queueurl_pointer)) { @@ -299,9 +284,9 @@ void nr_lib_aws_sdk_php_sqs_parse_queueurl( * SQS entity relationship requires: messaging.system, cloud.region, * cloud.account.id, messaging.destination.name */ - message_params->destination_name = nr_strdup(queue_name); - cloud_attrs->cloud_account_id = nr_strdup(account_id); - cloud_attrs->cloud_region = nr_strdup(region); + message_params->destination_name = queue_name; + cloud_attrs->cloud_account_id = account_id; + cloud_attrs->cloud_region = region; } char* nr_lib_aws_sdk_php_get_command_arg_value(char* command_arg_name, diff --git a/agent/lib_aws_sdk_php.h b/agent/lib_aws_sdk_php.h index b345076bd..7e33e5bdb 100644 --- a/agent/lib_aws_sdk_php.h +++ b/agent/lib_aws_sdk_php.h @@ -66,19 +66,17 @@ extern void nr_lib_aws_sdk_php_add_supportability_service_metric( * destination_name. The extraction sets all or none since the values are from * the same string and if it is malformed, it cannot be used. * - * Params : 1. The QueueUrl + * Params : 1. The QueueUrl, MUST be a modifiable string * 2. message_params to set message_params.destination_name * 3. cloud_attrs to set message_params.cloud_region, * message_params.cloud_account_id * - * Returns : + * Returns : applicable cloud_attrs and message params fields will point to null + * terminated strings within the original string. * - * Note: caller is responsible for - * freeing cloud_attrs.cloud_region, cloud_attrs.cloud_account_id, and - * message_params.destination_name */ extern void nr_lib_aws_sdk_php_sqs_parse_queueurl( - const char* sqs_queueurl, + char* sqs_queueurl, nr_segment_message_params_t* message_params, nr_segment_cloud_attrs_t* cloud_attrs); @@ -90,6 +88,7 @@ extern void nr_lib_aws_sdk_php_sqs_parse_queueurl( * 2. command_name_string : the string of the command being called * 3. command_name_len : the length of the command being called * 4. NR_EXECUTE_ORIG_ARGS (execute_data, func_return_value) + * * Returns : * */ @@ -107,7 +106,7 @@ extern void nr_lib_aws_sdk_php_sqs_handle(nr_segment_t* segment, * Params : 1. arg_name: name of argument to extract from command arg array * 2. NR_EXECUTE_PROTO (execute_data, func_return_value) * - * Returns : the value of the arg_name; NULL if does not exist + * Returns : the strduped value of the arg_name; NULL if does not exist * * Note: The caller is responsible for freeing the returned string value *