]> git.ipfire.org Git - thirdparty/curl.git/commit
tool_writeout_json: fix JSON encoding of non-ascii bytes
authorEmanuele Torre <torreemanuele6@gmail.com>
Fri, 1 Dec 2023 00:51:47 +0000 (01:51 +0100)
committerDaniel Stenberg <daniel@haxx.se>
Fri, 1 Dec 2023 09:39:13 +0000 (10:39 +0100)
commit6c7da8156173bb28cb019a56f83f9f2fcccb8ee6
tree12df571ba501e3c0077900dede0daf9947625331
parent83e4d61981efe94b7476cb36424fd400adcca45e
tool_writeout_json: fix JSON encoding of non-ascii bytes

char variables if unspecified can be either signed or unsigned depending
on the platform according to the C standard; in most platforms, they are
signed.

This meant that the  *i<32  waas always true for bytes with the top bit
set. So they were always getting encoded as \uXXXX, and then since they
were also signed negative, they were getting extended with 1s causing
'\xe2' to be expanded to \uffffffe2, for example:

  $ curl --variable 'v=“' --expand-write-out '{{v:json}}\n' file:///dev/null
  \uffffffe2\uffffff80\uffffff9c

I fixed this bug by making the code use explicitly unsigned char*
variables instead of char* variables.

Test 268 verifies

Reported-by: iconoclasthero
Closes #12434
src/tool_writeout_json.c
tests/data/Makefile.inc
tests/data/test268 [new file with mode: 0644]