c974171
To: vim_dev@googlegroups.com
c974171
Subject: Patch 7.3.146
c974171
Fcc: outbox
c974171
From: Bram Moolenaar <Bram@moolenaar.net>
c974171
Mime-Version: 1.0
c974171
Content-Type: text/plain; charset=UTF-8
c974171
Content-Transfer-Encoding: 8bit
c974171
------------
c974171
c974171
Patch 7.3.146
c974171
Problem:    It's possible to assign to a read-only member of a dict.
c974171
	    It's possible to create a global variable "0". (ZyX)
c974171
            It's possible to add a v: variable with ":let v:.name = 1".
c974171
Solution:   Add check for dict item being read-only.
c974171
	    Check the name of g: variables.
c974171
	    Disallow adding v: variables.
c974171
Files:	    src/eval.c
c974171
c974171
c974171
*** ../vim-7.3.145/src/eval.c	2011-02-01 13:48:47.000000000 +0100
c974171
--- src/eval.c	2011-03-27 15:56:44.000000000 +0200
c974171
***************
c974171
*** 789,794 ****
c974171
--- 789,796 ----
c974171
  static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
c974171
  static int var_check_ro __ARGS((int flags, char_u *name));
c974171
  static int var_check_fixed __ARGS((int flags, char_u *name));
c974171
+ static int var_check_func_name __ARGS((char_u *name, int new_var));
c974171
+ static int valid_varname __ARGS((char_u *varname));
c974171
  static int tv_check_lock __ARGS((int lock, char_u *name));
c974171
  static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
c974171
  static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
c974171
***************
c974171
*** 2716,2723 ****
c974171
--- 2718,2744 ----
c974171
  	    lp->ll_list = NULL;
c974171
  	    lp->ll_dict = lp->ll_tv->vval.v_dict;
c974171
  	    lp->ll_di = dict_find(lp->ll_dict, key, len);
c974171
+ 
c974171
+ 	    /* When assigning to g: check that a function and variable name is
c974171
+ 	     * valid. */
c974171
+ 	    if (rettv != NULL && lp->ll_dict == &globvardict)
c974171
+ 	    {
c974171
+ 		if (rettv->v_type == VAR_FUNC
c974171
+ 			       && var_check_func_name(key, lp->ll_di == NULL))
c974171
+ 		    return NULL;
c974171
+ 		if (!valid_varname(key))
c974171
+ 		    return NULL;
c974171
+ 	    }
c974171
+ 
c974171
  	    if (lp->ll_di == NULL)
c974171
  	    {
c974171
+ 		/* Can't add "v:" variable. */
c974171
+ 		if (lp->ll_dict == &vimvardict)
c974171
+ 		{
c974171
+ 		    EMSG2(_(e_illvar), name);
c974171
+ 		    return NULL;
c974171
+ 		}
c974171
+ 
c974171
  		/* Key does not exist in dict: may need to add it. */
c974171
  		if (*p == '[' || *p == '.' || unlet)
c974171
  		{
c974171
***************
c974171
*** 2737,2742 ****
c974171
--- 2758,2767 ----
c974171
  		    p = NULL;
c974171
  		break;
c974171
  	    }
c974171
+ 	    /* existing variable, need to check if it can be changed */
c974171
+ 	    else if (var_check_ro(lp->ll_di->di_flags, name))
c974171
+ 		return NULL;
c974171
+ 
c974171
  	    if (len == -1)
c974171
  		clear_tv(&var1;;
c974171
  	    lp->ll_tv = &lp->ll_di->di_tv;
c974171
***************
c974171
*** 19786,19792 ****
c974171
      dictitem_T	*v;
c974171
      char_u	*varname;
c974171
      hashtab_T	*ht;
c974171
-     char_u	*p;
c974171
  
c974171
      ht = find_var_ht(name, &varname);
c974171
      if (ht == NULL || *varname == NUL)
c974171
--- 19811,19816 ----
c974171
***************
c974171
*** 19796,19820 ****
c974171
      }
c974171
      v = find_var_in_ht(ht, varname, TRUE);
c974171
  
c974171
!     if (tv->v_type == VAR_FUNC)
c974171
!     {
c974171
! 	if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
c974171
! 		&& !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
c974171
! 							 ? name[2] : name[0]))
c974171
! 	{
c974171
! 	    EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
c974171
! 	    return;
c974171
! 	}
c974171
! 	/* Don't allow hiding a function.  When "v" is not NULL we might be
c974171
! 	 * assigning another function to the same var, the type is checked
c974171
! 	 * below. */
c974171
! 	if (v == NULL && function_exists(name))
c974171
! 	{
c974171
! 	    EMSG2(_("E705: Variable name conflicts with existing function: %s"),
c974171
! 									name);
c974171
! 	    return;
c974171
! 	}
c974171
!     }
c974171
  
c974171
      if (v != NULL)
c974171
      {
c974171
--- 19820,19827 ----
c974171
      }
c974171
      v = find_var_in_ht(ht, varname, TRUE);
c974171
  
c974171
!     if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
c974171
! 	return;
c974171
  
c974171
      if (v != NULL)
c974171
      {
c974171
***************
c974171
*** 19880,19892 ****
c974171
  	}
c974171
  
c974171
  	/* Make sure the variable name is valid. */
c974171
! 	for (p = varname; *p != NUL; ++p)
c974171
! 	    if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
c974171
! 						       && *p != AUTOLOAD_CHAR)
c974171
! 	    {
c974171
! 		EMSG2(_(e_illvar), varname);
c974171
! 		return;
c974171
! 	    }
c974171
  
c974171
  	v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
c974171
  							  + STRLEN(varname)));
c974171
--- 19887,19894 ----
c974171
  	}
c974171
  
c974171
  	/* Make sure the variable name is valid. */
c974171
! 	if (!valid_varname(varname))
c974171
! 	    return;
c974171
  
c974171
  	v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
c974171
  							  + STRLEN(varname)));
c974171
***************
c974171
*** 19951,19956 ****
c974171
--- 19953,20007 ----
c974171
  }
c974171
  
c974171
  /*
c974171
+  * Check if a funcref is assigned to a valid variable name.
c974171
+  * Return TRUE and give an error if not.
c974171
+  */
c974171
+     static int
c974171
+ var_check_func_name(name, new_var)
c974171
+     char_u *name;    /* points to start of variable name */
c974171
+     int    new_var;  /* TRUE when creating the variable */
c974171
+ {
c974171
+     if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
c974171
+ 	    && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
c974171
+ 						     ? name[2] : name[0]))
c974171
+     {
c974171
+ 	EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
c974171
+ 									name);
c974171
+ 	return TRUE;
c974171
+     }
c974171
+     /* Don't allow hiding a function.  When "v" is not NULL we might be
c974171
+      * assigning another function to the same var, the type is checked
c974171
+      * below. */
c974171
+     if (new_var && function_exists(name))
c974171
+     {
c974171
+ 	EMSG2(_("E705: Variable name conflicts with existing function: %s"),
c974171
+ 								    name);
c974171
+ 	return TRUE;
c974171
+     }
c974171
+     return FALSE;
c974171
+ }
c974171
+ 
c974171
+ /*
c974171
+  * Check if a variable name is valid.
c974171
+  * Return FALSE and give an error if not.
c974171
+  */
c974171
+     static int
c974171
+ valid_varname(varname)
c974171
+     char_u *varname;
c974171
+ {
c974171
+     char_u *p;
c974171
+ 
c974171
+     for (p = varname; *p != NUL; ++p)
c974171
+ 	if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
c974171
+ 						   && *p != AUTOLOAD_CHAR)
c974171
+ 	{
c974171
+ 	    EMSG2(_(e_illvar), varname);
c974171
+ 	    return FALSE;
c974171
+ 	}
c974171
+     return TRUE;
c974171
+ }
c974171
+ 
c974171
+ /*
c974171
   * Return TRUE if typeval "tv" is set to be locked (immutable).
c974171
   * Also give an error message, using "name".
c974171
   */
c974171
*** ../vim-7.3.145/src/version.c	2011-03-26 18:32:00.000000000 +0100
c974171
--- src/version.c	2011-03-27 16:01:03.000000000 +0200
c974171
***************
c974171
*** 716,717 ****
c974171
--- 716,719 ----
c974171
  {   /* Add new patch number below this line */
c974171
+ /**/
c974171
+     146,
c974171
  /**/
c974171
c974171
-- 
c974171
ARTHUR: It is I, Arthur, son of Uther Pendragon, from the castle of Camelot.
c974171
        King of all Britons, defeator of the Saxons, sovereign of all England!
c974171
   [Pause]
c974171
SOLDIER: Get away!
c974171
                 "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD
c974171
c974171
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
c974171
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
c974171
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
c974171
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///