# HG changeset patch # User Bram Moolenaar # Date 1555097404 -7200 # Node ID a2c598cbe220fd961f07c446d2c26b90b6993a39 # Parent 7128a1b06d119ca8b0d6e47ceb270945de2ceb95 patch 8.1.1158: json encoded string is sometimes missing the final NUL commit https://github.com/vim/vim/commit/04af19637c14045fa33b99576de4eea1e3524edb Author: Bram Moolenaar Date: Fri Apr 12 21:19:04 2019 +0200 patch 8.1.1158: json encoded string is sometimes missing the final NUL Problem: Json encoded string is sometimes missing the final NUL. Solution: Add the NUL. Also for log messages. diff --git a/src/channel.c b/src/channel.c --- a/src/channel.c +++ b/src/channel.c @@ -5877,6 +5877,7 @@ job_start( ga_concat(&ga, (char_u *)" "); ga_concat(&ga, (char_u *)argv[i]); } + ga_append(&ga, NUL); ch_log(NULL, "Starting job: %s", (char *)ga.ga_data); ga_clear(&ga); } diff --git a/src/json.c b/src/json.c --- a/src/json.c +++ b/src/json.c @@ -51,6 +51,7 @@ json_encode(typval_T *val, int options) /* Store bytes in the growarray. */ ga_init2(&ga, 1, 4000); json_encode_gap(&ga, val, options); + ga_append(&ga, NUL); return ga.ga_data; } @@ -82,6 +83,7 @@ json_encode_nr_expr(int nr, typval_T *va if (json_encode_gap(&ga, &listtv, options) == OK && (options & JSON_NL)) ga_append(&ga, '\n'); list_unref(listtv.vval.v_list); + ga_append(&ga, NUL); return ga.ga_data; } #endif diff --git a/src/testdir/test_json.vim b/src/testdir/test_json.vim --- a/src/testdir/test_json.vim +++ b/src/testdir/test_json.vim @@ -289,3 +289,10 @@ func Test_js_decode() call assert_equal(s:varl5, js_decode(s:jsl5)) endfunc + +func Test_json_encode_long() + " The growarray uses a grow size of 4000, check that a result that is exactly + " 4000 bytes long is not missing the final NUL. + let json = json_encode([repeat('a', 3996)]) + call assert_equal(4000, len(json)) +endfunc diff --git a/src/version.c b/src/version.c --- a/src/version.c +++ b/src/version.c @@ -772,6 +772,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 1158, +/**/ 1157, /**/ 1156,