From cfa9990c41f5326c9c280dacebf453d69afe8dd6 Mon Sep 17 00:00:00 2001 From: Robert Chou Date: Mon, 28 Aug 2017 17:06:48 +0800 Subject: [PATCH] net: lwm2m: return 4.04 when URI is not composed by digits Modify zoap_options_to_path() to return error when URI contains character other than digits and return 4.04 NOT FOUND to caller. PATH such as "/1a/2/3" was treated as "/1/2/3" after parsring which is incorrect. Signed-off-by: Robert Chou --- subsys/net/lib/lwm2m/lwm2m_engine.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/subsys/net/lib/lwm2m/lwm2m_engine.c b/subsys/net/lib/lwm2m/lwm2m_engine.c index 2af88a847cb..4cf103ec1ea 100644 --- a/subsys/net/lib/lwm2m/lwm2m_engine.c +++ b/subsys/net/lib/lwm2m/lwm2m_engine.c @@ -687,37 +687,39 @@ static u16_t atou16(u8_t *buf, u16_t buflen, u16_t *len) return val; } -static void zoap_options_to_path(struct zoap_option *opt, int options_count, +static int zoap_options_to_path(struct zoap_option *opt, int options_count, struct lwm2m_obj_path *path) { u16_t len; path->level = options_count; path->obj_id = atou16(opt[0].value, opt[0].len, &len); - if (len == 0) { + if (len == 0 || opt[0].len != len) { path->level = 0; } if (path->level > 1) { path->obj_inst_id = atou16(opt[1].value, opt[1].len, &len); - if (len == 0) { + if (len == 0 || opt[1].len != len) { path->level = 1; } } if (path->level > 2) { path->res_id = atou16(opt[2].value, opt[2].len, &len); - if (len == 0) { + if (len == 0 || opt[2].len != len) { path->level = 2; } } if (path->level > 3) { path->res_inst_id = atou16(opt[3].value, opt[3].len, &len); - if (len == 0) { + if (len == 0 || opt[3].len != len) { path->level = 3; } } + + return options_count == path->level ? 0 : -EINVAL; } static struct lwm2m_message *find_msg(struct zoap_pending *pending, @@ -2306,7 +2308,11 @@ static int handle_request(struct zoap_packet *request, strncmp(options[1].value, "core", 4) == 0)) { discover = true; } else { - zoap_options_to_path(options, r, &path); + r = zoap_options_to_path(options, r, &path); + if (r < 0) { + r = -ENOENT; + goto error; + } } /* read Content Format */