From a521d405a461b3384cd70daf95a2219d72db3eb1 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Mon, 8 Dec 2014 16:37:24 +0100 Subject: [PATCH] Guard against NULL pointer dereferencing. --- test.c | 2 +- utcp.c | 34 +++++++++++++++++++++------------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/test.c b/test.c index fcf0c79..b5d64a3 100644 --- a/test.c +++ b/test.c @@ -106,7 +106,7 @@ int main(int argc, char *argv[]) { struct timeval timeout = utcp_timeout(u); while(dir) { - size_t max = utcp_get_sndbuf_free(c); + size_t max = c ? utcp_get_sndbuf_free(c) : 0; if(max > sizeof buf) max = sizeof buf; diff --git a/utcp.c b/utcp.c index 1d8f4b2..21fbea6 100644 --- a/utcp.c +++ b/utcp.c @@ -1154,63 +1154,71 @@ void utcp_exit(struct utcp *utcp) { } uint16_t utcp_get_mtu(struct utcp *utcp) { - return utcp->mtu; + return utcp ? utcp->mtu : 0; } void utcp_set_mtu(struct utcp *utcp, uint16_t mtu) { // TODO: handle overhead of the header - utcp->mtu = mtu; + if(utcp) + utcp->mtu = mtu; } int utcp_get_user_timeout(struct utcp *u) { - return u->timeout; + return u ? u->timeout : 0; } void utcp_set_user_timeout(struct utcp *u, int timeout) { - u->timeout = timeout; + if(u) + u->timeout = timeout; } size_t utcp_get_sndbuf(struct utcp_connection *c) { - return c->sndbuf.maxsize; + return c ? c->sndbuf.maxsize : 0; } size_t utcp_get_sndbuf_free(struct utcp_connection *c) { - if(c->state == ESTABLISHED || c->state == CLOSE_WAIT) + if(c && (c->state == ESTABLISHED || c->state == CLOSE_WAIT)) return buffer_free(&c->sndbuf); else return 0; } void utcp_set_sndbuf(struct utcp_connection *c, size_t size) { + if(!c) + return; c->sndbuf.maxsize = size; if(c->sndbuf.maxsize != size) c->sndbuf.maxsize = -1; } bool utcp_get_nodelay(struct utcp_connection *c) { - return c->nodelay; + return c ? c->nodelay : false; } void utcp_set_nodelay(struct utcp_connection *c, bool nodelay) { - c->nodelay = nodelay; + if(c) + c->nodelay = nodelay; } bool utcp_get_keepalive(struct utcp_connection *c) { - return c->keepalive; + return c ? c->keepalive : false; } void utcp_set_keepalive(struct utcp_connection *c, bool keepalive) { - c->keepalive = keepalive; + if(c) + c->keepalive = keepalive; } size_t utcp_get_outq(struct utcp_connection *c) { - return seqdiff(c->snd.nxt, c->snd.una); + return c ? seqdiff(c->snd.nxt, c->snd.una) : 0; } void utcp_set_recv_cb(struct utcp_connection *c, utcp_recv_t recv) { - c->recv = recv; + if(c) + c->recv = recv; } void utcp_set_poll_cb(struct utcp_connection *c, utcp_poll_t poll) { - c->poll = poll; + if(c) + c->poll = poll; } -- 2.39.5