From fee6de6a7f010e8653656b01635ddf48c513198e Mon Sep 17 00:00:00 2001 From: cpq Date: Fri, 19 Aug 2022 15:07:50 +0100 Subject: [PATCH] Allow NULL head in struct mg_rpc --- mongoose.c | 2 +- src/rpc.c | 2 +- test/unit_test.c | 13 +++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/mongoose.c b/mongoose.c index 480bb312..7186494e 100644 --- a/mongoose.c +++ b/mongoose.c @@ -3580,7 +3580,7 @@ void mg_rpc_process(struct mg_rpc_req *r) { int len, off = mg_json_get(r->frame, "$.method", &len); if (off > 0 && r->frame.ptr[off] == '"') { struct mg_str m = mg_str_n(&r->frame.ptr[off + 1], (size_t) len - 2); - struct mg_rpc *h = *(struct mg_rpc **) r->head; + struct mg_rpc *h = r->head == NULL ? NULL : *r->head; while (h != NULL && !mg_match(m, h->method, NULL)) h = h->next; if (h != NULL) { r->rpc = h; diff --git a/src/rpc.c b/src/rpc.c index c513a555..c35e9c5e 100644 --- a/src/rpc.c +++ b/src/rpc.c @@ -24,7 +24,7 @@ void mg_rpc_process(struct mg_rpc_req *r) { int len, off = mg_json_get(r->frame, "$.method", &len); if (off > 0 && r->frame.ptr[off] == '"') { struct mg_str m = mg_str_n(&r->frame.ptr[off + 1], (size_t) len - 2); - struct mg_rpc *h = *(struct mg_rpc **) r->head; + struct mg_rpc *h = r->head == NULL ? NULL : *r->head; while (h != NULL && !mg_match(m, h->method, NULL)) h = h->next; if (h != NULL) { r->rpc = h; diff --git a/test/unit_test.c b/test/unit_test.c index 124aa659..dda44f1a 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -2524,6 +2524,19 @@ static void test_rpc(void) { mg_iobuf_free(&io); } + { + const char *resp = + "{\"id\":true,\"error\":{\"code\":-32601,\"message\":\"foo not " + "found\"}}"; + req.frame = mg_str("{\"id\": true,\"method\":\"foo\"}"); + req.head = NULL; + mg_rpc_process(&req); + // MG_INFO(("-> %s", io.buf)); + ASSERT(strcmp((char *) io.buf, resp) == 0); + mg_iobuf_free(&io); + req.head = &head; + } + { const char *resp = "{\"error\":{\"code\":-32700,\"message\":\"haha\"}}"; req.frame = mg_str("haha");