Skip to content

Commit 1b63785

Browse files
committed
Solve memory leak problems
1 parent 50bdbee commit 1b63785

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

proxy-helpers/url_parser.c

+16-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ void init_url_info(URL_INFO *info)
99
info->protocol = Malloc(MAXLINE);
1010
}
1111

12+
void free_url_info(URL_INFO *info)
13+
{
14+
Free(info->host);
15+
Free(info->port);
16+
Free(info->path);
17+
Free(info->protocol);
18+
19+
Free(info);
20+
}
21+
1222

1323
/*
1424
* parse_url - get plain URL and parse it into
@@ -23,13 +33,14 @@ URL_INFO *parse_url(URL_INFO *info, const char *url)
2333
init_url_info(info);
2434

2535
char *url_copy = (char *)Malloc(strlen(url) + 1);
36+
char *temp = url_copy;
2637
strcpy(url_copy, url);
2738
strcpy(info->protocol, __strtok_r(url_copy, "://", &save_ptr));
28-
strcpy(info->host, strstr(url, "://"));
29-
Free(url_copy);
39+
strcpy(url_copy, strstr(url, "://"));
3040

31-
if (info->host) {
32-
info->host += 3;
41+
if (url_copy) {
42+
url_copy += 3;
43+
strcpy(info->host, url_copy);
3344
char *host_port_path = (char *)Calloc(1, strlen(info->host) + 1);
3445
strcpy(host_port_path, info->host);
3546
strcpy(info->host, __strtok_r(host_port_path, ":", &save_ptr));
@@ -71,6 +82,7 @@ URL_INFO *parse_url(URL_INFO *info, const char *url)
7182
else if (r)
7283
strcpy(info->protocol, "tcp");
7384

85+
Free(temp);
7486
Free(URL);
7587
Free(port_path_copy);
7688
return info;

proxy-helpers/url_parser.h

+2
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ typedef struct url_info {
1414

1515
/* Functions prototypes */
1616
URL_INFO *parse_url(URL_INFO *info, const char *url);
17+
void init_url_info(URL_INFO *info);
18+
void free_url_info(URL_INFO *info);
1719
#endif

0 commit comments

Comments
 (0)