Patch 8.2.4543
Problem:    Coverity warning for refactored tag search code.
Solution:   Avoid the warnings.  Update comments.  Add one more test case.
            (Yegappan Lakshmanan, closes #9928)
Files:      src/tag.c, src/testdir/test_tagjump.vim


*** ../vim-8.2.4542/src/tag.c   2022-03-10 18:36:50.874611102 +0000
--- src/tag.c   2022-03-11 12:02:52.387896012 +0000
***************
*** 1663,1669 ****
      char_u     *lbuf;                 // line buffer
      int               lbuf_size;              // length of lbuf
  #ifdef FEAT_EMACS_TAGS
!     int               is_etag;                // current file is emaces style
      char_u    *ebuf;                  // additional buffer for etag fname
  #endif
      int               match_count;            // number of matches found
--- 1663,1669 ----
      char_u     *lbuf;                 // line buffer
      int               lbuf_size;              // length of lbuf
  #ifdef FEAT_EMACS_TAGS
!     int               is_etag;                // current file is emacs style
      char_u    *ebuf;                  // additional buffer for etag fname
  #endif
      int               match_count;            // number of matches found
***************
*** 1673,1679 ****
  } findtags_state_T;
  
  /*
!  * Initialize the state used by find_tags()
   */
      static int
  findtags_state_init(
--- 1673,1680 ----
  } findtags_state_T;
  
  /*
!  * Initialize the state used by find_tags().
!  * Returns OK on success and FAIL on memory allocation failure.
   */
      static int
  findtags_state_init(
***************
*** 1745,1751 ****
  /*
   * Initialize the language and priority used for searching tags in a Vim help
   * file.
!  * Returns TRUE to process the help file and FALSE to skip the file.
   */
      static int
  findtags_in_help_init(findtags_state_T *st)
--- 1746,1752 ----
  /*
   * Initialize the language and priority used for searching tags in a Vim help
   * file.
!  * Returns TRUE to process the help file for tags and FALSE to skip the file.
   */
      static int
  findtags_in_help_init(findtags_state_T *st)
***************
*** 1762,1768 ****
        // language name in help_lang[].
        i = (int)STRLEN(st->tag_fname);
        if (i > 3 && st->tag_fname[i - 3] == '-')
!           STRCPY(st->help_lang, st->tag_fname + i - 2);
        else
            STRCPY(st->help_lang, "en");
      }
--- 1763,1769 ----
        // language name in help_lang[].
        i = (int)STRLEN(st->tag_fname);
        if (i > 3 && st->tag_fname[i - 3] == '-')
!           vim_strncpy(st->help_lang, st->tag_fname + i - 2, 2);
        else
            STRCPY(st->help_lang, "en");
      }
***************
*** 1846,1852 ****
  static int incstack_idx = 0;  // index in incstack
  
  /*
!  * Free the include tags file stack.
   */
      static void
  emacs_tags_incstack_free(void)
--- 1847,1853 ----
  static int incstack_idx = 0;  // index in incstack
  
  /*
!  * Free the emacs include tags file stack.
   */
      static void
  emacs_tags_incstack_free(void)
***************
*** 1863,1871 ****
  /*
   * Emacs tags line with CTRL-L: New file name on next line.
   * The file name is followed by a ','.  Remember etag file name in ebuf.
!  * Returns a FILE pointer to the tags file. If another tags file is included,
!  * then returns a pointer to the new tags file. The old file pointer is saved
!  * in incstack.
   */
      static void
  emacs_tags_new_filename(findtags_state_T *st)
--- 1864,1872 ----
  /*
   * Emacs tags line with CTRL-L: New file name on next line.
   * The file name is followed by a ','.  Remember etag file name in ebuf.
!  * The FILE pointer to the tags file is stored in 'st->fp'.  If another tags
!  * file is included, then the FILE pointer to the new tags file is stored in
!  * 'st->fp'. The old file pointer is saved in incstack.
   */
      static void
  emacs_tags_new_filename(findtags_state_T *st)
***************
*** 1918,1925 ****
        st->fp = incstack[incstack_idx].fp;
        vim_free(incstack[incstack_idx].etag_fname);
      }
- 
-     return;
  }
  
  /*
--- 1919,1924 ----
***************
*** 1927,1933 ****
   * file, then pop it from the incstack and continue processing the parent tags
   * file. Otherwise, processed all the tags.
   * Returns TRUE if an included tags file is popped and processing should
!  * continue with the parent tags file. Otherwise returns FALSE.
   */
      static int
  emacs_tags_file_eof(findtags_state_T *st)
--- 1926,1932 ----
   * file, then pop it from the incstack and continue processing the parent tags
   * file. Otherwise, processed all the tags.
   * Returns TRUE if an included tags file is popped and processing should
!  * continue with the parent tags file. Returns FALSE to stop processing tags.
   */
      static int
  emacs_tags_file_eof(findtags_state_T *st)
***************
*** 1947,1953 ****
  
  /*
   * Parse a line from an emacs-style tags file.
!  * Returns OK is the line is parsed successfully, otherwise FALSE.
   */
      static int
  emacs_tags_parse_line(char_u *lbuf, tagptrs_T *tagp)
--- 1946,1953 ----
  
  /*
   * Parse a line from an emacs-style tags file.
!  * Returns OK if the line is parsed successfully, returns FAIL if the line is
!  * not terminated by a newline.
   */
      static int
  emacs_tags_parse_line(char_u *lbuf, tagptrs_T *tagp)
***************
*** 2167,2174 ****
      int               noic = (st->flags & TAG_NOIC);
      off_T     filesize;
  
!     // The header ends when the line sorts below "!_TAG_".  When
!     // case is folded lower case letters sort before "_".
      if (STRNCMP(st->lbuf, "!_TAG_", 6) <= 0
            || (st->lbuf[0] == '!' && ASCII_ISLOWER(st->lbuf[1])))
        return findtags_hdr_parse(st);
--- 2167,2174 ----
      int               noic = (st->flags & TAG_NOIC);
      off_T     filesize;
  
!     // The header ends when the line sorts below "!_TAG_".  When case is
!     // folded lower case letters sort before "_".
      if (STRNCMP(st->lbuf, "!_TAG_", 6) <= 0
            || (st->lbuf[0] == '!' && ASCII_ISLOWER(st->lbuf[1])))
        return findtags_hdr_parse(st);
***************
*** 2241,2247 ****
  /*
   * Parse a tag line read from a tags file.
   * Returns OK if a tags line is successfully parsed.
!  * Returns FAIL if an error is encountered.
   */
      static int
  findtags_parse_line(findtags_state_T *st, tagptrs_T *tagpp)
--- 2241,2247 ----
  /*
   * Parse a tag line read from a tags file.
   * Returns OK if a tags line is successfully parsed.
!  * Returns FAIL if a format error is encountered.
   */
      static int
  findtags_parse_line(findtags_state_T *st, tagptrs_T *tagpp)
***************
*** 2747,2756 ****
  
  /*
   * Read and get all the tags from file st->tag_fname.
!  * Returns OK if all the tags are processed successfully and FAIL is a tag
!  * format error is encountered.
   */
!     static int
  findtags_get_all_tags(
      findtags_state_T          *st,
      findtags_match_args_T     *margs,
--- 2747,2755 ----
  
  /*
   * Read and get all the tags from file st->tag_fname.
!  * Sets 'st->stop_searching' to TRUE to stop searching for additional tags.
   */
!     static void
  findtags_get_all_tags(
      findtags_state_T          *st,
      findtags_match_args_T     *margs,
***************
*** 2846,2852 ****
            {
                if (st->fp != NULL)
                    fclose(st->fp);
!               break;
            }
  
            if (st->state == TS_STEP_FORWARD)
--- 2845,2853 ----
            {
                if (st->fp != NULL)
                    fclose(st->fp);
!               st->fp = NULL;
!               st->stop_searching = TRUE;
!               return;
            }
  
            if (st->state == TS_STEP_FORWARD)
***************
*** 2859,2865 ****
        }
  
        if (findtags_parse_line(st, &tagp) == FAIL)
!           return FAIL;
  
        retval = findtags_match_tag(st, &tagp, margs, &search_info);
        if (retval == TAG_MATCH_NEXT)
--- 2860,2874 ----
        }
  
        if (findtags_parse_line(st, &tagp) == FAIL)
!       {
!           semsg(_(e_format_error_in_tags_file_str), st->tag_fname);
! #ifdef FEAT_CSCOPE
!           if (!use_cscope)
! #endif
!               semsg(_("Before byte %ld"), (long)vim_ftell(st->fp));
!           st->stop_searching = TRUE;
!           return;
!       }
  
        retval = findtags_match_tag(st, &tagp, margs, &search_info);
        if (retval == TAG_MATCH_NEXT)
***************
*** 2875,2896 ****
                break;
        }
      } // forever
- 
-     return OK;
  }
  
  /*
   * Search for tags matching 'st->orgpat.pat' in the 'st->tag_fname' tags file.
   * Information needed to search for the tags is in the 'st' state structure.
!  * The matching tags are returned in 'st'.
!  * Returns OK if successfully processed the file and FAIL on memory allocation
!  * failure.
   */
!     static int
  findtags_in_file(findtags_state_T *st, char_u *buf_ffname)
  {
      findtags_match_args_T margs;
-     int               line_error = FALSE;             // syntax error
  #ifdef FEAT_CSCOPE
      int               use_cscope = FALSE;
  #endif
--- 2884,2901 ----
                break;
        }
      } // forever
  }
  
  /*
   * Search for tags matching 'st->orgpat.pat' in the 'st->tag_fname' tags file.
   * Information needed to search for the tags is in the 'st' state structure.
!  * The matching tags are returned in 'st'. If an error is encountered, then
!  * 'st->stop_searching' is set to TRUE.
   */
!     static void
  findtags_in_file(findtags_state_T *st, char_u *buf_ffname)
  {
      findtags_match_args_T margs;
  #ifdef FEAT_CSCOPE
      int               use_cscope = FALSE;
  #endif
***************
*** 2913,2925 ****
        if (curbuf->b_help)
        {
            if (!findtags_in_help_init(st))
!               return OK;
        }
  #endif
  
        st->fp = mch_fopen((char *)st->tag_fname, "r");
        if (st->fp == NULL)
!           return OK;
  
        if (p_verbose >= 5)
        {
--- 2918,2930 ----
        if (curbuf->b_help)
        {
            if (!findtags_in_help_init(st))
!               return;
        }
  #endif
  
        st->fp = mch_fopen((char *)st->tag_fname, "r");
        if (st->fp == NULL)
!           return;
  
        if (p_verbose >= 5)
        {
***************
*** 2936,2957 ****
  #endif
  
      // Read and parse the lines in the file one by one
!     if (findtags_get_all_tags(st, &margs, buf_ffname) == FAIL)
!       line_error = TRUE;
! 
!     if (line_error)
!     {
!       semsg(_(e_format_error_in_tags_file_str), st->tag_fname);
! #ifdef FEAT_CSCOPE
!       if (!use_cscope)
! #endif
!           semsg(_("Before byte %ld"), (long)vim_ftell(st->fp));
!       st->stop_searching = TRUE;
!       line_error = FALSE;
!     }
  
      if (st->fp != NULL)
        fclose(st->fp);
  #ifdef FEAT_EMACS_TAGS
      emacs_tags_incstack_free();
  #endif
--- 2941,2951 ----
  #endif
  
      // Read and parse the lines in the file one by one
!     findtags_get_all_tags(st, &margs, buf_ffname);
  
      if (st->fp != NULL)
        fclose(st->fp);
+     st->fp = NULL;
  #ifdef FEAT_EMACS_TAGS
      emacs_tags_incstack_free();
  #endif
***************
*** 2964,2971 ****
      // Stop searching if sufficient tags have been found.
      if (st->match_count >= st->mincount)
        st->stop_searching = TRUE;
- 
-     return OK;
  }
  
  /*
--- 2958,2963 ----
***************
*** 3188,3195 ****
                get_tagfname(&tn, first_file, st.tag_fname) == OK;
                                                           first_file = FALSE)
        {
!         if (findtags_in_file(&st, buf_ffname) == FAIL)
!             goto findtag_end;
          if (st.stop_searching
  #ifdef FEAT_CSCOPE
                  || use_cscope
--- 3180,3186 ----
                get_tagfname(&tn, first_file, st.tag_fname) == OK;
                                                           first_file = FALSE)
        {
!         findtags_in_file(&st, buf_ffname);
          if (st.stop_searching
  #ifdef FEAT_CSCOPE
                  || use_cscope
*** ../vim-8.2.4542/src/testdir/test_tagjump.vim        2022-03-10 
18:36:50.874611102 +0000
--- src/testdir/test_tagjump.vim        2022-03-11 12:02:52.387896012 +0000
***************
*** 1578,1583 ****
--- 1578,1592 ----
    call assert_equal(3, line('.'))
    %bw!
  
+   " Binary search fails on EOF
+   call writefile([
+         \ "!_TAG_FILE_ENCODING\tutf-8\t//",
+         \ "!_TAG_FILE_SORTED\t1\t/0=unsorted, 1=sorted, 2=foldcase/",
+         \ "bar\tXfoo\t1",
+         \ "foo\tXfoo\t2"],
+         \ 'Xtags')
+   call assert_fails('tag bbb', 'E426:')
+ 
    call delete('Xtags')
    call delete('Xfoo')
    set tags& tagbsearch&
*** ../vim-8.2.4542/src/version.c       2022-03-10 21:53:40.829910566 +0000
--- src/version.c       2022-03-11 12:04:13.291733719 +0000
***************
*** 752,753 ****
--- 752,755 ----
  {   /* Add new patch number below this line */
+ /**/
+     4543,
  /**/

-- 
hundred-and-one symptoms of being an internet addict:
229. You spend so much time thinking what to add on this list.

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///                                                                      \\\
\\\        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/20220311120656.E48A91C0E03%40moolenaar.net.

Raspunde prin e-mail lui