patch 9.1.1367: too many strlen() calls in gui.c

Commit: 
https://github.com/vim/vim/commit/d2fcbb465d354029e9b085f7fbadc9c0a791b155
Author: John Marriott <basil...@internode.on.net>
Date:   Wed May 7 19:59:42 2025 +0200

    patch 9.1.1367: too many strlen() calls in gui.c
    
    Problem:  too many strlen() calls in gui.c
    Solution: refactor gui.c slightly (John Marriott)
    
    This does the following changes:
    - use macro STRCMP() instead of strcmp().
    - refactor gui_outstr_nowrap() to remove call to STRLEN().
    - refactor get_tabline_label() in attempt to simply it. At the same time
      use standard looping construct for iterating over windows in a tab.
      Move variables closer to where they are used. Add check that we don't
      exceed size of NameBuff.
    - small optimisation in get_find_dialog_text() to measure the string
      length once.
    
    closes: #17269
    
    Signed-off-by: John Marriott <basil...@internode.on.net>
    Signed-off-by: Christian Brabandt <c...@256bit.org>

diff --git a/src/gui.c b/src/gui.c
index 0b9e32899..804d39c15 100644
--- a/src/gui.c
+++ b/src/gui.c
@@ -357,7 +357,7 @@ gui_read_child_pipe(int fd)
     if (bytes_read < 0)
        return GUI_CHILD_IO_ERROR;
     buffer[bytes_read] = NUL;
-    if (strcmp(buffer, "ok") == 0)
+    if (STRCMP(buffer, "ok") == 0)
        return GUI_CHILD_OK;
     return GUI_CHILD_FAILED;
 }
@@ -2330,6 +2330,8 @@ gui_outstr_nowrap(
 # endif
        )
     {
+       size_t  slen = 2;                   // 2 spaces by default
+
 # ifdef FEAT_NETBEANS_INTG
        if (*s == MULTISIGN_BYTE)
            multi_sign = TRUE;
@@ -2338,14 +2340,16 @@ gui_outstr_nowrap(
        if (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u' &&
                (curwin->w_p_nu || curwin->w_p_rnu))
        {
-           sprintf((char *)extra, "%*c ", number_width(curwin), ' ');
-           s = extra;
+           int n = number_width(curwin);
+
+           slen = MIN(n, (int)sizeof(extra) - 1);
        }
-       else
-           s = (char_u *)"  ";
+       vim_memset(extra, ' ', slen);
+       extra[slen] = NUL;
+       s = extra;
        if (len == 1 && col > 0)
            --col;
-       len = (int)STRLEN(s);
+       len = (int)slen;
        if (len > 2)
            // right align sign icon in the number column
            signcol = col + len - 3;
@@ -3756,10 +3760,6 @@ get_tabline_label(
     tabpage_T  *tp,
     int                tooltip)        // TRUE: get tooltip
 {
-    int                modified = FALSE;
-    char_u     buf[40];
-    int                wincount;
-    win_T      *wp;
     char_u     **opt;
 
     // Use 'guitablabel' or 'guitabtooltip' if it's set.
@@ -3805,26 +3805,42 @@ get_tabline_label(
     // use a default label.
     if (**opt == NUL || *NameBuff == NUL)
     {
+       win_T   *wp;
+       int     wincount = 0;
+       int     modified = FALSE;
+
        // Get the buffer name into NameBuff[] and shorten it.
        get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
        if (!tooltip)
            shorten_dir(NameBuff);
 
-       wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
-       for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
+       FOR_ALL_WINDOWS_IN_TAB(tp, wp)
+       {
+           ++wincount;
            if (bufIsChanged(wp->w_buffer))
                modified = TRUE;
+       }
+
        if (modified || wincount > 1)
        {
+           char_u  buf[40] = "+ ";             // Tentatively assume modified 
only
+           size_t  buflen = 2;
+           size_t  NameBufflen = STRLEN(NameBuff);
+
            if (wincount > 1)
-               vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
-           else
-               buf[0] = NUL;
-           if (modified)
-               STRCAT(buf, "+");
-           STRCAT(buf, " ");
-           STRMOVE(NameBuff + STRLEN(buf), NameBuff);
-           mch_memmove(NameBuff, buf, STRLEN(buf));
+               buflen = vim_snprintf_safelen(
+                   (char *)buf,
+                   sizeof(buf),
+                   "%d%s ",
+                   wincount,
+                   modified ? "+" : "");
+
+           // Make sure resulting NameBuff will not exceed it's bounds.
+           if (NameBufflen + buflen < MAXPATHL)
+           {
+               mch_memmove(NameBuff + buflen, NameBuff, NameBufflen + 1);      
// +1 for NUL
+               mch_memmove(NameBuff, buf, buflen);
+           }
        }
     }
 }
@@ -5158,10 +5174,11 @@ get_find_dialog_text(
        text = arg;
     if (text != NULL)
     {
-       text = vim_strsave(text);
+       int len = (int)STRLEN(text);
+
+       text = vim_strnsave(text, len);
        if (text != NULL)
        {
-           int len = (int)STRLEN(text);
            int i;
 
            // Remove "\V"
diff --git a/src/version.c b/src/version.c
index e5c4a6896..721007330 100644
--- a/src/version.c
+++ b/src/version.c
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1367,
 /**/
     1366,
 /**/

-- 
-- 
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 vim_dev+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/vim_dev/E1uCjIO-00DxVE-98%40256bit.org.

Raspunde prin e-mail lui