. Add safefree(x) macro to set x to NULL after freeing.

This commit is contained in:
Emil Mikulic 2003-11-24 11:48:49 +00:00
parent e74bb5a077
commit 832851b83d

View File

@ -52,6 +52,8 @@
/* for easy defusal */ /* for easy defusal */
#define debugf printf #define debugf printf
#define safefree(x) do { free(x); x = NULL; } while(0)
#ifndef min #ifndef min
#define min(a,b) ( ((a)<(b)) ? (a) : (b) ) #define min(a,b) ( ((a)<(b)) ? (a) : (b) )
#endif #endif
@ -470,13 +472,13 @@ static char *make_safe_uri(char *uri)
/* unsafe string so free elem[]; all its elements are free at /* unsafe string so free elem[]; all its elements are free at
* this point. * this point.
*/ */
free(elem); safefree(elem);
return NULL; return NULL;
} }
else else
{ {
elements--; elements--;
free(elem[elements]); safefree(elem[elements]);
} }
} }
else elem[elements++] = split_string(uri, i, j); else elem[elements++] = split_string(uri, i, j);
@ -496,7 +498,7 @@ static char *make_safe_uri(char *uri)
assert(pos+delta <= urilen); assert(pos+delta <= urilen);
memcpy(out+pos, elem[i], delta); memcpy(out+pos, elem[i], delta);
free(elem[i]); safefree(elem[i]);
pos += delta; pos += delta;
} }
free(elem); free(elem);
@ -521,7 +523,7 @@ static void test_make_safe_uri(void)
debugf("FAIL: `%s' unsafe, expecting `%s'\n", from, to); \ debugf("FAIL: `%s' unsafe, expecting `%s'\n", from, to); \
else if (strcmp(tmp, to) != 0) \ else if (strcmp(tmp, to) != 0) \
debugf("FAIL: `%s' -> `%s', expecting `%s'\n", from, tmp, to); \ debugf("FAIL: `%s' -> `%s', expecting `%s'\n", from, tmp, to); \
free(tmp); free(uri); } while(0) safefree(tmp); free(uri); } while(0)
SAFE("/", "/"); SAFE("/", "/");
SAFE("//", "/"); SAFE("//", "/");
@ -552,7 +554,7 @@ static void test_make_safe_uri(void)
#define UNSAFE(x) do { char *uri = xstrdup(x), *tmp;\ #define UNSAFE(x) do { char *uri = xstrdup(x), *tmp;\
tmp = make_safe_uri(uri); if (tmp != NULL) { \ tmp = make_safe_uri(uri); if (tmp != NULL) { \
debugf("FAIL: `%s' is UNSAFE, not `%s'\n", x, tmp); \ debugf("FAIL: `%s' is UNSAFE, not `%s'\n", x, tmp); \
free(tmp); }; free(uri); } while(0) safefree(tmp); }; safefree(uri); } while(0)
UNSAFE("/.."); UNSAFE("/..");
UNSAFE("/../"); UNSAFE("/../");
@ -588,7 +590,7 @@ static void add_mime_mapping(const char *extension, const char *mimetype)
for (i=0; i<mime_map_size; i++) for (i=0; i<mime_map_size; i++)
if (strcmp(mime_map[i].extension, extension) == 0) if (strcmp(mime_map[i].extension, extension) == 0)
{ {
free(mime_map[i].mimetype); safefree(mime_map[i].mimetype);
mime_map[i].mimetype = xstrdup(mimetype); mime_map[i].mimetype = xstrdup(mimetype);
return; return;
} }
@ -663,8 +665,8 @@ static void parse_mimetype_line(const char *line)
mimetype = split_string(line, pad, bound1); mimetype = split_string(line, pad, bound1);
extension = split_string(line, lbound, rbound); extension = split_string(line, lbound, rbound);
add_mime_mapping(extension, mimetype); add_mime_mapping(extension, mimetype);
free(mimetype); safefree(mimetype);
free(extension); safefree(extension);
if (line[rbound] == '\0') return; /* end of line */ if (line[rbound] == '\0') return; /* end of line */
else lbound = rbound + 1; else lbound = rbound + 1;
@ -767,7 +769,7 @@ static void parse_extension_map_file(const char *filename)
{ {
chomp(buf); chomp(buf);
parse_mimetype_line(buf); parse_mimetype_line(buf);
free(buf); safefree(buf);
} }
fclose(fp); fclose(fp);
@ -1036,13 +1038,14 @@ static void free_connection(struct connection *conn)
debugf("free_connection(%d)\n", conn->socket); debugf("free_connection(%d)\n", conn->socket);
log_connection(conn); log_connection(conn);
if (conn->socket != -1) close(conn->socket); if (conn->socket != -1) close(conn->socket);
if (conn->request != NULL) free(conn->request); if (conn->request != NULL) safefree(conn->request);
if (conn->method != NULL) free(conn->method); if (conn->method != NULL) safefree(conn->method);
if (conn->uri != NULL) free(conn->uri); if (conn->uri != NULL) safefree(conn->uri);
if (conn->referer != NULL) free(conn->referer); if (conn->referer != NULL) safefree(conn->referer);
if (conn->user_agent != NULL) free(conn->user_agent); if (conn->user_agent != NULL) safefree(conn->user_agent);
if (conn->header != NULL && !conn->header_dont_free) free(conn->header); if (conn->header != NULL && !conn->header_dont_free)
if (conn->reply != NULL && !conn->reply_dont_free) free(conn->reply); safefree(conn->header);
if (conn->reply != NULL && !conn->reply_dont_free) safefree(conn->reply);
if (conn->reply_file != NULL) fclose(conn->reply_file); 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" "Generated by %s on %s\n"
"</body></html>\n", "</body></html>\n",
errcode, errname, errname, reason, pkgname, date); errcode, errname, errname, reason, pkgname, date);
free(reason); safefree(reason);
conn->header_length = xasprintf(&(conn->header), conn->header_length = xasprintf(&(conn->header),
"HTTP/1.1 %d %s\r\n" "HTTP/1.1 %d %s\r\n"
@ -1256,7 +1259,7 @@ static void redirect(struct connection *conn, const char *format, ...)
"\r\n", "\r\n",
date, pkgname, where, keep_alive(conn), conn->reply_length); date, pkgname, where, keep_alive(conn), conn->reply_length);
free(where); safefree(where);
conn->reply_type = REPLY_GENERATED; conn->reply_type = REPLY_GENERATED;
conn->http_code = 301; conn->http_code = 301;
} }
@ -1343,8 +1346,7 @@ static void parse_range_field(struct connection *conn)
} }
} }
while(0); /* break handling */ while(0); /* break handling */
safefree(range);
free(range);
/* sanity check: begin <= end */ /* sanity check: begin <= end */
if (conn->range_begin_given && conn->range_end_given && 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); proto = split_string(conn->request, bound1, bound2);
if (strcasecmp(proto, "HTTP/1.1") == 0) conn->conn_close = 0; if (strcasecmp(proto, "HTTP/1.1") == 0) conn->conn_close = 0;
free(proto); safefree(proto);
} }
/* parse connection field */ /* parse connection field */
@ -1413,7 +1415,7 @@ static int parse_request(struct connection *conn)
{ {
if (strcasecmp(tmp, "close") == 0) conn->conn_close = 1; if (strcasecmp(tmp, "close") == 0) conn->conn_close = 1;
else if (strcasecmp(tmp, "keep-alive") == 0) conn->conn_close = 0; else if (strcasecmp(tmp, "keep-alive") == 0) conn->conn_close = 0;
free(tmp); safefree(tmp);
} }
/* parse important fields */ /* parse important fields */
@ -1440,7 +1442,7 @@ static void process_get(struct connection *conn)
/* make sure it's safe */ /* make sure it's safe */
safe_url = make_safe_uri(decoded_url); safe_url = make_safe_uri(decoded_url);
free(decoded_url); decoded_url = NULL; safefree(decoded_url);
if (safe_url == NULL) if (safe_url == NULL)
{ {
default_reply(conn, 400, "Bad Request", default_reply(conn, 400, "Bad Request",
@ -1459,7 +1461,7 @@ static void process_get(struct connection *conn)
xasprintf(&target, "%s%s", wwwroot, safe_url); xasprintf(&target, "%s%s", wwwroot, safe_url);
mimetype = uri_content_type(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", debugf("uri=%s, target=%s, content-type=%s\n",
conn->uri, target, mimetype); conn->uri, target, mimetype);
@ -1491,7 +1493,7 @@ static void process_get(struct connection *conn)
} }
conn->reply_file = fopen(target, "rb"); conn->reply_file = fopen(target, "rb");
free(target); target = NULL; safefree(target);
if (conn->reply_file == NULL) 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); debugf("not modified since %s\n", if_mod_since);
default_reply(conn, 304, "Not Modified", ""); default_reply(conn, 304, "Not Modified", "");
conn->header_only = 1; conn->header_only = 1;
free(if_mod_since); safefree(if_mod_since);
return; return;
} }
free(if_mod_since); safefree(if_mod_since);
if (conn->range_begin_given || conn->range_end_given) if (conn->range_begin_given || conn->range_end_given)
{ {
@ -1645,7 +1647,7 @@ static void process_request(struct connection *conn)
conn->state = SEND_HEADER; conn->state = SEND_HEADER;
/* request not needed anymore */ /* 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); LIST_REMOVE(conn, entries);
free_connection(conn); free_connection(conn);
free(conn); safefree(conn);
} }
else else
{ {