Improve management of agent names
authorJose Bollo <jose.bollo@iot.bzh>
Wed, 30 Oct 2019 16:15:23 +0000 (17:15 +0100)
committerJose Bollo <jose.bollo@iot.bzh>
Mon, 4 Nov 2019 11:03:31 +0000 (12:03 +0100)
Signed-off-by: Jose Bollo <jose.bollo@iot.bzh>
src/cyn.c
src/cynagora.c
src/names.c
src/names.h

index b1477f4..f8848c3 100644 (file)
--- a/src/cyn.c
+++ b/src/cyn.c
@@ -367,17 +367,27 @@ static
 struct agent *
 search_agent(
        const char *name,
-       size_t length,
+       uint8_t length,
        struct agent ***ppprev
 ) {
        struct agent *it, **pprev;
 
        pprev = &agents;
-       while((it = *pprev)
-         &&  ((uint8_t)length != it->len || memcmp(it->name, name, length)))
+       it = agents;
+       while(it && length > it->len) {
                pprev = &it->next;
+               it = it->next;
+       }
+       while(it && length == it->len) {
+               if (!memcmp(it->name, name, length)) {
+                       *ppprev = pprev;
+                       return it;
+               }
+               pprev = &it->next;
+               it = it->next;
+       }
        *ppprev = pprev;
-       return it;
+       return NULL;
 }
 
 /**
@@ -392,12 +402,15 @@ struct agent*
 required_agent(
        const char *value
 ) {
-       struct agent **pprev;
+       struct agent **pprev, *agent;
        size_t length;
 
        for (length = 0 ; length <= UINT8_MAX && value[length] ; length++)
-               if (value[length] == AGENT_SEPARATOR_CHARACTER)
-                       return search_agent(value, length, &pprev);
+               if (value[length] == AGENT_SEPARATOR_CHARACTER) {
+                       agent = search_agent(value, (uint8_t)length, &pprev);
+                       if (agent)
+                               return agent;
+               }
        return NULL;
 }
 
@@ -565,7 +578,7 @@ cyn_agent_add(
        void *closure
 ) {
        struct agent *agent, **pprev;
-       size_t length;
+       uint8_t length;
 
        /* compute and check name */
        length = agent_check_name(name);
@@ -578,16 +591,16 @@ cyn_agent_add(
                return -EEXIST;
 
        /* allocates the memory */
-       agent = malloc(sizeof *agent + 1 + length);
+       agent = malloc(sizeof *agent + 1 + (size_t)length);
        if (!agent)
                return -ENOMEM;
 
        /* initialize the agent */
-       agent->next = 0;
        agent->agent_cb = agent_cb;
        agent->closure = closure;
-       agent->len = (uint8_t)length;
-       memcpy(agent->name, name, length + 1);
+       agent->len = length;
+       memcpy(agent->name, name, 1 + (size_t)length);
+       agent->next = *pprev;
        *pprev = agent;
 
        return 0;
@@ -599,7 +612,7 @@ cyn_agent_remove_by_name(
        const char *name
 ) {
        struct agent *agent, **pprev;
-       size_t length;
+       uint8_t length;
 
        /* compute and check name length */
        length = agent_check_name(name);
index 7f3f001..16d953f 100644 (file)
@@ -1430,7 +1430,7 @@ cynagora_agent_create(
                return -EPERM;
 
        /* check name */
-       length = agent_check_name(name);
+       length = (size_t)agent_check_name(name);
        if (!length)
                return -EINVAL;
 
index f9a7817..089df5f 100644 (file)
  * @param name the name to check
  * @return the length of the name or zero if invalid
  */
-size_t
+uint8_t
 agent_check_name(
        const char *name
 ) {
        char c;
-       size_t length = 0;
+       uint8_t length = 0;
+
        if (name) {
                while ((c = name[length])) {
-                       if (length > UINT8_MAX
-                        || (!isalnum(c) && !strchr("@_-$", c))) {
+                       if (!isalnum(c) && !strchr("@_-$", c)) {
                                length = 0;
                                break;
                        }
-                       length++;
+                       if (!++length)
+                               break;
                }
        }
        return length;
index 30f4a99..9792600 100644 (file)
@@ -27,7 +27,7 @@
  * @return the length of the name or zero if invalid
  */
 extern
-size_t
+uint8_t
 agent_check_name(
        const char *name
 );