From 7bed585c427cc5def85d68abedfc6bb382459d52 Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Wed, 24 May 2017 13:41:02 +0300 Subject: [PATCH] net: zoap: Fix NULL pointer access The code was setting pointer to null and then access it. Coverity-CID: 157575 Signed-off-by: Jukka Rissanen --- subsys/net/lib/zoap/zoap_link_format.c | 54 ++++++++++++++++---------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/subsys/net/lib/zoap/zoap_link_format.c b/subsys/net/lib/zoap/zoap_link_format.c index 818cb34cb0e..2b9e176e3a1 100644 --- a/subsys/net/lib/zoap/zoap_link_format.c +++ b/subsys/net/lib/zoap/zoap_link_format.c @@ -28,7 +28,7 @@ static bool match_path_uri(const char * const *path, const char *uri, u16_t len) { const char * const *p = NULL; - int i, j, plen; + int i, j, k, plen; if (!path) { return false; @@ -46,31 +46,43 @@ static bool match_path_uri(const char * const *path, return false; } + /* Go through uri and try to find a matching path */ for (i = 1; i < len; i++) { - if (!*p) { - return false; - } + while (*p) { + plen = strlen(*p); - if (!p) { + k = i; + + for (j = 0; j < plen; j++) { + if (uri[k] == '*') { + if ((k + 1) == len) { + return true; + } + } + + if (uri[k] != (*p)[j]) { + goto next; + } + + k++; + } + + if (i == (k - 1) && j == plen) { + return true; + } + + if (k == len && j == plen) { + return true; + } + + next: p++; - plen = *p ? strlen(*p) : 0; - j = 0; } + } - if (j == plen && uri[i] == '/') { - p = NULL; - continue; - } - - if (uri[i] == '*' && i + 1 == len) { - return true; - } - - if (uri[i] != (*p)[j]) { - return false; - } - - j++; + /* Did we find the resource or not */ + if (i == len && !*p) { + return false; } return true;