From 015d998deb903cb10e4ff2ff6115c55dd2950b57 Mon Sep 17 00:00:00 2001
From: Jakob Olsson <Jakob Olsson>
Date: Mon, 9 Dec 2019 15:05:11 +0100
Subject: [PATCH] parse definitions as blob

---
 src/json-validator.cpp | 42 ++++++++++++++++++++++++++++++------------
 src/schema.cpp         | 13 ++++++++-----
 src/schema.h           |  4 ++--
 3 files changed, 40 insertions(+), 19 deletions(-)

diff --git a/src/json-validator.cpp b/src/json-validator.cpp
index 8dcd0d4..fa288fc 100644
--- a/src/json-validator.cpp
+++ b/src/json-validator.cpp
@@ -84,8 +84,7 @@ bool schema_validator_validate_blob(struct blob_attr *msg, const char *object, c
 	struct schema_method *s_method;
 	json schema, obj;
 	int rv = 1;
-	char *str, *s;
-
+	char *arg_str, *schema_str;
 
 	if (!initialized)
 		return true;
@@ -101,28 +100,37 @@ bool schema_validator_validate_blob(struct blob_attr *msg, const char *object, c
 		goto out;
 
 	if (type == SCHEMA_INPUT_CALL)
-		s = blobmsg_format_json(s_method->b_input, true);
+		schema_str = blobmsg_format_json(s_method->b_input, true);
 	else if (type == SCHEMA_OUTPUT_CALL)
-		s = blobmsg_format_json(s_method->b_output, true);
+		schema_str = blobmsg_format_json(s_method->b_output, true);
 	else
 		goto out;
 
-	if (!s)
+	if (!schema_str)
 		goto out;
-	schema = json::parse(s);
+	schema = json::parse(schema_str);
 
-	str = blobmsg_format_json(msg, true);
-	obj = json::parse(str);
+	arg_str = blobmsg_format_json(msg, true);
+	obj = json::parse(arg_str);
 
 	if (s_object->definitions) {
-		json definitions = json::parse(s_object->definitions);
+		char *def_str;
+
+		def_str = blobmsg_format_json(s_object->definitions, true);
+		if (!def_str)
+			goto out_definitions;
+
+		json definitions = json::parse(def_str);
 		schema += json::object_t::value_type("definitions", definitions);
+
+		free(def_str);
 	}
 
 	rv = json_object_validate_schema(obj, schema);
 
-	free(str);
-	free(s);
+out_definitions:
+	free(arg_str);
+	free(schema_str);
 out:
 	return rv;
 }
@@ -158,14 +166,24 @@ bool schema_validator_validate_jobj(struct json_object *j_object, const char *ob
 	sch = json::parse(s);
 
 	if (s_object->definitions) {
-		json definitions = json::parse(s_object->definitions);
+		char *def_str;
+
+		def_str = blobmsg_format_json(s_object->definitions, true);
+		if (!def_str)
+			goto out_definitions;
+
+		json definitions = json::parse(def_str);
 		sch += json::object_t::value_type("definitions", definitions);
+
+		free(def_str);
 	}
 
 	obj = json::parse(json_object_get_string(j_object));
 
 	rv = json_object_validate_schema(obj, sch);
 
+out_definitions:
+	free(s);
 out:
 	return rv;
 }
diff --git a/src/schema.cpp b/src/schema.cpp
index 0b2af26..a9b31db 100644
--- a/src/schema.cpp
+++ b/src/schema.cpp
@@ -124,7 +124,6 @@ schema_setup_file(const char *path)
 	struct blob_attr *root, *object;
 	int rem, rem2;
 	struct schema_object *schema_object;
-	char *definitions = NULL;
 
 	memset(&acl, 0, sizeof(acl));
 	blob_buf_init(&acl, 0);
@@ -154,8 +153,12 @@ schema_setup_file(const char *path)
 				goto out_flush;
 		}
 
-		if (!strncmp(blobmsg_name(root), "definitions", 12))
-			definitions = blobmsg_format_json(root, true);
+		if (!strncmp(blobmsg_name(root), "definitions", 12)) {
+			int len = blob_raw_len(root);
+
+			schema_object->definitions = (struct blob_attr *) calloc(1, sizeof(struct blob_attr) + len);
+			memcpy(schema_object->definitions, root, len);
+		}
 
 		if (strncmp(blobmsg_name(root), "properties", 11))
 			continue;
@@ -183,7 +186,6 @@ schema_setup_file(const char *path)
 	if (!schema_object->avl.key)
 		goto out_flush;
 
-	schema_object->definitions = definitions;
 	avl_insert(&s_ctx.schema_objects, &schema_object->avl);
 
 out:
@@ -191,6 +193,7 @@ out:
 	return;
 out_flush:
 	schema_flush_object(schema_object);
+	blob_buf_free(&acl);
 }
 
 struct schema_method *schema_get_method_schema(const char *object, const char *method)
@@ -212,7 +215,7 @@ struct schema_method *schema_get_method_schema(const char *object, const char *m
 	return NULL;
 }
 
-const char *schema_get_definitions_schema(const char *object)
+struct blob_attr *schema_get_definitions_schema(const char *object)
 {
 	struct schema_object *s_object;
 
diff --git a/src/schema.h b/src/schema.h
index 2575a06..2201b45 100644
--- a/src/schema.h
+++ b/src/schema.h
@@ -25,7 +25,7 @@ struct schema_method {
 struct schema_object {
 	char object_name[OBJECT_NAME_MAX_LEN];
 
-	char *definitions;
+	struct blob_attr *definitions;
 
 	bool regex;
 	regex_t regex_exp;
@@ -35,7 +35,7 @@ struct schema_object {
 };
 
 struct schema_method *schema_get_method_schema(const char *object, const char *method);
-const char *schema_get_definitions_schema(const char *object);
+struct blob_attr *schema_get_definitions_schema(const char *object);
 struct schema_object *schema_get_object_schema(const char *object);
 void schema_setup_json();
 void schema_flush_objects();
-- 
GitLab