fix: remove double free and memory leaks

This commit is contained in:
kevinj 2024-08-01 09:32:47 +02:00
parent 134c353161
commit 5f77691429
2 changed files with 32 additions and 25 deletions

6
README
View File

@ -10,6 +10,11 @@
RES: en.wikipedia.org/wiki/Proxy_server RES: en.wikipedia.org/wiki/Proxy_server
RES: TCP/IP sockets in C - Practical guide for programmers 2nd edition RES: TCP/IP sockets in C - Practical guide for programmers 2nd edition
TECHNICALS
Parsing is done through a chain of regex expressions that verify
the format of received HTTP/1.0 messages.
COMMITS COMMITS
Each commit is prefixed with an indicator token of what the change is Each commit is prefixed with an indicator token of what the change is
@ -19,4 +24,5 @@ COMMITS
* repo: change is about the repository in general * repo: change is about the repository in general
* proxy: change is about the proxy in general * proxy: change is about the proxy in general
* logic: change is about proxy logic - parsing, forwarding, ... * logic: change is about proxy logic - parsing, forwarding, ...
* fix: change is about fixing a bug or TODO

51
proxy.c
View File

@ -13,6 +13,7 @@
struct http_msg *child_msg; struct http_msg *child_msg;
regex_t preg; regex_t preg;
regmatch_t pmatch[REGEX_MATCHN]; regmatch_t pmatch[REGEX_MATCHN];
int par_line = 0;
void *extractsub(const char *msg, regmatch_t match) { void *extractsub(const char *msg, regmatch_t match) {
int buflen = match.rm_eo - match.rm_so; int buflen = match.rm_eo - match.rm_so;
@ -98,6 +99,28 @@ _err:
} }
void free_title() {
free(child_msg->method);
free(child_msg->uri);
free(child_msg->ver);
}
void free_headers() {
for (int i = 0; i < child_msg->header_num; i++) {
struct header *header = &child_msg->headers[i];
free(header->key);
free(header->value);
}
free(child_msg->headers);
}
void free_message() {
free_title();
free_headers();
free(child_msg);
}
int parse_line(char *line, int line_count) { int parse_line(char *line, int line_count) {
int ret = 0; int ret = 0;
@ -110,13 +133,6 @@ int parse_line(char *line, int line_count) {
return ret; return ret;
} }
int test(char *line, int num) {
fprintf(stdout, "line %s and num %d\n", line, num);
return 0;
}
int par_line = 0;
void handle_request(int sockfd) { void handle_request(int sockfd) {
int ret; int ret;
@ -139,14 +155,7 @@ void handle_request(int sockfd) {
goto end_sock; goto end_sock;
} }
char *ln = strdup(msgbuff); char *ln = strtok(msgbuff, "\n");
if (!ln) {
fprintf(stdout, "Not enough dynamic memory\n");
goto end_sock;
}
ln = strtok(ln, "\n");
while (ln) { while (ln) {
parse_line(ln, par_line); parse_line(ln, par_line);
par_line++; par_line++;
@ -155,16 +164,8 @@ void handle_request(int sockfd) {
// logic // logic
for (int i = 0; i < child_msg->header_num; i++) { free_message();
struct header *header = &child_msg->headers[i]; free(msgbuff);
free(header->key);
free(header->value);
free(header);
}
free(child_msg->method);
free(child_msg->uri);
free(child_msg->ver);
free(child_msg);
end_sock: end_sock:
close(sockfd); close(sockfd);