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 <cb@256bit.org>
Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Co-authored-by: Ernie Rael <errael@raelity.com>
diff --git a/src/vim9compile.c b/src/vim9compile.c
index 5c173c1..0d2de99 100644
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -1865,20 +1865,30 @@
else if (use_class)
{
// for an object or class member get the type of the member
- class_T *cl = lhs->lhs_type->tt_class;
+ class_T *cl = lhs->lhs_type->tt_class;
+ omacc_T access;
+
+ lhs->lhs_member_type = class_member_type(cl, after + 1,
+ lhs->lhs_end, &lhs->lhs_member_idx,
+ &access);
+ if (lhs->lhs_member_idx < 0)
+ return FAIL;
+
// If it is private member variable, then accessing it outside the
// class is not allowed.
- if (*(after + 1) == '_' && !inside_class(cctx, cl))
+ if ((access != VIM_ACCESS_ALL) && !inside_class(cctx, cl))
{
- char_u *m_name = vim_strnsave(after + 1, lhs->lhs_end - after);
- semsg(_(e_cannot_access_private_member_str), m_name);
+ char_u *m_name;
+ char *msg;
+
+ m_name = vim_strnsave(after + 1, lhs->lhs_end - after - 1);
+ msg = (access == VIM_ACCESS_PRIVATE)
+ ? e_cannot_access_private_member_str
+ : e_cannot_change_readonly_variable_str;
+ semsg(_(msg), m_name);
vim_free(m_name);
return FAIL;
}
- lhs->lhs_member_type = class_member_type(cl, after + 1,
- lhs->lhs_end, &lhs->lhs_member_idx);
- if (lhs->lhs_member_idx < 0)
- return FAIL;
}
else
{
@@ -2086,9 +2096,11 @@
if (dot == NULL)
return FAIL;
- class_T *cl = lhs->lhs_type->tt_class;
- type_T *type = class_member_type(cl, dot + 1,
- lhs->lhs_end, &lhs->lhs_member_idx);
+ class_T *cl = lhs->lhs_type->tt_class;
+ omacc_T access;
+ type_T *type = class_member_type(cl, dot + 1,
+ lhs->lhs_end, &lhs->lhs_member_idx,
+ &access);
if (lhs->lhs_member_idx < 0)
return FAIL;