In copy mode, when searching up from the top left corner or searching
down from the bottom right corner (the latter code path actually can't
be reached because of a check which is off by one), the functions
window_copy_search_{up,down}() return without freeing the members of `ss'.
This can be fixed cheaply by calling `screen_free(&ss);' before returning
(but please continue reading below):

------8<------
Index: window-copy.c
===================================================================
RCS file: /cvs/src/usr.bin/tmux/window-copy.c,v
retrieving revision 1.120
diff -u -p -r1.120 window-copy.c
--- window-copy.c       9 Nov 2014 15:13:01 -0000       1.120
+++ window-copy.c       17 Nov 2014 07:53:22 -0000
@@ -1062,8 +1062,10 @@ window_copy_search_up(struct window_pane
        fy = gd->hsize - data->oy + data->cy;
 
        if (fx == 0) {
-               if (fy == 0)
+               if (fy == 0) {
+                       screen_free(&ss);
                        return;
+               }
                fx = gd->sx - 1;
                fy--;
        } else
@@ -1129,8 +1131,10 @@ window_copy_search_down(struct window_pa
        fy = gd->hsize - data->oy + data->cy;
 
        if (fx == gd->sx - 1) {
-               if (fy == gd->hsize + gd->sy)
+               if (fy == gd->hsize + gd->sy - 1) {
+                       screen_free(&ss);
                        return;
+               }
                fx = 0;
                fy++;
        } else
------->8------

However, there also is a bug in the logic in that the functions should
only return if the `wrap-search' option is set to `off'.  Otherwise the
search should wrap around and start from the other end of the screen.

Therefore, I suggest the following slightly more complicated fix which
checks first if the search should wrap around and defers the allocation
of the members of `ss' to later:

Index: window-copy.c
===================================================================
RCS file: /cvs/src/usr.bin/tmux/window-copy.c,v
retrieving revision 1.120
diff -u -p -r1.120 window-copy.c
--- window-copy.c       9 Nov 2014 15:13:01 -0000       1.120
+++ window-copy.c       17 Nov 2014 07:43:32 -0000
@@ -1048,27 +1048,26 @@ window_copy_search_up(struct window_pane
 
        if (*searchstr == '\0')
                return;
-       utf8flag = options_get_number(&wp->window->options, "utf8");
+
        wrapflag = options_get_number(&wp->window->options, "wrap-search");
-       searchlen = screen_write_strlen(utf8flag, "%s", searchstr);
 
-       screen_init(&ss, searchlen, 1, 0);
-       screen_write_start(&ctx, NULL, &ss);
-       memcpy(&gc, &grid_default_cell, sizeof gc);
-       screen_write_nputs(&ctx, -1, &gc, utf8flag, "%s", searchstr);
-       screen_write_stop(&ctx);
+       n = wrapped = 0;
 
        fx = data->cx;
        fy = gd->hsize - data->oy + data->cy;
 
        if (fx == 0) {
-               if (fy == 0)
-                       return;
+               if (fy == 0) {
+                       if (wrapflag) {
+                               fy = gd->hsize + gd->sy - 1;
+                               wrapped = 1;
+                       } else
+                               return;
+               } else
+                       fy--;
                fx = gd->sx - 1;
-               fy--;
        } else
                fx--;
-       n = wrapped = 0;
 
        cis = 1;
        for (ptr = searchstr; *ptr != '\0'; ptr++) {
@@ -1078,6 +1077,15 @@ window_copy_search_up(struct window_pane
                }
        }
 
+       utf8flag = options_get_number(&wp->window->options, "utf8");
+       searchlen = screen_write_strlen(utf8flag, "%s", searchstr);
+
+       screen_init(&ss, searchlen, 1, 0);
+       screen_write_start(&ctx, NULL, &ss);
+       memcpy(&gc, &grid_default_cell, sizeof gc);
+       screen_write_nputs(&ctx, -1, &gc, utf8flag, "%s", searchstr);
+       screen_write_stop(&ctx);
+
 retry:
        sgd = ss.grid;
        for (i = fy + 1; i > 0; i--) {
@@ -1115,27 +1123,26 @@ window_copy_search_down(struct window_pa
 
        if (*searchstr == '\0')
                return;
-       utf8flag = options_get_number(&wp->window->options, "utf8");
+
        wrapflag = options_get_number(&wp->window->options, "wrap-search");
-       searchlen = screen_write_strlen(utf8flag, "%s", searchstr);
 
-       screen_init(&ss, searchlen, 1, 0);
-       screen_write_start(&ctx, NULL, &ss);
-       memcpy(&gc, &grid_default_cell, sizeof gc);
-       screen_write_nputs(&ctx, -1, &gc, utf8flag, "%s", searchstr);
-       screen_write_stop(&ctx);
+       n = wrapped = 0;
 
        fx = data->cx;
        fy = gd->hsize - data->oy + data->cy;
 
        if (fx == gd->sx - 1) {
-               if (fy == gd->hsize + gd->sy)
-                       return;
+               if (fy == gd->hsize + gd->sy - 1) {
+                       if (wrapflag) {
+                               fy = 0;
+                               wrapped = 1;
+                       } else
+                               return;
+               } else
+                       fy++;
                fx = 0;
-               fy++;
        } else
                fx++;
-       n = wrapped = 0;
 
        cis = 1;
        for (ptr = searchstr; *ptr != '\0'; ptr++) {
@@ -1144,6 +1151,15 @@ window_copy_search_down(struct window_pa
                        break;
                }
        }
+
+       utf8flag = options_get_number(&wp->window->options, "utf8");
+       searchlen = screen_write_strlen(utf8flag, "%s", searchstr);
+
+       screen_init(&ss, searchlen, 1, 0);
+       screen_write_start(&ctx, NULL, &ss);
+       memcpy(&gc, &grid_default_cell, sizeof gc);
+       screen_write_nputs(&ctx, -1, &gc, utf8flag, "%s", searchstr);
+       screen_write_stop(&ctx);
 
 retry:
        sgd = ss.grid;

Reply via email to