Skip to content

Commit

Permalink
Memleak fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Burrow committed Apr 4, 2011
1 parent b2b6f87 commit 2093e10
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 20 deletions.
19 changes: 15 additions & 4 deletions dispatcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "request.h"

#include "string.h"
#include "stdio.h"

#include "fcgi_stdio.h"

Expand Down Expand Up @@ -58,12 +59,12 @@ void dispatch() {
}
for (cur = head; cur != NULL; cur = cur->next) {
if (cur->method == method) {
if (cur->regex == NULL) {
cur->regex = malloc(sizeof(regex_t));
regcomp(cur->regex, cur->regex_str, 0);
if (cur->regex_set == false) {
if (regcomp(&(cur->regex), cur->regex_str, 0)) continue;
cur->regex_set = true;
}
regmatch_t *matches = malloc(sizeof(regmatch_t) * cur->nmatch);
int m = regexec(cur->regex, path_info, cur->nmatch, matches, 0);
int m = regexec(&(cur->regex), path_info, cur->nmatch, matches, 0);
if (m == 0) {
cur->func(matches);
free(matches);
Expand All @@ -83,3 +84,13 @@ void add_handler(handler *h) {
}
last = h;
}

void cleanup_handlers() {
handler *curr = head, *temp = NULL;
while (curr != last) {
if (curr->regex_set)
regfree(&(curr->regex));
curr = curr->next;
}
if (curr->regex_set) regfree(&(curr->regex));
}
7 changes: 5 additions & 2 deletions dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "regex.h"
#include "stdlib.h"
#include "stdbool.h"

#define GET 1
#define POST 2
Expand All @@ -35,15 +36,16 @@ struct handler {
void (*func)(regmatch_t[]);
int method;
const char *regex_str;
regex_t *regex;
regex_t regex;
size_t nmatch;
struct handler *next;
bool regex_set;
};
typedef struct handler handler;

#define START_HANDLER(NAME, METHOD, REGEX, RES, NUM, MATCHES) \
void NAME##_func(); \
handler NAME##_data = {NAME##_func, METHOD, REGEX, NULL, NUM, NULL}; \
handler NAME##_data = {NAME##_func, METHOD, REGEX, {0}, NUM, NULL, false}; \
handler *NAME = &NAME##_data; \
void NAME##_func(regmatch_t MATCHES[]) { \
response *int_response = response_empty(); \
Expand All @@ -55,5 +57,6 @@ void NAME##_func(regmatch_t MATCHES[]) { \

void dispatch();
void add_handler(handler *);
void cleanup_handlers();

#endif
3 changes: 2 additions & 1 deletion raphters.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
*/

#include "raphters.h"

#include "dispatcher.h"
void serve_forever() {
while(FCGI_Accept() >= 0) {
dispatch();
}
cleanup_handlers();
}
31 changes: 21 additions & 10 deletions response.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,17 @@ struct response {
};

response *response_empty() {
response *result = malloc(sizeof(response));
result->header_head = NULL;
result->header_tail = NULL;
result->segment_head = NULL;
result->segment_tail = NULL;
return result;
calloc(1, sizeof(response));
}

void response_write(response *res, const char *text) {
/*
The structure of your code makes it difficult to correctly handle the error condition where response_write or response_add_header fails.
*/

bool response_write(response *res, const char *text) {
text_segment *segment = malloc(sizeof(text_segment));
if (!segment) return false;

segment->text = strdup(text);
segment->next = NULL;
if (res->segment_head == NULL) {
Expand All @@ -63,10 +64,13 @@ void response_write(response *res, const char *text) {
res->segment_tail->next = segment;
}
res->segment_tail = segment;
return true;
}

void response_add_header(response *res, const char *name, const char *val) {
bool response_add_header(response *res, const char *name, const char *val) {
header *h = malloc(sizeof(header));
if (!h) return false;

h->name = strdup(name);
h->value = strdup(val);
h->next = NULL;
Expand All @@ -76,10 +80,11 @@ void response_add_header(response *res, const char *name, const char *val) {
res->header_tail->next = h;
}
res->header_tail = h;
return true;
}

void response_send(response *res) {
header *cur_h;
header* cur_h;
for (cur_h = res->header_head; cur_h != NULL;) {
printf("%s: %s\n", cur_h->name, cur_h->value);
free(cur_h->name);
Expand All @@ -90,7 +95,7 @@ void response_send(response *res) {
cur_h = next;
}
printf("\n");
text_segment *cur_s;
text_segment* cur_s;
for (cur_s = res->segment_head; cur_s != NULL;) {
printf("%s", cur_s->text);
free(cur_s->text);
Expand All @@ -99,4 +104,10 @@ void response_send(response *res) {
free(cur_s);
cur_s = next;
}

response_cleanup(res);
}

void response_cleanup(response* res) {
if (res) free (res);
}
6 changes: 3 additions & 3 deletions response.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ typedef struct response response;

response *response_empty();

void response_add_header(response *, const char *, const char *);
void response_write(response *, const char *);

bool response_add_header(response *, const char *, const char *);
bool response_write(response *, const char *);
void response_cleanup(response* );
#endif

0 comments on commit 2093e10

Please sign in to comment.