[LTP] [PATCH v2 10/15] ujson: reader: Parse escape sequences in object IDs

Andrea Cervesato andrea.cervesato@suse.de
Tue Oct 13 14:02:06 CEST 2026


From: Andrea Cervesato <andrea.cervesato@suse.com>

copy_id_str() copied characters verbatim without handling escape
sequences, which caused escaped quotes \" to prematurely terminate
the key and left other escape sequences such as \uXXXX unparsed.
Reuse copy_str() to parse object ID strings with full escape support.

As a side effect, too long and unterminated IDs now report the generic
copy_str() errors ("String buffer too short!", "Unterminated string")
instead of the ID-specific ones. Also set res->type to UJSON_VOID on
ID-parsing errors to prevent iterating with stale values on failure.

Fixes: c50a0e0751c8 ("libs: Vendor ujson library")
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 libs/ujson/ujson_reader.c | 32 +++++++++-----------------------
 1 file changed, 9 insertions(+), 23 deletions(-)

diff --git a/libs/ujson/ujson_reader.c b/libs/ujson/ujson_reader.c
index 890d07b35..21425f848 100644
--- a/libs/ujson/ujson_reader.c
+++ b/libs/ujson/ujson_reader.c
@@ -258,32 +258,14 @@ static int copy_str(ujson_reader *buf, char *str, size_t len)
 
 static int copy_id_str(ujson_reader *buf, char *str, size_t len)
 {
-	size_t pos = 0;
-
 	if (eatws(buf))
 		goto err0;
 
-	if (!eatb(buf, '"'))
+	if (peekb(buf) != '"')
 		goto err0;
 
-	for (;;) {
-		if (buf_empty(buf)) {
-			ujson_err(buf, "Unterminated ID string");
-			return 1;
-		}
-
-		if (eatb(buf, '"')) {
-			str[pos] = 0;
-			break;
-		}
-
-		if (pos >= len-1) {
-			ujson_err(buf, "ID string too long");
-			return 1;
-		}
-
-		str[pos++] = getb(buf);
-	}
+	if (copy_str(buf, str, len))
+		return 1;
 
 	if (eatws(buf))
 		goto err1;
@@ -706,8 +688,10 @@ static int skip_obj_val(ujson_reader *buf)
 
 static int obj_next(ujson_reader *buf, struct ujson_val *res)
 {
-	if (copy_id_str(buf, res->id, sizeof(res->id)))
+	if (copy_id_str(buf, res->id, sizeof(res->id))) {
+		res->type = UJSON_VOID;
 		return 0;
+	}
 
 	return get_value(buf, res);
 }
@@ -732,8 +716,10 @@ static int obj_next_filter(ujson_reader *buf, struct ujson_val *res,
 	const struct ujson_obj_attr *attr;
 
 	for (;;) {
-		if (copy_id_str(buf, res->id, sizeof(res->id)))
+		if (copy_id_str(buf, res->id, sizeof(res->id))) {
+			res->type = UJSON_VOID;
 			return 0;
+		}
 
 		res->idx = obj ? ujson_obj_lookup(obj, res->id) : (size_t)-1;
 

-- 
2.51.0



More information about the ltp mailing list