From: Emanuele Torre Date: Fri, 1 Dec 2023 00:51:47 +0000 (+0100) Subject: tool_writeout_json: fix JSON encoding of non-ascii bytes X-Git-Tag: curl-8_5_0~21 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6c7da8156173bb28cb019a56f83f9f2fcccb8ee6;p=thirdparty%2Fcurl.git 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 --- diff --git a/src/tool_writeout_json.c b/src/tool_writeout_json.c index 7bc74a2697..4ed6b93fb7 100644 --- a/src/tool_writeout_json.c +++ b/src/tool_writeout_json.c @@ -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++) { diff --git a/tests/data/Makefile.inc b/tests/data/Makefile.inc index 2608e16d6e..de13c525e8 100644 --- a/tests/data/Makefile.inc +++ b/tests/data/Makefile.inc @@ -51,7 +51,7 @@ test226 test227 test228 test229 test230 test231 test232 test233 test234 \ test235 test236 test237 test238 test239 test240 test241 test242 test243 \ test244 test245 test246 test247 test248 test249 test250 test251 test252 \ test253 test254 test255 test256 test257 test258 test259 test260 test261 \ -test262 test263 test264 test265 test266 test267 test269 test270 \ +test262 test263 test264 test265 test266 test267 test268 test269 test270 \ test271 test272 test273 test274 test275 test276 test277 test278 test279 \ test280 test281 test282 test283 test284 test285 test286 test287 test288 \ test289 test290 test291 test292 test293 test294 test295 test296 test297 \ diff --git a/tests/data/test268 b/tests/data/test268 new file mode 100644 index 0000000000..3a1ab6a9b5 --- /dev/null +++ b/tests/data/test268 @@ -0,0 +1,59 @@ + + + +HTTP +variables + + + +# +# Server-side + + +HTTP/1.1 200 OK +Date: Tue, 09 Nov 2010 14:49:00 GMT +Server: test-server/fake +Last-Modified: Tue, 13 Jun 2000 12:10:00 GMT +ETag: "21025-dc7-39462498" +Accept-Ranges: bytes +Content-Length: 6 +Connection: close +Content-Type: text/html +Funny-head: yesyes + +-foo- + + + +# +# Client-side + + +http + + +JSON encoding of unicode string + + +%hex[%e2%80%9c]hex% + + +http://%HOSTIP:%HTTPPORT/%TESTNUMBER --variable hello@%LOGDIR/junk --expand-data {{hello:json}} + + + +# +# Verify data after the test has been "shot" + + +POST /%TESTNUMBER HTTP/1.1 +Host: %HOSTIP:%HTTPPORT +User-Agent: curl/%VERSION +Accept: */* +Content-Length: 3 +Content-Type: application/x-www-form-urlencoded + +%hex[%e2%80%9c]hex% + + +