Skip to content

Commit

Permalink
refactor(agent): avoid strdups by passing in modifiable string
Browse files Browse the repository at this point in the history
  • Loading branch information
zsistla committed Jan 14, 2025
1 parent 09f10e8 commit f48df3f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
29 changes: 7 additions & 22 deletions agent/lib_aws_sdk_php.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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)) {
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 6 additions & 7 deletions agent/lib_aws_sdk_php.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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 :
*
*/
Expand All @@ -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
*
Expand Down

0 comments on commit f48df3f

Please sign in to comment.