Skip to content

Commit 4f8b8fa

Browse files
committed
patch 7.4.1269
Problem: Encoding {'key':} to JSON doesn't give an error (Tyru) Solution: Give an error.
1 parent 26dfc41 commit 4f8b8fa

3 files changed

Lines changed: 14 additions & 7 deletions

File tree

src/json.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "vim.h"
1717

1818
#if defined(FEAT_EVAL) || defined(PROTO)
19-
static int json_encode_item(garray_T *gap, typval_T *val, int copyID);
19+
static int json_encode_item(garray_T *gap, typval_T *val, int copyID, int allow_none);
2020
static int json_decode_item(js_read_T *reader, typval_T *res);
2121

2222
/*
@@ -29,7 +29,7 @@ json_encode(typval_T *val)
2929

3030
/* Store bytes in the growarray. */
3131
ga_init2(&ga, 1, 4000);
32-
json_encode_item(&ga, val, get_copyID());
32+
json_encode_item(&ga, val, get_copyID(), TRUE);
3333
return ga.ga_data;
3434
}
3535

@@ -121,7 +121,7 @@ write_string(garray_T *gap, char_u *str)
121121
* Return FAIL or OK.
122122
*/
123123
static int
124-
json_encode_item(garray_T *gap, typval_T *val, int copyID)
124+
json_encode_item(garray_T *gap, typval_T *val, int copyID, int allow_none)
125125
{
126126
char_u numbuf[NUMBUFLEN];
127127
char_u *res;
@@ -135,7 +135,10 @@ json_encode_item(garray_T *gap, typval_T *val, int copyID)
135135
{
136136
case VVAL_FALSE: ga_concat(gap, (char_u *)"false"); break;
137137
case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break;
138-
case VVAL_NONE: break;
138+
case VVAL_NONE: if (!allow_none)
139+
/* TODO: better error */
140+
EMSG(_(e_invarg));
141+
break;
139142
case VVAL_NULL: ga_concat(gap, (char_u *)"null"); break;
140143
}
141144
break;
@@ -152,7 +155,7 @@ json_encode_item(garray_T *gap, typval_T *val, int copyID)
152155
break;
153156

154157
case VAR_FUNC:
155-
/* no JSON equivalent */
158+
/* no JSON equivalent TODO: better error */
156159
EMSG(_(e_invarg));
157160
return FAIL;
158161

@@ -172,7 +175,8 @@ json_encode_item(garray_T *gap, typval_T *val, int copyID)
172175
ga_append(gap, '[');
173176
for (li = l->lv_first; li != NULL && !got_int; )
174177
{
175-
if (json_encode_item(gap, &li->li_tv, copyID) == FAIL)
178+
if (json_encode_item(gap, &li->li_tv, copyID, TRUE)
179+
== FAIL)
176180
return FAIL;
177181
li = li->li_next;
178182
if (li != NULL)
@@ -213,7 +217,7 @@ json_encode_item(garray_T *gap, typval_T *val, int copyID)
213217
write_string(gap, hi->hi_key);
214218
ga_append(gap, ':');
215219
if (json_encode_item(gap, &dict_lookup(hi)->di_tv,
216-
copyID) == FAIL)
220+
copyID, FALSE) == FAIL)
217221
return FAIL;
218222
}
219223
ga_append(gap, '}');

src/testdir/test_json.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func Test_encode()
7474

7575
call assert_fails('echo jsonencode(function("tr"))', 'E474:')
7676
call assert_fails('echo jsonencode([function("tr")])', 'E474:')
77+
call assert_fails('echo jsonencode({"key":v:none})', 'E474:')
7778
endfunc
7879

7980
func Test_decode()

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,8 @@ static char *(features[]) =
742742

743743
static int included_patches[] =
744744
{ /* Add new patch number below this line */
745+
/**/
746+
1269,
745747
/**/
746748
1268,
747749
/**/

0 commit comments

Comments
 (0)