]> granicus.if.org Git - vim/commitdiff
patch 9.0.1375: crash when getting member of obj of unknown class v9.0.1375
authorErnie Rael <errael@raelity.com>
Fri, 3 Mar 2023 15:05:30 +0000 (15:05 +0000)
committerBram Moolenaar <Bram@vim.org>
Fri, 3 Mar 2023 15:05:30 +0000 (15:05 +0000)
Problem:    Crash when getting member of obj of unknown class.
Solution:   Check for NULL class and give an error message. (Ernie Rael,
            closes #12096)

src/errors.h
src/testdir/test_vim9_class.vim
src/version.c
src/vim9expr.c

index f1c36ad7bb9c4c59ca8ae5ee3f2babd3765f38bf..445a9b8d6e8347d10ee6ec24081fcca40dcf79f3 100644 (file)
@@ -3452,4 +3452,6 @@ EXTERN char e_cannot_use_color_none_did_you_mean_none[]
 #ifdef FEAT_EVAL
 EXTERN char e_cannot_use_non_null_object[]
        INIT(= N_("E1362: Cannot use a non-null object"));
+EXTERN char e_incomplete_type[]
+       INIT(= N_("E1363: Incomplete type"));
 #endif
index c228f2642ba3c0f03ef75200f094aea3cce27e39..bc8a8e1d548a27fd7e679cf073fe995cb514f2a0 100644 (file)
@@ -272,6 +272,25 @@ def Test_object_not_set()
       echo Colorscheme.new(bg).GetBackground()
   END
   v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected object<Background> but got object<Unknown>')
+
+  # TODO: this should not give an error but be handled at runtime
+  lines =<< trim END
+      vim9script
+
+      class Class
+          this.id: string
+          def Method1()
+              echo 'Method1' .. this.id
+          enddef
+      endclass
+
+      var obj = null_object
+      def Func()
+          obj.Method1()
+      enddef
+      Func()
+  END
+  v9.CheckScriptFailure(lines, 'E1363:')
 enddef
 
 def Test_class_member_initializer()
index 1caf5b24d015240e243f091e166682f0f0315ed2..8f26bdf47a26a784302310e5b7bf532edb300277 100644 (file)
@@ -695,6 +695,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1375,
 /**/
     1374,
 /**/
index b8458aa41712cf79da7ad44bc71030fcd5c9de74..d600cb0ae1a62a41e46367269879ad298762fe26 100644 (file)
@@ -293,6 +293,13 @@ compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
        }
     }
 
+    if (cl == NULL)
+    {
+       // TODO: this should not give an error but be handled at runtime
+       emsg(_(e_incomplete_type));
+       return FAIL;
+    }
+
     ++*arg;
     char_u *name = *arg;
     char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);