From 3c71977e2e7b6c6ef91572b859cf464328f16b2f Mon Sep 17 00:00:00 2001 From: James Hilliard Date: Sun, 25 Sep 2022 14:08:58 -0400 Subject: [PATCH] Fix null pointer dereference in mg_rpc_add() Fixes #1736 --- mongoose.c | 6 ++++-- src/rpc.c | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/mongoose.c b/mongoose.c index f606da31..e5efa3cb 100644 --- a/mongoose.c +++ b/mongoose.c @@ -3546,8 +3546,10 @@ void mg_mgr_init(struct mg_mgr *mgr) { void mg_rpc_add(struct mg_rpc **head, struct mg_str method, void (*fn)(struct mg_rpc_req *), void *fn_data) { struct mg_rpc *rpc = (struct mg_rpc *) calloc(1, sizeof(*rpc)); - rpc->method = mg_strdup(method), rpc->fn = fn, rpc->fn_data = fn_data; - rpc->next = *head, *head = rpc; + if (rpc != NULL) { + rpc->method = mg_strdup(method), rpc->fn = fn, rpc->fn_data = fn_data; + rpc->next = *head, *head = rpc; + } } void mg_rpc_del(struct mg_rpc **head, void (*fn)(struct mg_rpc_req *)) { diff --git a/src/rpc.c b/src/rpc.c index befd0ced..d532d502 100644 --- a/src/rpc.c +++ b/src/rpc.c @@ -3,8 +3,10 @@ void mg_rpc_add(struct mg_rpc **head, struct mg_str method, void (*fn)(struct mg_rpc_req *), void *fn_data) { struct mg_rpc *rpc = (struct mg_rpc *) calloc(1, sizeof(*rpc)); - rpc->method = mg_strdup(method), rpc->fn = fn, rpc->fn_data = fn_data; - rpc->next = *head, *head = rpc; + if (rpc != NULL) { + rpc->method = mg_strdup(method), rpc->fn = fn, rpc->fn_data = fn_data; + rpc->next = *head, *head = rpc; + } } void mg_rpc_del(struct mg_rpc **head, void (*fn)(struct mg_rpc_req *)) {