Skip to content

Commit

Permalink
patch 9.1.0385: Vim9: crash with null_class and null_object
Browse files Browse the repository at this point in the history
Problem:  Vim9: crash with null_class and null_object
          (Aliaksei Budavei)
Solution: Handle null_class and null_object correctly
          (Yegappan Lakshmanan)

fixes: #14678
closes: #14681

Signed-off-by: Yegappan Lakshmanan <[email protected]>
Signed-off-by: Christian Brabandt <[email protected]>
  • Loading branch information
yegappan authored and chrisbra committed May 1, 2024
1 parent ca4b81a commit b2e42b9
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -6403,9 +6403,9 @@ echo_string_core(
{
class_T *cl = tv->vval.v_class;
char *s = "class";
if (IS_INTERFACE(cl))
if (cl && IS_INTERFACE(cl))
s = "interface";
else if (IS_ENUM(cl))
else if (cl && IS_ENUM(cl))
s = "enum";
size_t len = STRLEN(s) + 1 +
(cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
Expand Down
17 changes: 12 additions & 5 deletions src/evalfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -11497,19 +11497,26 @@ f_type(typval_T *argvars, typval_T *rettv)
case VAR_CLASS:
{
class_T *cl = argvars[0].vval.v_class;
if (IS_ENUM(cl))
if (cl && IS_ENUM(cl))
n = VAR_TYPE_ENUM;
else
n = VAR_TYPE_CLASS;
break;
}
case VAR_OBJECT:
{
class_T *cl = argvars[0].vval.v_object->obj_class;
if (IS_ENUM(cl))
n = VAR_TYPE_ENUMVALUE;
else
object_T *obj = argvars[0].vval.v_object;

if (obj == NULL)
n = VAR_TYPE_OBJECT;
else
{
class_T *cl = argvars[0].vval.v_object->obj_class;
if (IS_ENUM(cl))
n = VAR_TYPE_ENUMVALUE;
else
n = VAR_TYPE_OBJECT;
}
break;
}
case VAR_UNKNOWN:
Expand Down
46 changes: 46 additions & 0 deletions src/testdir/test_vim9_class.vim
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,52 @@ def Test_using_null_class()
@_ = null_class.member
END
v9.CheckDefExecAndScriptFailure(lines, ['E715: Dictionary required', 'E1363: Incomplete type'])

# Test for using a null class as a value
lines =<< trim END
vim9script
echo empty(null_class)
END
v9.CheckSourceFailure(lines, 'E1405: Class "" cannot be used as a value', 2)

# Test for using a null class with string()
lines =<< trim END
vim9script
assert_equal('class [unknown]', string(null_class))
END
v9.CheckSourceSuccess(lines)

# Test for using a null class with string()
lines =<< trim END
vim9script
assert_equal(12, type(null_class))
assert_equal('class<Unknown>', typename(null_class))
END
v9.CheckSourceSuccess(lines)
enddef

def Test_using_null_object()
# Test for using a null object as a value
var lines =<< trim END
vim9script
assert_equal(1, empty(null_object))
END
v9.CheckSourceSuccess(lines)

# Test for using a null object with string()
lines =<< trim END
vim9script
assert_equal('object of [unknown]', string(null_object))
END
v9.CheckSourceSuccess(lines)

# Test for using a null object with string()
lines =<< trim END
vim9script
assert_equal(13, type(null_object))
assert_equal('object<Unknown>', typename(null_object))
END
v9.CheckSourceSuccess(lines)
enddef

def Test_class_interface_wrong_end()
Expand Down
2 changes: 2 additions & 0 deletions src/version.c
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,8 @@ static char *(features[]) =

static int included_patches[] =
{ /* Add new patch number below this line */
/**/
385,
/**/
384,
/**/
Expand Down
8 changes: 5 additions & 3 deletions src/vim9type.c
Original file line number Diff line number Diff line change
Expand Up @@ -2091,10 +2091,12 @@ check_typval_is_value(typval_T *tv)
case VAR_CLASS:
{
class_T *cl = tv->vval.v_class;
if (IS_ENUM(cl))
semsg(_(e_using_enum_as_value_str), cl->class_name);
char_u *class_name = (cl == NULL) ? (char_u *)""
: cl->class_name;
if (cl && IS_ENUM(cl))
semsg(_(e_using_enum_as_value_str), class_name);
else
semsg(_(e_using_class_as_value_str), cl->class_name);
semsg(_(e_using_class_as_value_str), class_name);
}
return FAIL;

Expand Down

0 comments on commit b2e42b9

Please sign in to comment.