Skip to content

Commit

Permalink
patch 8.1.1158: json encoded string is sometimes missing the final NUL
Browse files Browse the repository at this point in the history
Problem:    Json encoded string is sometimes missing the final NUL.
Solution:   Add the NUL.  Also for log messages.
  • Loading branch information
brammool committed Apr 12, 2019
1 parent f9b89b4 commit 04af196
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/channel.c
Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions src/json.c
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/testdir/test_json.vim
Expand Up @@ -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
2 changes: 2 additions & 0 deletions src/version.c
Expand Up @@ -771,6 +771,8 @@ static char *(features[]) =

static int included_patches[] =
{ /* Add new patch number below this line */
/**/
1158,
/**/
1157,
/**/
Expand Down

0 comments on commit 04af196

Please sign in to comment.