Skip to content

Commit eb91e24

Browse files
yegappanerrael
authored andcommitted
patch 9.0.1829: Vim9 missing access-checks for private vars
Problem: Vim9 missing access-checks for private vars Solution: Use the proper check for private/readonly variable. Access level for a member cannot be changed in a class implementing an interface. Update the code indentation closes: #12978 Signed-off-by: Christian Brabandt <[email protected]> Co-authored-by: Yegappan Lakshmanan <[email protected]> Co-authored-by: Ernie Rael <[email protected]>
1 parent ac2d881 commit eb91e24

8 files changed

Lines changed: 116 additions & 48 deletions

File tree

src/errors.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3486,6 +3486,8 @@ EXTERN char e_cannot_use_a_return_type_with_new[]
34863486
INIT(= N_("E1365: Cannot use a return type with the \"new\" function"));
34873487
EXTERN char e_cannot_access_private_method_str[]
34883488
INIT(= N_("E1366: Cannot access private method: %s"));
3489+
EXTERN char e_member_str_of_interface_str_has_different_access[]
3490+
INIT(= N_("E1367: Access level of member \"%s\" of interface \"%s\" is different"));
34893491

34903492
EXTERN char e_static_cannot_be_followed_by_this[]
34913493
INIT(= N_("E1368: Static cannot be followed by \"this\" in a member name"));
@@ -3510,4 +3512,4 @@ EXTERN char e_member_str_type_mismatch_expected_str_but_got_str[]
35103512
EXTERN char e_method_str_type_mismatch_expected_str_but_got_str[]
35113513
INIT(= N_("E1407: Member \"%s\": type mismatch, expected %s but got %s"));
35123514

3513-
// E1367, E1371 - E1399 unused
3515+
// E1371 - E1399 unused

src/option.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2982,7 +2982,8 @@ insecure_flag(int opt_idx, int opt_flags)
29822982
/*
29832983
* Redraw the window title and/or tab page text later.
29842984
*/
2985-
void redraw_titles(void)
2985+
void
2986+
redraw_titles(void)
29862987
{
29872988
need_maketitle = TRUE;
29882989
redraw_tabline = TRUE;

src/proto/vim9class.pro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* vim9class.c */
22
int object_index_from_itf_index(class_T *itf, int is_method, int idx, class_T *cl);
33
void ex_class(exarg_T *eap);
4-
type_T *class_member_type(class_T *cl, char_u *name, char_u *name_end, int *member_idx);
4+
type_T *class_member_type(class_T *cl, char_u *name, char_u *name_end, int *member_idx, omacc_T *access);
55
void ex_enum(exarg_T *eap);
66
void ex_type(exarg_T *eap);
77
int class_object_index(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int verbose);

src/testdir/test_vim9_class.vim

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ def Test_assignment_with_operator()
490490
vim9script
491491

492492
class Foo
493-
this.x: number
493+
public this.x: number
494494

495495
def Add(n: number)
496496
this.x += n
@@ -2593,15 +2593,15 @@ def Test_multi_level_member_access()
25932593
vim9script
25942594

25952595
class A
2596-
this.val1: number = 0
2596+
public this.val1: number = 0
25972597
endclass
25982598

25992599
class B extends A
2600-
this.val2: number = 0
2600+
public this.val2: number = 0
26012601
endclass
26022602

26032603
class C extends B
2604-
this.val3: number = 0
2604+
public this.val3: number = 0
26052605
endclass
26062606

26072607
def A_members(a: A)
@@ -3672,18 +3672,60 @@ def Test_private_member_access_outside_class()
36723672
END
36733673
v9.CheckScriptFailure(lines, 'E1333: Cannot access private member: _val')
36743674

3675-
# private class member variable
3675+
# access a non-existing private object member variable
36763676
lines =<< trim END
36773677
vim9script
36783678
class A
3679-
static _val: number = 10
3679+
this._val = 10
36803680
endclass
36813681
def T()
3682-
A._val = 20
3682+
var a = A.new()
3683+
a._a = 1
36833684
enddef
36843685
T()
36853686
END
3686-
v9.CheckScriptFailure(lines, 'E1333: Cannot access private member: _val')
3687+
v9.CheckScriptFailure(lines, 'E1089: Unknown variable: _a = 1')
3688+
enddef
3689+
3690+
" Test for changing the member access of an interface in a implementation class
3691+
def Test_change_interface_member_access()
3692+
var lines =<< trim END
3693+
vim9script
3694+
interface A
3695+
public this.val: number
3696+
endinterface
3697+
class B implements A
3698+
this.val = 10
3699+
endclass
3700+
END
3701+
v9.CheckScriptFailure(lines, 'E1367: Access level of member "val" of interface "A" is different')
3702+
3703+
lines =<< trim END
3704+
vim9script
3705+
interface A
3706+
this.val: number
3707+
endinterface
3708+
class B implements A
3709+
public this.val = 10
3710+
endclass
3711+
END
3712+
v9.CheckScriptFailure(lines, 'E1367: Access level of member "val" of interface "A" is different')
3713+
enddef
3714+
3715+
" Test for trying to change a readonly member from a def function
3716+
def Test_readonly_member_change_in_def_func()
3717+
var lines =<< trim END
3718+
vim9script
3719+
class A
3720+
this.val: number
3721+
endclass
3722+
def T()
3723+
var a = A.new()
3724+
a.val = 20
3725+
enddef
3726+
T()
3727+
END
3728+
v9.CheckScriptFailure(lines, 'E46: Cannot change read-only variable "val"')
36873729
enddef
36883730

36893731
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker

src/typval.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ check_for_list_arg(typval_T *args, int idx)
539539
{
540540
if (args[idx].v_type != VAR_LIST)
541541
{
542-
semsg(_(e_list_required_for_argument_nr), idx + 1);
542+
semsg(_(e_list_required_for_argument_nr), idx + 1);
543543
return FAIL;
544544
}
545545
return OK;
@@ -981,7 +981,7 @@ check_for_object_arg(typval_T *args, int idx)
981981
{
982982
if (args[idx].v_type != VAR_OBJECT)
983983
{
984-
semsg(_(e_object_required_for_argument_nr), idx + 1);
984+
semsg(_(e_object_required_for_argument_nr), idx + 1);
985985
return FAIL;
986986
}
987987
return OK;
@@ -995,7 +995,7 @@ check_for_class_or_list_arg(typval_T *args, int idx)
995995
{
996996
if (args[idx].v_type != VAR_CLASS && args[idx].v_type != VAR_LIST)
997997
{
998-
semsg(_(e_list_or_class_required_for_argument_nr), idx + 1);
998+
semsg(_(e_list_or_class_required_for_argument_nr), idx + 1);
999999
return FAIL;
10001000
}
10011001
return OK;

src/version.c

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

700700
static int included_patches[] =
701701
{ /* Add new patch number below this line */
702+
/**/
703+
1829,
702704
/**/
703705
1828,
704706
/**/

src/vim9class.c

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@
3030
*/
3131
static int
3232
parse_member(
33-
exarg_T *eap,
34-
char_u *line,
35-
char_u *varname,
36-
int has_public, // TRUE if "public" seen before "varname"
37-
char_u **varname_end,
38-
garray_T *type_list,
39-
type_T **type_ret,
40-
char_u **init_expr)
33+
exarg_T *eap,
34+
char_u *line,
35+
char_u *varname,
36+
int has_public, // TRUE if "public" seen before "varname"
37+
char_u **varname_end,
38+
garray_T *type_list,
39+
type_T **type_ret,
40+
char_u **init_expr)
4141
{
4242
*varname_end = to_name_end(varname, FALSE);
4343
if (*varname == '_' && has_public)
@@ -119,12 +119,12 @@ parse_member(
119119
*/
120120
static int
121121
add_member(
122-
garray_T *gap,
123-
char_u *varname,
124-
char_u *varname_end,
125-
int has_public,
126-
type_T *type,
127-
char_u *init_expr)
122+
garray_T *gap,
123+
char_u *varname,
124+
char_u *varname_end,
125+
int has_public,
126+
type_T *type,
127+
char_u *init_expr)
128128
{
129129
if (ga_grow(gap, 1) == FAIL)
130130
return FAIL;
@@ -357,6 +357,13 @@ validate_interface_members(
357357
where) == FAIL)
358358
return FALSE;
359359

360+
if (if_ms[if_i].ocm_access != m->ocm_access)
361+
{
362+
semsg(_(e_member_str_of_interface_str_has_different_access),
363+
if_ms[if_i].ocm_name, intf_class_name);
364+
return FALSE;
365+
}
366+
360367
break;
361368
}
362369
if (cl_i == cl_count)
@@ -622,11 +629,11 @@ is_valid_constructor(ufunc_T *uf, int is_abstract, int has_static)
622629
*/
623630
static int
624631
update_member_method_lookup_table(
625-
class_T *ifcl,
626-
class_T *cl,
627-
garray_T *objmethods,
628-
int pobj_method_offset,
629-
int is_interface)
632+
class_T *ifcl,
633+
class_T *cl,
634+
garray_T *objmethods,
635+
int pobj_method_offset,
636+
int is_interface)
630637
{
631638
if (ifcl == NULL)
632639
return OK;
@@ -1550,10 +1557,11 @@ ex_class(exarg_T *eap)
15501557
*/
15511558
type_T *
15521559
class_member_type(
1553-
class_T *cl,
1554-
char_u *name,
1555-
char_u *name_end,
1556-
int *member_idx)
1560+
class_T *cl,
1561+
char_u *name,
1562+
char_u *name_end,
1563+
int *member_idx,
1564+
omacc_T *access)
15571565
{
15581566
*member_idx = -1; // not found (yet)
15591567
size_t len = name_end - name;
@@ -1564,6 +1572,7 @@ class_member_type(
15641572
if (STRNCMP(m->ocm_name, name, len) == 0 && m->ocm_name[len] == NUL)
15651573
{
15661574
*member_idx = i;
1575+
*access = m->ocm_access;
15671576
return m->ocm_type;
15681577
}
15691578
}

src/vim9compile.c

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,20 +1865,30 @@ compile_lhs(
18651865
else if (use_class)
18661866
{
18671867
// for an object or class member get the type of the member
1868-
class_T *cl = lhs->lhs_type->tt_class;
1868+
class_T *cl = lhs->lhs_type->tt_class;
1869+
omacc_T access;
1870+
1871+
lhs->lhs_member_type = class_member_type(cl, after + 1,
1872+
lhs->lhs_end, &lhs->lhs_member_idx,
1873+
&access);
1874+
if (lhs->lhs_member_idx < 0)
1875+
return FAIL;
1876+
18691877
// If it is private member variable, then accessing it outside the
18701878
// class is not allowed.
1871-
if (*(after + 1) == '_' && !inside_class(cctx, cl))
1879+
if ((access != VIM_ACCESS_ALL) && !inside_class(cctx, cl))
18721880
{
1873-
char_u *m_name = vim_strnsave(after + 1, lhs->lhs_end - after);
1874-
semsg(_(e_cannot_access_private_member_str), m_name);
1881+
char_u *m_name;
1882+
char *msg;
1883+
1884+
m_name = vim_strnsave(after + 1, lhs->lhs_end - after - 1);
1885+
msg = (access == VIM_ACCESS_PRIVATE)
1886+
? e_cannot_access_private_member_str
1887+
: e_cannot_change_readonly_variable_str;
1888+
semsg(_(msg), m_name);
18751889
vim_free(m_name);
18761890
return FAIL;
18771891
}
1878-
lhs->lhs_member_type = class_member_type(cl, after + 1,
1879-
lhs->lhs_end, &lhs->lhs_member_idx);
1880-
if (lhs->lhs_member_idx < 0)
1881-
return FAIL;
18821892
}
18831893
else
18841894
{
@@ -2086,9 +2096,11 @@ compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
20862096
if (dot == NULL)
20872097
return FAIL;
20882098

2089-
class_T *cl = lhs->lhs_type->tt_class;
2090-
type_T *type = class_member_type(cl, dot + 1,
2091-
lhs->lhs_end, &lhs->lhs_member_idx);
2099+
class_T *cl = lhs->lhs_type->tt_class;
2100+
omacc_T access;
2101+
type_T *type = class_member_type(cl, dot + 1,
2102+
lhs->lhs_end, &lhs->lhs_member_idx,
2103+
&access);
20922104
if (lhs->lhs_member_idx < 0)
20932105
return FAIL;
20942106

0 commit comments

Comments
 (0)