Fix memory corruption in parse_connection_string

- Fixed 'free(): invalid pointer' error when using service prefix format
- Issue was caused by modifying the original allocated string and then trying to free a pointer to the middle of it
- Added proper pointer tracking to ensure correct memory deallocation
parent 952e5687
No preview for this file type
...@@ -70,26 +70,28 @@ int parse_connection_string(const char *conn_str, char **service, char **client_ ...@@ -70,26 +70,28 @@ int parse_connection_string(const char *conn_str, char **service, char **client_
char *str = strdup(conn_str); char *str = strdup(conn_str);
if (!str) return 0; if (!str) return 0;
char *working_str = str; // Keep track of what to free
// Check for service prefix (e.g., "ssh://client@host:port") // Check for service prefix (e.g., "ssh://client@host:port")
char *service_end = strstr(str, "://"); char *service_end = strstr(str, "://");
if (service_end) { if (service_end) {
*service_end = '\0'; *service_end = '\0';
*service = strdup(str); *service = strdup(str);
str = service_end + 3; working_str = service_end + 3; // Point to part after "://"
} }
// Find @ separator for client@host // Find @ separator for client@host
char *at_pos = strchr(str, '@'); char *at_pos = strchr(working_str, '@');
if (!at_pos) { if (!at_pos) {
// No @ found, treat whole string as client_id // No @ found, treat whole string as client_id
*client_id = strdup(str); *client_id = strdup(working_str);
free(str); free(str);
return 1; return 1;
} }
// Split client_id and host part // Split client_id and host part
*at_pos = '\0'; *at_pos = '\0';
*client_id = strdup(str); *client_id = strdup(working_str);
// Parse host and port // Parse host and port
char *host_part = at_pos + 1; char *host_part = at_pos + 1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment