diff options
author | Jose Bollo <jose.bollo@iot.bzh> | 2019-10-30 17:15:23 +0100 |
---|---|---|
committer | Jose Bollo <jose.bollo@iot.bzh> | 2019-11-04 12:03:31 +0100 |
commit | 195d93caaec446b2cbca2059b41803498a0b30c9 (patch) | |
tree | 31e9ec5ca099d7261e9fd82e9008ab64eb3b82af | |
parent | 3ab6b486ca043d237b89fe530cb10ca89b489344 (diff) |
Improve management of agent names
Signed-off-by: Jose Bollo <jose.bollo@iot.bzh>
-rw-r--r-- | src/cyn.c | 39 | ||||
-rw-r--r-- | src/cynagora.c | 2 | ||||
-rw-r--r-- | src/names.c | 11 | ||||
-rw-r--r-- | src/names.h | 2 |
4 files changed, 34 insertions, 20 deletions
@@ -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); diff --git a/src/cynagora.c b/src/cynagora.c index 7f3f001..16d953f 100644 --- a/src/cynagora.c +++ b/src/cynagora.c @@ -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; diff --git a/src/names.c b/src/names.c index f9a7817..089df5f 100644 --- a/src/names.c +++ b/src/names.c @@ -31,20 +31,21 @@ * @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; diff --git a/src/names.h b/src/names.h index 30f4a99..9792600 100644 --- a/src/names.h +++ b/src/names.h @@ -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 ); |