Linux Test Project
 help / color / mirror / Atom feed
From: Cyril Hrubis <chrubis@suse.cz>
To: Andrea Cervesato <andrea.cervesato@suse.de>
Cc: Linux Test Project <ltp@lists.linux.it>
Subject: Re: [LTP] [PATCH 05/15] ujson: reader: Combine UTF-16 surrogate pairs
Date: Mon, 5 Oct 2026 17:36:18 +0200	[thread overview]
Message-ID: <asPD8h8o2BBeLMUS@yuki.lan> (raw)
In-Reply-To: <20261005-fix_core_lib_deepseek-v1-5-fec334ce8e80@suse.com>

Hi!
> RFC 8259 specifies that characters outside the BMP may be represented
> as a surrogate pair (\uD800-\uDBFF followed by \uDC00-\uDFFF).
> Currently, each \uXXXX escape is converted independently to UTF-8,
> producing invalid surrogate code point sequences instead of combining
> them into a single UTF-8 sequence. Lone surrogates are also improperly
> accepted. Combine valid surrogate pairs and reject unpaired surrogates.
> 
> Also return the UTF-8 length instead of the code point when no output
> buffer is given. The caller treats 0 as failure, so \u0000 made
> copy_str() fail without setting an error message.
> 
> Fixes: c50a0e0751c8 ("libs: Vendor ujson library")
> Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
> ---
>  libs/ujson/ujson_reader.c | 28 +++++++++++++++++++++++++++-
>  1 file changed, 27 insertions(+), 1 deletion(-)
> 
> diff --git a/libs/ujson/ujson_reader.c b/libs/ujson/ujson_reader.c
> index 2219c5801..0975ca1a1 100644
> --- a/libs/ujson/ujson_reader.c
> +++ b/libs/ujson/ujson_reader.c
> @@ -134,8 +134,34 @@ static unsigned int parse_ucode_esc(ujson_reader *buf, char *str,
>  	if (ucode < 0)
>  		return 0;
>  
> +	if (ucode >= 0xd800 && ucode <= 0xdbff) {
> +		int32_t low;
> +
> +		if (peekb(buf) != '\\' || peekb_off(buf, 1) != 'u') {
> +			ujson_err(buf, "Unpaired high surrogate");
> +			return 0;
> +		}
> +
> +		getb(buf);
> +		getb(buf);
> +
> +		low = parse_ucode_cp(buf);
> +		if (low < 0)
> +			return 0;
> +
> +		if (low < 0xdc00 || low > 0xdfff) {
> +			ujson_err(buf, "Invalid low surrogate in escape sequence");
> +			return 0;
> +		}
> +
> +		ucode = 0x10000 + (((ucode - 0xd800) << 10) | (low - 0xdc00));
> +	} else if (ucode >= 0xdc00 && ucode <= 0xdfff) {
> +		ujson_err(buf, "Unpaired low surrogate");
> +		return 0;
> +	}

I would put this code first, not in an elase branch.


        if (ucode >= 0xdc00 && ucode <= 0xdfff) {
		ujson_err(buf, "Unpaired low surrogate");
		return 0;
	}

	if (ucode >= 0xd800 && ucode <= 0xdbff) {
	    ...
	}

Also it's unlikely that we will hit this in LTP, unless someone adds
emojis or hieroglyphs in test strings.

>  	if (!str)
> -		return ucode;
> +		return ujson_utf8_bytes(ucode);

This is a good bugfix, should have gone in a separate patch. Without
this we may overshoot the required size for the buffer by order of a
magnitude.

>  	if (ujson_utf8_bytes(ucode) + 1 >= len - off) {
>  		ujson_err(buf, "String buffer too short!");
> 
> -- 
> 2.51.0
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

  reply	other threads:[~2026-10-05 15:36 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-05 13:25 [LTP] [PATCH 00/15] Fix ujson bugs Andrea Cervesato
2026-10-05 13:25 ` [LTP] [PATCH 01/15] ujson: Fix 4-byte UTF-8 sequence decoding Andrea Cervesato
2026-10-05 14:57   ` Cyril Hrubis
2026-10-05 13:25 ` [LTP] [PATCH 02/15] ujson: writer: Fix OOB read on invalid UTF-8 Andrea Cervesato
2026-10-05 15:04   ` Cyril Hrubis
2026-10-05 13:25 ` [LTP] [PATCH 03/15] ujson: reader: Prevent infinite loop on EOF Andrea Cervesato
2026-10-05 15:12   ` Cyril Hrubis
2026-10-05 18:06   ` [LTP] ujson: Fix 4-byte UTF-8 sequence decoding linuxtestproject.agent
2026-10-05 13:25 ` [LTP] [PATCH 04/15] ujson: writer: Fix open() return value check Andrea Cervesato
2026-10-05 15:15   ` Cyril Hrubis
2026-10-05 13:25 ` [LTP] [PATCH 05/15] ujson: reader: Combine UTF-16 surrogate pairs Andrea Cervesato
2026-10-05 15:36   ` Cyril Hrubis [this message]
2026-10-05 13:25 ` [LTP] [PATCH 06/15] ujson: reader: Check for signed integer overflow Andrea Cervesato
2026-10-09 11:59   ` Cyril Hrubis
2026-10-05 13:25 ` [LTP] [PATCH 07/15] ujson: writer: Return error when newline() fails Andrea Cervesato
2026-10-09 13:44   ` Cyril Hrubis
2026-10-05 13:25 ` [LTP] [PATCH 08/15] ujson: writer: Check return value in ESC_FLUSH Andrea Cervesato
2026-10-09 14:01   ` Cyril Hrubis
2026-10-05 13:25 ` [LTP] [PATCH 09/15] ujson: reader: Do not increment depth on overflow Andrea Cervesato
2026-10-09 14:15   ` Cyril Hrubis
2026-10-05 13:25 ` [LTP] [PATCH 10/15] ujson: reader: Parse escape sequences in object IDs Andrea Cervesato
2026-10-05 13:25 ` [LTP] [PATCH 11/15] ujson: reader: Support files with zero lseek size Andrea Cervesato
2026-10-09 15:21   ` Cyril Hrubis
2026-10-05 13:25 ` [LTP] [PATCH 12/15] ujson: reader: Reject \u0000 escape in strings Andrea Cervesato
2026-10-09 15:24   ` Cyril Hrubis
2026-10-05 13:25 ` [LTP] [PATCH 13/15] ujson: writer: Set errno when a flush fails Andrea Cervesato
2026-10-09 15:46   ` Cyril Hrubis
2026-10-05 13:25 ` [LTP] [PATCH 14/15] ujson: writer: Advance buffer on short write Andrea Cervesato
2026-10-09 15:52   ` Cyril Hrubis
2026-10-05 13:25 ` [LTP] [PATCH 15/15] ujson: writer: Flush buffer before direct writes Andrea Cervesato
2026-10-09 16:05   ` Cyril Hrubis

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=asPD8h8o2BBeLMUS@yuki.lan \
    --to=chrubis@suse.cz \
    --cc=andrea.cervesato@suse.de \
    --cc=ltp@lists.linux.it \
    /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