mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 2/4] lib: jsmn: add helper for allocating tokens
Date: Thu, 21 Sep 2023 12:24:24 +0200	[thread overview]
Message-ID: <20230921102426.1109289-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20230921102426.1109289-1-a.fatoum@pengutronix.de>

jsmn_parse can be called with a NULL buffer to determine the needed
allocation size beforehand. Most users will go on to dynamically
allocate that buffer, so add a helper that does just that and start
using it to simplify the json selftest.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 include/jsmn.h   |  6 ++++++
 lib/jsmn.c       | 36 ++++++++++++++++++++++++++++++++++++
 test/self/json.c | 33 ++++-----------------------------
 3 files changed, 46 insertions(+), 29 deletions(-)

diff --git a/include/jsmn.h b/include/jsmn.h
index 2dd1d5ffc08b..62197c2593b4 100644
--- a/include/jsmn.h
+++ b/include/jsmn.h
@@ -84,6 +84,12 @@ JSMN_API void jsmn_init(jsmn_parser *parser);
 JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len,
 			jsmntok_t *tokens, const unsigned int num_tokens);
 
+/**
+ * Like jsmn_parse, but allocates tokens dynamically.
+ */
+JSMN_API jsmntok_t *jsmn_parse_alloc(const char *js, const size_t len,
+				     unsigned int *num_tokens);
+
 static inline int jsmn_token_size(const jsmntok_t *token)
 {
 	return token->end - token->start;
diff --git a/lib/jsmn.c b/lib/jsmn.c
index 9eeed790d93d..3d2ada7b7fdc 100644
--- a/lib/jsmn.c
+++ b/lib/jsmn.c
@@ -370,6 +370,42 @@ JSMN_API void jsmn_init(jsmn_parser *parser) {
 	parser->toksuper = -1;
 }
 
+/**
+ * Parse JSON string and fill tokens into self-allocated buffer.
+ */
+JSMN_API jsmntok_t *jsmn_parse_alloc(const char *js, const size_t len,
+				     unsigned int *num_tokens)
+{
+
+	ssize_t token_count;
+	jsmn_parser parser;
+	jsmntok_t *tokens;
+	int ret;
+
+	jsmn_init(&parser);
+
+	/* Figure out how many tokens we need. */
+	ret = jsmn_parse(&parser, js, len, NULL, 0);
+	if (ret < 0)
+		return NULL;
+
+	token_count = ret;
+
+	tokens = kmalloc_array(token_count, sizeof(jsmntok_t), GFP_KERNEL);
+	if (!tokens)
+		return NULL;
+
+	jsmn_init(&parser);
+	ret = jsmn_parse(&parser, js, len, tokens, token_count);
+	if (ret < 0) {
+		free(tokens);
+		return NULL;
+	}
+
+	if (num_tokens)
+		*num_tokens = ret;
+	return tokens;
+}
 JSMN_API bool jsmn_eq(const char *val, const char *json, const jsmntok_t *token)
 {
 	size_t token_size = jsmn_token_size(token);
diff --git a/test/self/json.c b/test/self/json.c
index 54323cf4350c..d3088f1b7542 100644
--- a/test/self/json.c
+++ b/test/self/json.c
@@ -75,45 +75,20 @@ static const char json[] =
 
 static void test_json(void)
 {
-	ssize_t token_count;
 	const jsmntok_t *token;
 	jsmntok_t *tokens;
-	jsmn_parser parser;
 	char *string;
-	int ret;
 
 	total_tests++;
 
-	jsmn_init(&parser);
-
 	/* Figure out how many tokens we need. */
-	ret = jsmn_parse(&parser, json, sizeof(json), NULL, 0);
-	if (ret < 0) {
-		printf("failed to determine number of tokens: %d\n", ret);
+	tokens = jsmn_parse_alloc(json, sizeof(json), NULL);
+	if (!tokens) {
+		printf("failed to parse JSON\n");
 		failed_tests++;
 		return;
 	}
 
-	token_count = ret;
-
-	/* `token_count` is strictly less than `length` which is strictly less
-	 * than CONFIG_SYS_EEPROM_SIZE (or 8K), so we should never overflow an
-	 * int here.
-	 */
-	tokens = kmalloc_array(token_count, sizeof(jsmntok_t), GFP_KERNEL);
-	if (WARN_ON(!tokens))
-		return;
-
-	total_tests++;
-
-	jsmn_init(&parser);
-	ret = jsmn_parse(&parser, json, sizeof(json), tokens, token_count);
-	if (ret < 0) {
-		printf("failed to parse JSON with tokens: %d\n", ret);
-		failed_tests++;
-		goto out;
-	}
-
 	json_expect(json, tokens, JP("null"), JSMN_PRIMITIVE, "null");
 	json_expect(json, tokens, JP("number"), JSMN_PRIMITIVE, "0x42");
 	json_expect(json, tokens, JP("object"), JSMN_OBJECT, NULL);
@@ -131,7 +106,7 @@ static void test_json(void)
 
 	string = jsmn_strcpy(JP("string"), json, tokens);
 	if (WARN_ON(!string))
-		return;
+		goto out;
 
 	total_tests++;
 	if (strcmp(string, "Hello World\n")) {
-- 
2.39.2




  reply	other threads:[~2023-09-21 10:25 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-21 10:24 [PATCH 1/4] lib: jsmn: add and use new jsmn_token_size helper Ahmad Fatoum
2023-09-21 10:24 ` Ahmad Fatoum [this message]
2023-09-21 10:24 ` [PATCH 3/4] lib: jsmn: add case-insensitive comparison Ahmad Fatoum
2023-09-21 10:24 ` [PATCH 4/4] lib: rename json_strcpy to json_strdup Ahmad Fatoum
2023-09-21 13:03   ` Sascha Hauer
2023-09-21 13:04     ` Ahmad Fatoum
2023-09-21 13:08       ` Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230921102426.1109289-2-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox