summaryrefslogtreecommitdiff
path: root/ecp
diff options
context:
space:
mode:
authorUros Majstorovic <majstor@majstor.org>2024-01-22 00:24:18 +0100
committerUros Majstorovic <majstor@majstor.org>2024-01-22 00:24:18 +0100
commitf5d41629a61ffde8dd444e9aa0bb86479bfa7985 (patch)
treef1ad75e4f6750183e19939f3cb2638a648552f08 /ecp
parent0142b5a696e541a874c5429c28c1a132a284ca79 (diff)
fixed racing condition in vconn
Diffstat (limited to 'ecp')
-rw-r--r--ecp/src/ecp/vconn/vconn.c18
1 files changed, 12 insertions, 6 deletions
diff --git a/ecp/src/ecp/vconn/vconn.c b/ecp/src/ecp/vconn/vconn.c
index f8dc5df..2f3f1b0 100644
--- a/ecp/src/ecp/vconn/vconn.c
+++ b/ecp/src/ecp/vconn/vconn.c
@@ -69,25 +69,31 @@ static int insert_key_next(ECPVConnInb *vconn, unsigned char c_idx, ecp_ecdh_pub
static ssize_t handle_next(ECPConnection *conn, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs) {
ECPVConnInb *vconn = (ECPVConnInb *)conn;
ECPSocket *sock = conn->sock;
- int is_open;
+ int rv = ECP_OK;
if (msg_size < ECP_SIZE_ECDH_PUB) return ECP_ERR_SIZE;
if (ecp_conn_is_outb(conn)) return ECP_ERR;
- if (vconn->next) return ECP_ERR_NEXT;
-
#ifdef ECP_WITH_PTHREAD
pthread_mutex_lock(&sock->conn_table.mutex);
#endif
- vconn->next = ecp_ht_search(sock->conn_table.keys, (ecp_ecdh_public_t *)msg);
- if (vconn->next) ecp_conn_refcount_inc(vconn->next);
+ if (vconn->next == NULL) {
+ vconn->next = ecp_ht_search(sock->conn_table.keys, (ecp_ecdh_public_t *)msg);
+ if (vconn->next) {
+ ecp_conn_refcount_inc(vconn->next);
+ } else {
+ rv = ECP_ERR_NEXT;
+ }
+ } else {
+ rv = ECP_ERR_NEXT;
+ }
#ifdef ECP_WITH_PTHREAD
pthread_mutex_unlock(&sock->conn_table.mutex);
#endif
- if (vconn->next == NULL) return ECP_ERR_NEXT;
+ if (rv) return rv;
return ECP_SIZE_ECDH_PUB;
}