]> granicus.if.org Git - vim/commitdiff
patch 8.1.1158: json encoded string is sometimes missing the final NUL v8.1.1158
authorBram Moolenaar <Bram@vim.org>
Fri, 12 Apr 2019 19:19:04 +0000 (21:19 +0200)
committerBram Moolenaar <Bram@vim.org>
Fri, 12 Apr 2019 19:19:04 +0000 (21:19 +0200)
Problem:    Json encoded string is sometimes missing the final NUL.
Solution:   Add the NUL.  Also for log messages.

src/channel.c
src/json.c
src/testdir/test_json.vim
src/version.c

index 9ce47d3a470d0bfe429c8b8aa72d2df56b800c81..dd47c5b02b5bcbbcd542e0b1d5dfd0b197e94bb4 100644 (file)
@@ -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);
     }
index d3c4817be52625ae3530893c973b742b8d91ffb9..9fb6af0defd1ea4e8706115868a1107913526f92 100644 (file)
@@ -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 *val, int options)
     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
index fb3d9927e4262e3de1c6d98f1b1c49dfd9859b3a..e16a7f03076cd6adaecfd2877d9ed354324952ed 100644 (file)
@@ -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
index 4daa41e24a295d82ff5e5bb99406dadc2230ef6c..7e5d406efd3f127cce0fbbee07937745d8ac68ed 100644 (file)
@@ -771,6 +771,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1158,
 /**/
     1157,
 /**/