Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

Commit

Permalink
adding more null checks. I wish __attr__(nonull) was portable
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFrench committed Aug 31, 2017
1 parent f60e8ea commit 9b552b7
Showing 1 changed file with 69 additions and 38 deletions.
107 changes: 69 additions & 38 deletions evhtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ htp_log_connection(evhtp_connection_t * c)
(var) = (tvar))
#endif

/* r_c == request->conn. Just little things to make life easier */
#define rc_scratch conn->scratch_buf
#define rc_parser conn->parser

#ifndef EVHTP_DISABLE_MEMFUNCTIONS

static void * (*malloc_)(size_t sz) = malloc;
Expand Down Expand Up @@ -169,11 +173,13 @@ htp__calloc_(size_t nmemb, size_t size)
size_t len = nmemb * size;
void * p;

if ((p = malloc_(len)) != NULL)
if ((p = malloc_(len)) == NULL)
{
memset(p, 0, len);
return NULL;
}

memset(p, 0, len);

return p;
}

Expand All @@ -185,14 +191,18 @@ htp__strdup_(const char * str)
{
if (malloc_ != malloc)
{
size_t len = strlen(str);
size_t len;
void * p;

if ((p = malloc_(len + 1)) != NULL)
len = strlen(str);

if ((p = malloc_(len + 1)) == NULL)
{
memcpy(p, str, len + 1);
return NULL;
}

memcpy(p, str, len + 1);

return p;
}

Expand Down Expand Up @@ -1594,6 +1604,11 @@ htp__request_parse_path_(htparser * p, const char * data, size_t len)
evhtp_connection_t * c = htparser_get_userdata(p);
evhtp_path_t * path;

if (evhtp_unlikely(p == NULL || c == NULL))
{
return -1;
}

if (htp__require_uri_(c) == -1)
{
return -1;
Expand Down Expand Up @@ -1879,18 +1894,24 @@ htp__create_reply_(evhtp_request_t * request, evhtp_res code)
unsigned char minor;
char out_buf[64];

evhtp_assert(request
&& request->headers_out
&& request->buffer_out
&& request->conn
&& request->rc_parser);

content_type = evhtp_header_find(request->headers_out, "Content-Type");
out_len = evbuffer_get_length(request->buffer_out);

if ((buf = request->conn->scratch_buf) == NULL)
if ((buf = request->rc_scratch) == NULL)
{
return NULL;
request->rc_scratch = evbuffer_new();
evhtp_alloc_assert(request->rc_scratch);
}

evbuffer_drain(buf, -1);

if (htparser_get_multipart(request->conn->parser) == 1)
if (htparser_get_multipart(request->rc_parser) == 1)
{
goto check_proto;
}
Expand Down Expand Up @@ -1939,8 +1960,8 @@ htp__create_reply_(evhtp_request_t * request, evhtp_res code)
default:
/* this sometimes happens when a response is made but paused before
* the method has been parsed */
htparser_set_major(request->conn->parser, 1);
htparser_set_minor(request->conn->parser, 0);
htparser_set_major(request->rc_parser, 1);
htparser_set_minor(request->rc_parser, 0);
break;
} /* switch */

Expand All @@ -1957,8 +1978,8 @@ htp__create_reply_(evhtp_request_t * request, evhtp_res code)
* we fallback to using evbuffer_add_printf().
*/

major = evhtp_modp_uchartoa(htparser_get_major(request->conn->parser));
minor = evhtp_modp_uchartoa(htparser_get_minor(request->conn->parser));
major = evhtp_modp_uchartoa(htparser_get_major(request->rc_parser));
minor = evhtp_modp_uchartoa(htparser_get_minor(request->rc_parser));

evhtp_modp_u32toa((uint32_t)code, out_buf);

Expand Down Expand Up @@ -2022,12 +2043,13 @@ htp__connection_readcb_(struct bufferevent * bev, void * arg)
size_t nread;
size_t avail;

htp_log_debug("enter sock = %d", c->sock);
if (evhtp_unlikely(bev == NULL))
{
return;
}

avail = evbuffer_get_length(bufferevent_get_input(bev));

htp_log_debug("available bytes %zu", avail);

if (evhtp_unlikely(avail == 0))
{
return;
Expand All @@ -2045,7 +2067,8 @@ htp__connection_readcb_(struct bufferevent * bev, void * arg)

buf = evbuffer_pullup(bufferevent_get_input(bev), avail);

htp_log_debug("buffer is\n----\n%.*s\n-----", (int)avail, (const char *)buf);
evhtp_assert(buf != NULL);
evhtp_assert(c->parser != NULL);

nread = htparser_run(c->parser, &request_psets, (const char *)buf, avail);

Expand Down Expand Up @@ -2094,14 +2117,11 @@ htp__connection_readcb_(struct bufferevent * bev, void * arg)
static void
htp__connection_writecb_(struct bufferevent * bev, void * arg)
{
evhtp_connection_t * c = arg;
evhtp_connection_t * c;

htp_log_debug("c->request = %p", c->request);
c = arg;

if (evhtp_unlikely(c->request == NULL))
{
return;
}
evhtp_assert(c && c->htp && c->request && c->parser && bev);

htp__hook_connection_write_(c);

Expand Down Expand Up @@ -2501,7 +2521,11 @@ htp__accept_cb_(struct evconnlistener * serv, int fd, struct sockaddr * s, int s
evhtp_t * htp = arg;
evhtp_connection_t * connection;

if (evhtp_unlikely(!(connection = htp__connection_new_(htp, fd, evhtp_type_server))))
evhtp_assert(htp && serv && serv && s);

connection = htp__connection_new_(htp, fd, evhtp_type_server);

if (evhtp_unlikely(connection == NULL))
{
return;
}
Expand Down Expand Up @@ -2530,17 +2554,15 @@ htp__accept_cb_(struct evconnlistener * serv, int fd, struct sockaddr * s, int s
#endif
connection->evbase = htp->evbase;

if (htp__connection_accept_(htp->evbase, connection) < 0)
if (htp__connection_accept_(htp->evbase, connection) == -1)
{
evhtp_connection_free(connection);

return;
}

if (htp__run_post_accept_(htp, connection) < 0)
if (htp__run_post_accept_(htp, connection) == -1)
{
evhtp_connection_free(connection);

return;
}
} /* htp__accept_cb_ */
Expand Down Expand Up @@ -2647,6 +2669,11 @@ htp__ssl_servername_(evhtp_ssl_t * ssl, int * unused, void * arg)
evhtp_t * evhtp;
evhtp_t * evhtp_vhost;

if (evhtp_unlikely(ssl == NULL))
{
return SSL_TLSEXT_ERR_NOACK;
}

if (!(sname = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name)))
{
return SSL_TLSEXT_ERR_NOACK;
Expand Down Expand Up @@ -2766,11 +2793,11 @@ evhtp_request_resume(evhtp_request_t * request)
}

evhtp_header_t *
evhtp_header_key_add(evhtp_headers_t * headers, const char * key, char kalloc)
evhtp_header_key_add(evhtp_headers_t * headers, const char * key, char key_alloc)
{
evhtp_header_t * header;

if (!(header = evhtp_header_new(key, NULL, kalloc, 0)))
if (!(header = evhtp_header_new(key, NULL, key_alloc, 0)))
{
return NULL;
}
Expand All @@ -2781,7 +2808,7 @@ evhtp_header_key_add(evhtp_headers_t * headers, const char * key, char kalloc)
}

evhtp_header_t *
evhtp_header_val_add(evhtp_headers_t * headers, const char * val, char valloc)
evhtp_header_val_add(evhtp_headers_t * headers, const char * val, char val_alloc)
{
evhtp_header_t * header;

Expand All @@ -2802,16 +2829,18 @@ evhtp_header_val_add(evhtp_headers_t * headers, const char * val, char valloc)

header->vlen = strlen(val);

if (valloc == 1)
if (val_alloc == 1)
{
header->val = htp__malloc_(header->vlen + 1);
evhtp_alloc_assert(header->val);

header->val[header->vlen] = '\0';
memcpy(header->val, val, header->vlen);
} else {
header->val = (char *)val;
}

header->v_heaped = valloc;
header->v_heaped = val_alloc;

return header;
}
Expand All @@ -2830,15 +2859,16 @@ evhtp_kvs_new(void)
}

evhtp_kv_t *
evhtp_kv_new(const char * key, const char * val, char kalloc, char valloc)
evhtp_kv_new(const char * key, const char * val,
char key_alloc, char val_alloc)
{
evhtp_kv_t * kv;

kv = htp__malloc_(sizeof(evhtp_kv_t));
evhtp_alloc_assert(kv);

kv->k_heaped = kalloc;
kv->v_heaped = valloc;
kv->k_heaped = key_alloc;
kv->v_heaped = val_alloc;
kv->klen = 0;
kv->vlen = 0;
kv->key = NULL;
Expand All @@ -2848,7 +2878,7 @@ evhtp_kv_new(const char * key, const char * val, char kalloc, char valloc)
{
kv->klen = strlen(key);

if (kalloc == 1)
if (key_alloc == 1)
{
char * s;

Expand All @@ -2872,7 +2902,7 @@ evhtp_kv_new(const char * key, const char * val, char kalloc, char valloc)
{
kv->vlen = strlen(val);

if (valloc == 1)
if (val_alloc == 0)
{
char * s = htp__malloc_(kv->vlen + 1);

Expand Down Expand Up @@ -4216,7 +4246,8 @@ evhtp_get_cb(evhtp_t * htp, const char * path)

evhtp_assert(htp != NULL);

if (evhtp_unlikely(htp->callbacks == NULL)) {
if (evhtp_unlikely(htp->callbacks == NULL))
{
return NULL;
}

Expand Down

0 comments on commit 9b552b7

Please sign in to comment.