From fa1e8610f8182d1d1f20315682c7e31bca7ed2a3 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Fri, 14 Apr 2023 15:30:51 -0500 Subject: [PATCH] Fixed bug with manipulation of the message string, by creating a copy of the string parameter --- message_helpers.c | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/message_helpers.c b/message_helpers.c index 1c87f3b..edcf33f 100644 --- a/message_helpers.c +++ b/message_helpers.c @@ -25,26 +25,34 @@ char* fetch_from_string(char* message, char* indicator) { } char* fetch_message_string(char* message) { - int num_of_terminators; - char* start = strstr(message,"START_OF_MESSAGE"); + char* message_copy = malloc(strlen(message)); + strcpy(message_copy,message); + + int num_of_terminators = 0; + int start_index = 0; + int end_index = 0; + int message_length = 0; + + + char* start = strstr(message_copy,"START_OF_MESSAGE"); if (start == NULL) { return NULL; } - int start_index = start - message; + start_index = start - message_copy; start_index += strlen("START_OF_MESSAGE"); - while (*(message + start_index) == '\n' || *(message + start_index) == '\r') { + while (*(message_copy + start_index) == '\n' || *(message_copy + start_index) == '\r') { start_index++; } - char* end = strstr(message,"END_OF_MESSAGE"); + char* end = strstr(message_copy,"END_OF_MESSAGE"); if (end == NULL) { return NULL; } - int end_index = end - message; + end_index = end - message_copy; - if (*(message + end_index-2) = '\r') { + if (*(message_copy + end_index-2) == '\r') { num_of_terminators = 2; } else { num_of_terminators = 1; @@ -52,15 +60,15 @@ char* fetch_message_string(char* message) { end_index -= num_of_terminators; - int message_length = end_index - start_index; + message_length = end_index - start_index; char* message_string = malloc(message_length + 2); - printf("Message length is %d\n",message_length); + printf("Message goes from %d to %d\n",start_index,end_index); for (int i=0; i