From 832851b83d3ee2a918896d6f61d8e05d280fde25 Mon Sep 17 00:00:00 2001 From: Emil Mikulic Date: Mon, 24 Nov 2003 11:48:49 +0000 Subject: [PATCH] . Add safefree(x) macro to set x to NULL after freeing. --- trunk/darkhttpd.c | 60 ++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/trunk/darkhttpd.c b/trunk/darkhttpd.c index 9174d5e..d62e1d7 100644 --- a/trunk/darkhttpd.c +++ b/trunk/darkhttpd.c @@ -52,6 +52,8 @@ /* for easy defusal */ #define debugf printf +#define safefree(x) do { free(x); x = NULL; } while(0) + #ifndef min #define min(a,b) ( ((a)<(b)) ? (a) : (b) ) #endif @@ -470,13 +472,13 @@ static char *make_safe_uri(char *uri) /* unsafe string so free elem[]; all its elements are free at * this point. */ - free(elem); + safefree(elem); return NULL; } else { elements--; - free(elem[elements]); + safefree(elem[elements]); } } else elem[elements++] = split_string(uri, i, j); @@ -496,7 +498,7 @@ static char *make_safe_uri(char *uri) assert(pos+delta <= urilen); memcpy(out+pos, elem[i], delta); - free(elem[i]); + safefree(elem[i]); pos += delta; } free(elem); @@ -521,7 +523,7 @@ static void test_make_safe_uri(void) debugf("FAIL: `%s' unsafe, expecting `%s'\n", from, to); \ else if (strcmp(tmp, to) != 0) \ debugf("FAIL: `%s' -> `%s', expecting `%s'\n", from, tmp, to); \ - free(tmp); free(uri); } while(0) + safefree(tmp); free(uri); } while(0) SAFE("/", "/"); SAFE("//", "/"); @@ -552,7 +554,7 @@ static void test_make_safe_uri(void) #define UNSAFE(x) do { char *uri = xstrdup(x), *tmp;\ tmp = make_safe_uri(uri); if (tmp != NULL) { \ debugf("FAIL: `%s' is UNSAFE, not `%s'\n", x, tmp); \ - free(tmp); }; free(uri); } while(0) + safefree(tmp); }; safefree(uri); } while(0) UNSAFE("/.."); UNSAFE("/../"); @@ -588,7 +590,7 @@ static void add_mime_mapping(const char *extension, const char *mimetype) for (i=0; isocket); log_connection(conn); if (conn->socket != -1) close(conn->socket); - if (conn->request != NULL) free(conn->request); - if (conn->method != NULL) free(conn->method); - if (conn->uri != NULL) free(conn->uri); - if (conn->referer != NULL) free(conn->referer); - if (conn->user_agent != NULL) free(conn->user_agent); - if (conn->header != NULL && !conn->header_dont_free) free(conn->header); - if (conn->reply != NULL && !conn->reply_dont_free) free(conn->reply); + if (conn->request != NULL) safefree(conn->request); + if (conn->method != NULL) safefree(conn->method); + if (conn->uri != NULL) safefree(conn->uri); + if (conn->referer != NULL) safefree(conn->referer); + if (conn->user_agent != NULL) safefree(conn->user_agent); + if (conn->header != NULL && !conn->header_dont_free) + safefree(conn->header); + if (conn->reply != NULL && !conn->reply_dont_free) safefree(conn->reply); if (conn->reply_file != NULL) fclose(conn->reply_file); } @@ -1202,7 +1205,7 @@ static void default_reply(struct connection *conn, "Generated by %s on %s\n" "\n", errcode, errname, errname, reason, pkgname, date); - free(reason); + safefree(reason); conn->header_length = xasprintf(&(conn->header), "HTTP/1.1 %d %s\r\n" @@ -1256,7 +1259,7 @@ static void redirect(struct connection *conn, const char *format, ...) "\r\n", date, pkgname, where, keep_alive(conn), conn->reply_length); - free(where); + safefree(where); conn->reply_type = REPLY_GENERATED; conn->http_code = 301; } @@ -1343,8 +1346,7 @@ static void parse_range_field(struct connection *conn) } } while(0); /* break handling */ - - free(range); + safefree(range); /* sanity check: begin <= end */ if (conn->range_begin_given && conn->range_end_given && @@ -1404,7 +1406,7 @@ static int parse_request(struct connection *conn) proto = split_string(conn->request, bound1, bound2); if (strcasecmp(proto, "HTTP/1.1") == 0) conn->conn_close = 0; - free(proto); + safefree(proto); } /* parse connection field */ @@ -1413,7 +1415,7 @@ static int parse_request(struct connection *conn) { if (strcasecmp(tmp, "close") == 0) conn->conn_close = 1; else if (strcasecmp(tmp, "keep-alive") == 0) conn->conn_close = 0; - free(tmp); + safefree(tmp); } /* parse important fields */ @@ -1440,7 +1442,7 @@ static void process_get(struct connection *conn) /* make sure it's safe */ safe_url = make_safe_uri(decoded_url); - free(decoded_url); decoded_url = NULL; + safefree(decoded_url); if (safe_url == NULL) { default_reply(conn, 400, "Bad Request", @@ -1459,7 +1461,7 @@ static void process_get(struct connection *conn) xasprintf(&target, "%s%s", wwwroot, safe_url); mimetype = uri_content_type(safe_url); } - free(safe_url); safe_url = NULL; + safefree(safe_url); debugf("uri=%s, target=%s, content-type=%s\n", conn->uri, target, mimetype); @@ -1491,7 +1493,7 @@ static void process_get(struct connection *conn) } conn->reply_file = fopen(target, "rb"); - free(target); target = NULL; + safefree(target); if (conn->reply_file == NULL) { @@ -1526,10 +1528,10 @@ static void process_get(struct connection *conn) debugf("not modified since %s\n", if_mod_since); default_reply(conn, 304, "Not Modified", ""); conn->header_only = 1; - free(if_mod_since); + safefree(if_mod_since); return; } - free(if_mod_since); + safefree(if_mod_since); if (conn->range_begin_given || conn->range_end_given) { @@ -1645,7 +1647,7 @@ static void process_request(struct connection *conn) conn->state = SEND_HEADER; /* request not needed anymore */ - free(conn->request); conn->request = NULL; + safefree(conn->request); } @@ -1903,7 +1905,7 @@ static void httpd_poll(void) { LIST_REMOVE(conn, entries); free_connection(conn); - free(conn); + safefree(conn); } else {