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
This commit is contained in:
Emanuele Torre 2023-12-01 01:51:47 +01:00 committed by Daniel Stenberg
parent 83e4d61981
commit 6c7da81561
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
3 changed files with 62 additions and 3 deletions

View file

@ -41,8 +41,8 @@
int jsonquoted(const char *in, size_t len,
struct curlx_dynbuf *out, bool lowercase)
{
const char *i = in;
const char *in_end = &in[len];
const unsigned char *i = (unsigned char *)in;
const unsigned char *in_end = &i[len];
CURLcode result = CURLE_OK;
for(; (i < in_end) && !result; i++) {