All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH BlueZ v2] mesh: Fix reading/writing key indices
@ 2019-10-20 21:29 Inga Stotland
  2019-10-28 16:49 ` Gix, Brian
  0 siblings, 1 reply; 2+ messages in thread
From: Inga Stotland @ 2019-10-20 21:29 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: brian.gix, Inga Stotland

This fixes inconsistency when reading/writing NetKey and AppKey
indices to/from JSON config storage:
	- when writing, convert an integer to hex string
	- when reading, convert hex string to uint16 integer
---
 mesh/mesh-config-json.c | 67 ++++++++++++++++++++++++-----------------
 1 file changed, 40 insertions(+), 27 deletions(-)

diff --git a/mesh/mesh-config-json.c b/mesh/mesh-config-json.c
index df58cbd7d..865fbdf07 100644
--- a/mesh/mesh-config-json.c
+++ b/mesh/mesh-config-json.c
@@ -45,7 +45,7 @@
 #define MIN_SEQ_CACHE_VALUE	(2 * 32)
 #define MIN_SEQ_CACHE_TIME	(5 * 60)
 
-#define CHECK_KEY_IDX_RANGE(x) (((x) >= 0) && ((x) <= 4095))
+#define CHECK_KEY_IDX_RANGE(x) ((x) <= 4095)
 
 struct mesh_config {
 	json_object *jnode;
@@ -263,13 +263,16 @@ static json_object *get_key_object(json_object *jarray, uint16_t idx)
 
 	for (i = 0; i < sz; ++i) {
 		json_object *jentry, *jvalue;
-		uint32_t jidx;
+		const char *str;
+		uint16_t jidx;
 
 		jentry = json_object_array_get_idx(jarray, i);
 		if (!json_object_object_get_ex(jentry, "index", &jvalue))
 			return NULL;
 
-		jidx = json_object_get_int(jvalue);
+		str = json_object_get_string(jvalue);
+		if (sscanf(str, "%04hx", &jidx) != 1)
+			return NULL;
 
 		if (jidx == idx)
 			return jentry;
@@ -278,6 +281,28 @@ static json_object *get_key_object(json_object *jarray, uint16_t idx)
 	return NULL;
 }
 
+static bool get_key_index(json_object *jobj, const char *keyword,
+								uint16_t *index)
+{
+	uint16_t idx;
+	json_object *jvalue;
+	const char *str;
+
+	if (!json_object_object_get_ex(jobj, keyword, &jvalue))
+		return false;
+
+	str = json_object_get_string(jvalue);
+
+	if (sscanf(str, "%04hx", &idx) != 1)
+		return false;
+
+	if (!CHECK_KEY_IDX_RANGE(idx))
+		return false;
+
+	*index = (uint16_t) idx;
+	return true;
+}
+
 static json_object *jarray_key_del(json_object *jarray, int16_t idx)
 {
 	json_object *jarray_new;
@@ -288,16 +313,13 @@ static json_object *jarray_key_del(json_object *jarray, int16_t idx)
 		return NULL;
 
 	for (i = 0; i < sz; ++i) {
-		json_object *jentry, *jvalue;
+		json_object *jentry;
+		uint16_t nidx;
 
 		jentry = json_object_array_get_idx(jarray, i);
 
-		if (json_object_object_get_ex(jentry, "index", &jvalue)) {
-			int tmp = json_object_get_int(jvalue);
-
-			if (tmp == idx)
-				continue;
-		}
+		if (get_key_index(jentry, "index", &nidx) && nidx == idx)
+			continue;
 
 		json_object_get(jentry);
 		json_object_array_add(jarray_new, jentry);
@@ -419,21 +441,6 @@ static bool read_device_key(json_object *jobj, uint8_t key_buf[16])
 	return true;
 }
 
-static bool get_key_index(json_object *jobj, const char *keyword,
-								uint16_t *index)
-{
-	int idx;
-
-	if (!get_int(jobj, keyword, &idx))
-		return false;
-
-	if (!CHECK_KEY_IDX_RANGE(idx))
-		return false;
-
-	*index = (uint16_t) idx;
-	return true;
-}
-
 static bool read_app_keys(json_object *jobj, struct mesh_config_node *node)
 {
 	json_object *jarray;
@@ -570,6 +577,7 @@ bool mesh_config_net_key_add(struct mesh_config *cfg, uint16_t idx,
 
 	jnode = cfg->jnode;
 
+	l_debug("netKey %4.4x", idx);
 	json_object_object_get_ex(jnode, "netKeys", &jarray);
 	if (jarray)
 		jentry = get_key_object(jarray, idx);
@@ -965,14 +973,19 @@ static bool parse_bindings(json_object *jarray, struct mesh_config_model *mod)
 	mod->bindings = l_new(uint16_t, cnt);
 
 	for (i = 0; i < cnt; ++i) {
-		int idx;
+		uint16_t idx;
+		const char *str;
 		json_object *jvalue;
 
 		jvalue = json_object_array_get_idx(jarray, i);
 		if (!jvalue)
 			return false;
 
-		idx = json_object_get_int(jvalue);
+		str = json_object_get_string(jvalue);
+
+		if (sscanf(str, "%04hx", &idx) != 1)
+			return false;
+
 		if (!CHECK_KEY_IDX_RANGE(idx))
 			return false;
 
-- 
2.21.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH BlueZ v2] mesh: Fix reading/writing key indices
  2019-10-20 21:29 [PATCH BlueZ v2] mesh: Fix reading/writing key indices Inga Stotland
@ 2019-10-28 16:49 ` Gix, Brian
  0 siblings, 0 replies; 2+ messages in thread
From: Gix, Brian @ 2019-10-28 16:49 UTC (permalink / raw)
  To: linux-bluetooth, Stotland, Inga

Applied, Thanks.

On Sun, 2019-10-20 at 14:29 -0700, Inga Stotland wrote:
> This fixes inconsistency when reading/writing NetKey and AppKey
> indices to/from JSON config storage:
> 	- when writing, convert an integer to hex string
> 	- when reading, convert hex string to uint16 integer
> ---
>  mesh/mesh-config-json.c | 67 ++++++++++++++++++++++++-----------------
>  1 file changed, 40 insertions(+), 27 deletions(-)
> 
> diff --git a/mesh/mesh-config-json.c b/mesh/mesh-config-json.c
> index df58cbd7d..865fbdf07 100644
> --- a/mesh/mesh-config-json.c
> +++ b/mesh/mesh-config-json.c
> @@ -45,7 +45,7 @@
>  #define MIN_SEQ_CACHE_VALUE	(2 * 32)
>  #define MIN_SEQ_CACHE_TIME	(5 * 60)
>  
> -#define CHECK_KEY_IDX_RANGE(x) (((x) >= 0) && ((x) <= 4095))
> +#define CHECK_KEY_IDX_RANGE(x) ((x) <= 4095)
>  
>  struct mesh_config {
>  	json_object *jnode;
> @@ -263,13 +263,16 @@ static json_object *get_key_object(json_object *jarray, uint16_t idx)
>  
>  	for (i = 0; i < sz; ++i) {
>  		json_object *jentry, *jvalue;
> -		uint32_t jidx;
> +		const char *str;
> +		uint16_t jidx;
>  
>  		jentry = json_object_array_get_idx(jarray, i);
>  		if (!json_object_object_get_ex(jentry, "index", &jvalue))
>  			return NULL;
>  
> -		jidx = json_object_get_int(jvalue);
> +		str = json_object_get_string(jvalue);
> +		if (sscanf(str, "%04hx", &jidx) != 1)
> +			return NULL;
>  
>  		if (jidx == idx)
>  			return jentry;
> @@ -278,6 +281,28 @@ static json_object *get_key_object(json_object *jarray, uint16_t idx)
>  	return NULL;
>  }
>  
> +static bool get_key_index(json_object *jobj, const char *keyword,
> +								uint16_t *index)
> +{
> +	uint16_t idx;
> +	json_object *jvalue;
> +	const char *str;
> +
> +	if (!json_object_object_get_ex(jobj, keyword, &jvalue))
> +		return false;
> +
> +	str = json_object_get_string(jvalue);
> +
> +	if (sscanf(str, "%04hx", &idx) != 1)
> +		return false;
> +
> +	if (!CHECK_KEY_IDX_RANGE(idx))
> +		return false;
> +
> +	*index = (uint16_t) idx;
> +	return true;
> +}
> +
>  static json_object *jarray_key_del(json_object *jarray, int16_t idx)
>  {
>  	json_object *jarray_new;
> @@ -288,16 +313,13 @@ static json_object *jarray_key_del(json_object *jarray, int16_t idx)
>  		return NULL;
>  
>  	for (i = 0; i < sz; ++i) {
> -		json_object *jentry, *jvalue;
> +		json_object *jentry;
> +		uint16_t nidx;
>  
>  		jentry = json_object_array_get_idx(jarray, i);
>  
> -		if (json_object_object_get_ex(jentry, "index", &jvalue)) {
> -			int tmp = json_object_get_int(jvalue);
> -
> -			if (tmp == idx)
> -				continue;
> -		}
> +		if (get_key_index(jentry, "index", &nidx) && nidx == idx)
> +			continue;
>  
>  		json_object_get(jentry);
>  		json_object_array_add(jarray_new, jentry);
> @@ -419,21 +441,6 @@ static bool read_device_key(json_object *jobj, uint8_t key_buf[16])
>  	return true;
>  }
>  
> -static bool get_key_index(json_object *jobj, const char *keyword,
> -								uint16_t *index)
> -{
> -	int idx;
> -
> -	if (!get_int(jobj, keyword, &idx))
> -		return false;
> -
> -	if (!CHECK_KEY_IDX_RANGE(idx))
> -		return false;
> -
> -	*index = (uint16_t) idx;
> -	return true;
> -}
> -
>  static bool read_app_keys(json_object *jobj, struct mesh_config_node *node)
>  {
>  	json_object *jarray;
> @@ -570,6 +577,7 @@ bool mesh_config_net_key_add(struct mesh_config *cfg, uint16_t idx,
>  
>  	jnode = cfg->jnode;
>  
> +	l_debug("netKey %4.4x", idx);
>  	json_object_object_get_ex(jnode, "netKeys", &jarray);
>  	if (jarray)
>  		jentry = get_key_object(jarray, idx);
> @@ -965,14 +973,19 @@ static bool parse_bindings(json_object *jarray, struct mesh_config_model *mod)
>  	mod->bindings = l_new(uint16_t, cnt);
>  
>  	for (i = 0; i < cnt; ++i) {
> -		int idx;
> +		uint16_t idx;
> +		const char *str;
>  		json_object *jvalue;
>  
>  		jvalue = json_object_array_get_idx(jarray, i);
>  		if (!jvalue)
>  			return false;
>  
> -		idx = json_object_get_int(jvalue);
> +		str = json_object_get_string(jvalue);
> +
> +		if (sscanf(str, "%04hx", &idx) != 1)
> +			return false;
> +
>  		if (!CHECK_KEY_IDX_RANGE(idx))
>  			return false;
>  

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-10-28 16:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-20 21:29 [PATCH BlueZ v2] mesh: Fix reading/writing key indices Inga Stotland
2019-10-28 16:49 ` Gix, Brian

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.