3efd197
To: vim_dev@googlegroups.com
3efd197
Subject: Patch 7.3.160
3efd197
Fcc: outbox
3efd197
From: Bram Moolenaar <Bram@moolenaar.net>
3efd197
Mime-Version: 1.0
3efd197
Content-Type: text/plain; charset=UTF-8
3efd197
Content-Transfer-Encoding: 8bit
3efd197
------------
3efd197
3efd197
Patch 7.3.160
3efd197
Problem:    Unsafe string copying.
3efd197
Solution:   Use vim_strncpy() instead of strcpy().  Use vim_strcat() instead
3efd197
	    of strcat().
3efd197
Files:	    src/buffer.c, src/ex_docmd.c, src/hardcopy.c, src/menu.c,
3efd197
	    src/misc1.c, src/misc2.c, src/proto/misc2.pro, src/netbeans.c,
3efd197
	    src/os_unix.c, src/spell.c, src/syntax.c, src/tag.c
3efd197
3efd197
*** ../vim-7.3.159/src/buffer.c	2011-02-15 14:24:42.000000000 +0100
3efd197
--- src/buffer.c	2011-04-11 16:08:38.000000000 +0200
3efd197
***************
3efd197
*** 3176,3182 ****
3efd197
  	    /* format: "fname + (path) (1 of 2) - VIM" */
3efd197
  
3efd197
  	    if (curbuf->b_fname == NULL)
3efd197
! 		STRCPY(buf, _("[No Name]"));
3efd197
  	    else
3efd197
  	    {
3efd197
  		p = transstr(gettail(curbuf->b_fname));
3efd197
--- 3176,3182 ----
3efd197
  	    /* format: "fname + (path) (1 of 2) - VIM" */
3efd197
  
3efd197
  	    if (curbuf->b_fname == NULL)
3efd197
! 		vim_strncpy(buf, (char_u *)_("[No Name]"), IOSIZE - 100);
3efd197
  	    else
3efd197
  	    {
3efd197
  		p = transstr(gettail(curbuf->b_fname));
3efd197
***************
3efd197
*** 3232,3238 ****
3efd197
  	    if (serverName != NULL)
3efd197
  	    {
3efd197
  		STRCAT(buf, " - ");
3efd197
! 		STRCAT(buf, serverName);
3efd197
  	    }
3efd197
  	    else
3efd197
  #endif
3efd197
--- 3232,3238 ----
3efd197
  	    if (serverName != NULL)
3efd197
  	    {
3efd197
  		STRCAT(buf, " - ");
3efd197
! 		vim_strcat(buf, serverName, IOSIZE);
3efd197
  	    }
3efd197
  	    else
3efd197
  #endif
3efd197
*** ../vim-7.3.159/src/ex_docmd.c	2011-03-03 15:54:45.000000000 +0100
3efd197
--- src/ex_docmd.c	2011-04-11 15:43:48.000000000 +0200
3efd197
***************
3efd197
*** 5096,5102 ****
3efd197
  		char_u	buff[IOSIZE];
3efd197
  
3efd197
  		if (n == 1)
3efd197
! 		    STRCPY(buff, _("1 more file to edit.  Quit anyway?"));
3efd197
  		else
3efd197
  		    vim_snprintf((char *)buff, IOSIZE,
3efd197
  			      _("%d more files to edit.  Quit anyway?"), n);
3efd197
--- 5096,5104 ----
3efd197
  		char_u	buff[IOSIZE];
3efd197
  
3efd197
  		if (n == 1)
3efd197
! 		    vim_strncpy(buff,
3efd197
! 			    (char_u *)_("1 more file to edit.  Quit anyway?"),
3efd197
! 								  IOSIZE - 1);
3efd197
  		else
3efd197
  		    vim_snprintf((char *)buff, IOSIZE,
3efd197
  			      _("%d more files to edit.  Quit anyway?"), n);
3efd197
*** ../vim-7.3.159/src/hardcopy.c	2010-08-15 21:57:25.000000000 +0200
3efd197
--- src/hardcopy.c	2011-04-11 15:30:09.000000000 +0200
3efd197
***************
3efd197
*** 1761,1772 ****
3efd197
  {
3efd197
      char_u	buffer[MAXPATHL + 1];
3efd197
  
3efd197
!     STRCPY(resource->name, name);
3efd197
      /* Look for named resource file in runtimepath */
3efd197
      STRCPY(buffer, "print");
3efd197
      add_pathsep(buffer);
3efd197
!     STRCAT(buffer, name);
3efd197
!     STRCAT(buffer, ".ps");
3efd197
      resource->filename[0] = NUL;
3efd197
      return (do_in_runtimepath(buffer, FALSE, prt_resource_name,
3efd197
  							   resource->filename)
3efd197
--- 1761,1772 ----
3efd197
  {
3efd197
      char_u	buffer[MAXPATHL + 1];
3efd197
  
3efd197
!     vim_strncpy(resource->name, (char_u *)name, 63);
3efd197
      /* Look for named resource file in runtimepath */
3efd197
      STRCPY(buffer, "print");
3efd197
      add_pathsep(buffer);
3efd197
!     vim_strcat(buffer, (char_u *)name, MAXPATHL);
3efd197
!     vim_strcat(buffer, (char_u *)".ps", MAXPATHL);
3efd197
      resource->filename[0] = NUL;
3efd197
      return (do_in_runtimepath(buffer, FALSE, prt_resource_name,
3efd197
  							   resource->filename)
3efd197
*** ../vim-7.3.159/src/menu.c	2011-01-04 17:49:25.000000000 +0100
3efd197
--- src/menu.c	2011-04-11 15:17:21.000000000 +0200
3efd197
***************
3efd197
*** 1394,1400 ****
3efd197
      int		idx;
3efd197
  {
3efd197
      static vimmenu_T	*menu = NULL;
3efd197
!     static char_u	tbuffer[256]; /*hack*/
3efd197
      char_u		*str;
3efd197
  #ifdef FEAT_MULTI_LANG
3efd197
      static  int		should_advance = FALSE;
3efd197
--- 1394,1401 ----
3efd197
      int		idx;
3efd197
  {
3efd197
      static vimmenu_T	*menu = NULL;
3efd197
! #define TBUFFER_LEN 256
3efd197
!     static char_u	tbuffer[TBUFFER_LEN]; /*hack*/
3efd197
      char_u		*str;
3efd197
  #ifdef FEAT_MULTI_LANG
3efd197
      static  int		should_advance = FALSE;
3efd197
***************
3efd197
*** 1428,1438 ****
3efd197
  	{
3efd197
  #ifdef FEAT_MULTI_LANG
3efd197
  	    if (should_advance)
3efd197
! 		STRCPY(tbuffer, menu->en_dname);
3efd197
  	    else
3efd197
  	    {
3efd197
  #endif
3efd197
! 		STRCPY(tbuffer, menu->dname);
3efd197
  #ifdef FEAT_MULTI_LANG
3efd197
  		if (menu->en_dname == NULL)
3efd197
  		    should_advance = TRUE;
3efd197
--- 1429,1439 ----
3efd197
  	{
3efd197
  #ifdef FEAT_MULTI_LANG
3efd197
  	    if (should_advance)
3efd197
! 		vim_strncpy(tbuffer, menu->en_dname, TBUFFER_LEN - 2);
3efd197
  	    else
3efd197
  	    {
3efd197
  #endif
3efd197
! 		vim_strncpy(tbuffer, menu->dname,  TBUFFER_LEN - 2);
3efd197
  #ifdef FEAT_MULTI_LANG
3efd197
  		if (menu->en_dname == NULL)
3efd197
  		    should_advance = TRUE;
3efd197
*** ../vim-7.3.159/src/misc1.c	2011-04-11 14:27:34.000000000 +0200
3efd197
--- src/misc1.c	2011-04-11 16:03:22.000000000 +0200
3efd197
***************
3efd197
*** 3332,3350 ****
3efd197
  	if (pn == 1)
3efd197
  	{
3efd197
  	    if (n > 0)
3efd197
! 		STRCPY(msg_buf, _("1 more line"));
3efd197
  	    else
3efd197
! 		STRCPY(msg_buf, _("1 line less"));
3efd197
  	}
3efd197
  	else
3efd197
  	{
3efd197
  	    if (n > 0)
3efd197
! 		sprintf((char *)msg_buf, _("%ld more lines"), pn);
3efd197
  	    else
3efd197
! 		sprintf((char *)msg_buf, _("%ld fewer lines"), pn);
3efd197
  	}
3efd197
  	if (got_int)
3efd197
! 	    STRCAT(msg_buf, _(" (Interrupted)"));
3efd197
  	if (msg(msg_buf))
3efd197
  	{
3efd197
  	    set_keep_msg(msg_buf, 0);
3efd197
--- 3332,3354 ----
3efd197
  	if (pn == 1)
3efd197
  	{
3efd197
  	    if (n > 0)
3efd197
! 		vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3efd197
! 							     MSG_BUF_LEN - 1);
3efd197
  	    else
3efd197
! 		vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3efd197
! 							     MSG_BUF_LEN - 1);
3efd197
  	}
3efd197
  	else
3efd197
  	{
3efd197
  	    if (n > 0)
3efd197
! 		vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3efd197
! 						     _("%ld more lines"), pn);
3efd197
  	    else
3efd197
! 		vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3efd197
! 						    _("%ld fewer lines"), pn);
3efd197
  	}
3efd197
  	if (got_int)
3efd197
! 	    vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
3efd197
  	if (msg(msg_buf))
3efd197
  	{
3efd197
  	    set_keep_msg(msg_buf, 0);
3efd197
*** ../vim-7.3.159/src/misc2.c	2010-12-08 13:11:15.000000000 +0100
3efd197
--- src/misc2.c	2011-04-11 15:30:20.000000000 +0200
3efd197
***************
3efd197
*** 1647,1652 ****
3efd197
--- 1647,1674 ----
3efd197
  }
3efd197
  
3efd197
  /*
3efd197
+  * Like strcat(), but make sure the result fits in "tosize" bytes and is
3efd197
+  * always NUL terminated.
3efd197
+  */
3efd197
+     void
3efd197
+ vim_strcat(to, from, tosize)
3efd197
+     char_u	*to;
3efd197
+     char_u	*from;
3efd197
+     size_t	tosize;
3efd197
+ {
3efd197
+     size_t tolen = STRLEN(to);
3efd197
+     size_t fromlen = STRLEN(from);
3efd197
+ 
3efd197
+     if (tolen + fromlen + 1 > tosize)
3efd197
+     {
3efd197
+ 	mch_memmove(to + tolen, from, tosize - tolen - 1);
3efd197
+ 	to[tosize - 1] = NUL;
3efd197
+     }
3efd197
+     else
3efd197
+ 	STRCPY(to + tolen, from);
3efd197
+ }
3efd197
+ 
3efd197
+ /*
3efd197
   * Isolate one part of a string option where parts are separated with
3efd197
   * "sep_chars".
3efd197
   * The part is copied into "buf[maxlen]".
3efd197
*** ../vim-7.3.159/src/proto/misc2.pro	2010-08-15 21:57:28.000000000 +0200
3efd197
--- src/proto/misc2.pro	2011-04-11 15:29:55.000000000 +0200
3efd197
***************
3efd197
*** 40,45 ****
3efd197
--- 40,46 ----
3efd197
  void copy_chars __ARGS((char_u *ptr, size_t count, int c));
3efd197
  void del_trailing_spaces __ARGS((char_u *ptr));
3efd197
  void vim_strncpy __ARGS((char_u *to, char_u *from, size_t len));
3efd197
+ void vim_strcat __ARGS((char_u *to, char_u *from, size_t tosize));
3efd197
  int copy_option_part __ARGS((char_u **option, char_u *buf, int maxlen, char *sep_chars));
3efd197
  void vim_free __ARGS((void *x));
3efd197
  int vim_stricmp __ARGS((char *s1, char *s2));
3efd197
*** ../vim-7.3.159/src/netbeans.c	2011-04-01 15:33:54.000000000 +0200
3efd197
--- src/netbeans.c	2011-04-11 16:02:51.000000000 +0200
3efd197
***************
3efd197
*** 3914,3927 ****
3efd197
      }
3efd197
      else
3efd197
      {
3efd197
! 	char_u ebuf[BUFSIZ];
3efd197
  
3efd197
! 	STRCPY(ebuf, (char_u *)_("E505: "));
3efd197
! 	STRCAT(ebuf, IObuff);
3efd197
! 	STRCAT(ebuf, (char_u *)_("is read-only (add ! to override)"));
3efd197
! 	STRCPY(IObuff, ebuf);
3efd197
! 	nbdebug(("    %s\n", ebuf ));
3efd197
! 	emsg(IObuff);
3efd197
      }
3efd197
  }
3efd197
  
3efd197
--- 3914,3925 ----
3efd197
      }
3efd197
      else
3efd197
      {
3efd197
! 	char_u msgbuf[IOSIZE];
3efd197
  
3efd197
! 	vim_snprintf((char *)msgbuf, IOSIZE,
3efd197
! 		_("E505: %s is read-only (add ! to override)"), IObuff);
3efd197
! 	nbdebug(("    %s\n", msgbuf));
3efd197
! 	emsg(msgbuf);
3efd197
      }
3efd197
  }
3efd197
  
3efd197
*** ../vim-7.3.159/src/os_unix.c	2011-02-15 17:39:14.000000000 +0100
3efd197
--- src/os_unix.c	2011-04-11 16:39:11.000000000 +0200
3efd197
***************
3efd197
*** 5725,5730 ****
3efd197
--- 5725,5731 ----
3efd197
  	if (shell_style == STYLE_PRINT && !did_find_nul)
3efd197
  	{
3efd197
  	    /* If there is a NUL, set did_find_nul, else set check_spaces */
3efd197
+ 	    buffer[len] = NUL;
3efd197
  	    if (len && (int)STRLEN(buffer) < (int)len - 1)
3efd197
  		did_find_nul = TRUE;
3efd197
  	    else
3efd197
***************
3efd197
*** 6594,6600 ****
3efd197
  	    xterm_hints.x = 2;
3efd197
  	return TRUE;
3efd197
      }
3efd197
!     if (mouse_code == NULL)
3efd197
      {
3efd197
  	xterm_trace = 0;
3efd197
  	return FALSE;
3efd197
--- 6595,6601 ----
3efd197
  	    xterm_hints.x = 2;
3efd197
  	return TRUE;
3efd197
      }
3efd197
!     if (mouse_code == NULL || STRLEN(mouse_code) > 45)
3efd197
      {
3efd197
  	xterm_trace = 0;
3efd197
  	return FALSE;
3efd197
*** ../vim-7.3.159/src/spell.c	2011-02-01 13:59:44.000000000 +0100
3efd197
--- src/spell.c	2011-04-11 15:50:40.000000000 +0200
3efd197
***************
3efd197
*** 6957,6963 ****
3efd197
  			    if (ae->ae_add == NULL)
3efd197
  				*newword = NUL;
3efd197
  			    else
3efd197
! 				STRCPY(newword, ae->ae_add);
3efd197
  			    p = word;
3efd197
  			    if (ae->ae_chop != NULL)
3efd197
  			    {
3efd197
--- 6957,6963 ----
3efd197
  			    if (ae->ae_add == NULL)
3efd197
  				*newword = NUL;
3efd197
  			    else
3efd197
! 				vim_strncpy(newword, ae->ae_add, MAXWLEN - 1);
3efd197
  			    p = word;
3efd197
  			    if (ae->ae_chop != NULL)
3efd197
  			    {
3efd197
***************
3efd197
*** 6978,6984 ****
3efd197
  			else
3efd197
  			{
3efd197
  			    /* suffix: chop/add at the end of the word */
3efd197
! 			    STRCPY(newword, word);
3efd197
  			    if (ae->ae_chop != NULL)
3efd197
  			    {
3efd197
  				/* Remove chop string. */
3efd197
--- 6978,6984 ----
3efd197
  			else
3efd197
  			{
3efd197
  			    /* suffix: chop/add at the end of the word */
3efd197
! 			    vim_strncpy(newword, word, MAXWLEN - 1);
3efd197
  			    if (ae->ae_chop != NULL)
3efd197
  			    {
3efd197
  				/* Remove chop string. */
3efd197
***************
3efd197
*** 8654,8660 ****
3efd197
       * Write the .sug file.
3efd197
       * Make the file name by changing ".spl" to ".sug".
3efd197
       */
3efd197
!     STRCPY(fname, wfname);
3efd197
      len = (int)STRLEN(fname);
3efd197
      fname[len - 2] = 'u';
3efd197
      fname[len - 1] = 'g';
3efd197
--- 8654,8660 ----
3efd197
       * Write the .sug file.
3efd197
       * Make the file name by changing ".spl" to ".sug".
3efd197
       */
3efd197
!     vim_strncpy(fname, wfname, MAXPATHL - 1);
3efd197
      len = (int)STRLEN(fname);
3efd197
      fname[len - 2] = 'u';
3efd197
      fname[len - 1] = 'g';
3efd197
***************
3efd197
*** 10261,10267 ****
3efd197
  
3efd197
  	    /* The suggested word may replace only part of the bad word, add
3efd197
  	     * the not replaced part. */
3efd197
! 	    STRCPY(wcopy, stp->st_word);
3efd197
  	    if (sug.su_badlen > stp->st_orglen)
3efd197
  		vim_strncpy(wcopy + stp->st_wordlen,
3efd197
  					       sug.su_badptr + stp->st_orglen,
3efd197
--- 10261,10267 ----
3efd197
  
3efd197
  	    /* The suggested word may replace only part of the bad word, add
3efd197
  	     * the not replaced part. */
3efd197
! 	    vim_strncpy(wcopy, stp->st_word, MAXWLEN);
3efd197
  	    if (sug.su_badlen > stp->st_orglen)
3efd197
  		vim_strncpy(wcopy + stp->st_wordlen,
3efd197
  					       sug.su_badptr + stp->st_orglen,
3efd197
***************
3efd197
*** 13162,13168 ****
3efd197
  	pbad = badsound2;
3efd197
      }
3efd197
  
3efd197
!     if (lendiff > 0)
3efd197
      {
3efd197
  	/* Add part of the bad word to the good word, so that we soundfold
3efd197
  	 * what replaces the bad word. */
3efd197
--- 13162,13168 ----
3efd197
  	pbad = badsound2;
3efd197
      }
3efd197
  
3efd197
!     if (lendiff > 0 && stp->st_wordlen + lendiff < MAXWLEN)
3efd197
      {
3efd197
  	/* Add part of the bad word to the good word, so that we soundfold
3efd197
  	 * what replaces the bad word. */
3efd197
***************
3efd197
*** 13875,13881 ****
3efd197
      for (i = gap->ga_len - 1; i >= 0; --i)
3efd197
      {
3efd197
  	/* Need to append what follows to check for "the the". */
3efd197
! 	STRCPY(longword, stp[i].st_word);
3efd197
  	len = stp[i].st_wordlen;
3efd197
  	vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
3efd197
  							       MAXWLEN - len);
3efd197
--- 13875,13881 ----
3efd197
      for (i = gap->ga_len - 1; i >= 0; --i)
3efd197
      {
3efd197
  	/* Need to append what follows to check for "the the". */
3efd197
! 	vim_strncpy(longword, stp[i].st_word, MAXWLEN);
3efd197
  	len = stp[i].st_wordlen;
3efd197
  	vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
3efd197
  							       MAXWLEN - len);
3efd197
***************
3efd197
*** 14221,14227 ****
3efd197
  	*t = NUL;
3efd197
      }
3efd197
      else
3efd197
! 	STRCPY(word, s);
3efd197
  
3efd197
      smp = (salitem_T *)slang->sl_sal.ga_data;
3efd197
  
3efd197
--- 14221,14227 ----
3efd197
  	*t = NUL;
3efd197
      }
3efd197
      else
3efd197
! 	vim_strncpy(word, s, MAXWLEN - 1);
3efd197
  
3efd197
      smp = (salitem_T *)slang->sl_sal.ga_data;
3efd197
  
3efd197
*** ../vim-7.3.159/src/syntax.c	2011-04-02 15:12:45.000000000 +0200
3efd197
--- src/syntax.c	2011-04-11 15:44:30.000000000 +0200
3efd197
***************
3efd197
*** 8576,8583 ****
3efd197
  		if (iarg & hl_attr_table[i])
3efd197
  		{
3efd197
  		    if (buf[0] != NUL)
3efd197
! 			STRCAT(buf, ",");
3efd197
! 		    STRCAT(buf, hl_name_table[i]);
3efd197
  		    iarg &= ~hl_attr_table[i];	    /* don't want "inverse" */
3efd197
  		}
3efd197
  	    }
3efd197
--- 8576,8583 ----
3efd197
  		if (iarg & hl_attr_table[i])
3efd197
  		{
3efd197
  		    if (buf[0] != NUL)
3efd197
! 			vim_strcat(buf, (char_u *)",", 100);
3efd197
! 		    vim_strcat(buf, (char_u *)hl_name_table[i], 100);
3efd197
  		    iarg &= ~hl_attr_table[i];	    /* don't want "inverse" */
3efd197
  		}
3efd197
  	    }
3efd197
*** ../vim-7.3.159/src/tag.c	2011-02-25 15:13:43.000000000 +0100
3efd197
--- src/tag.c	2011-04-11 15:34:59.000000000 +0200
3efd197
***************
3efd197
*** 806,812 ****
3efd197
  		    p = tag_full_fname(&tagp);
3efd197
  		    if (p == NULL)
3efd197
  			continue;
3efd197
! 		    STRCPY(fname, p);
3efd197
  		    vim_free(p);
3efd197
  
3efd197
  		    /*
3efd197
--- 806,812 ----
3efd197
  		    p = tag_full_fname(&tagp);
3efd197
  		    if (p == NULL)
3efd197
  			continue;
3efd197
! 		    vim_strncpy(fname, p, MAXPATHL);
3efd197
  		    vim_free(p);
3efd197
  
3efd197
  		    /*
3efd197
*** ../vim-7.3.159/src/version.c	2011-04-11 14:29:13.000000000 +0200
3efd197
--- src/version.c	2011-04-11 16:50:53.000000000 +0200
3efd197
***************
3efd197
*** 716,717 ****
3efd197
--- 716,719 ----
3efd197
  {   /* Add new patch number below this line */
3efd197
+ /**/
3efd197
+     160,
3efd197
  /**/
3efd197
3efd197
-- 
3efd197
If someone questions your market projections, simply point out that your
3efd197
target market is "People who are nuts" and "People who will buy any damn
3efd197
thing".  Nobody is going to tell you there aren't enough of those people
3efd197
to go around.
3efd197
				(Scott Adams - The Dilbert principle)
3efd197
3efd197
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
3efd197
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
3efd197
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
3efd197
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///