Add a 10 second timeout to prevent searches taking too much time, from

Anindya Mukherjee.
pull/2151/head
nicm 2020-04-01 09:36:37 +00:00
parent dd2fdcda79
commit 567b27e10a
1 changed files with 39 additions and 5 deletions

View File

@ -261,6 +261,9 @@ struct window_copy_mode_data {
int searchy;
int searcho;
int timeout; /* search has timed out */
#define WINDOW_COPY_SEARCH_TIMEOUT 10
int jumptype;
char jumpchar;
@ -316,6 +319,7 @@ window_copy_common_init(struct window_mode_entry *wme)
}
data->searchmark = NULL;
data->searchx = data->searchy = data->searcho = -1;
data->timeout = 0;
data->jumptype = WINDOW_COPY_OFF;
data->jumpchar = '\0';
@ -680,8 +684,8 @@ window_copy_resize(struct window_mode_entry *wme, u_int sx, u_int sy)
window_copy_write_lines(wme, &ctx, 0, screen_size_y(s) - 1);
screen_write_stop(&ctx);
if (search)
window_copy_search_marks(wme, NULL, 1);
if (search && !data->timeout)
window_copy_search_marks(wme, NULL, data->searchregex);
data->searchx = data->cx;
data->searchy = data->cy;
data->searcho = data->oy;
@ -1800,6 +1804,7 @@ window_copy_cmd_search_backward(struct window_copy_cmd_state *cs)
if (data->searchstr != NULL) {
data->searchtype = WINDOW_COPY_SEARCHUP;
data->searchregex = 1;
data->timeout = 0;
for (; np != 0; np--)
window_copy_search_up(wme, 1);
}
@ -1819,6 +1824,7 @@ window_copy_cmd_search_backward_text(struct window_copy_cmd_state *cs)
if (data->searchstr != NULL) {
data->searchtype = WINDOW_COPY_SEARCHUP;
data->searchregex = 0;
data->timeout = 0;
for (; np != 0; np--)
window_copy_search_up(wme, 0);
}
@ -1838,6 +1844,7 @@ window_copy_cmd_search_forward(struct window_copy_cmd_state *cs)
if (data->searchstr != NULL) {
data->searchtype = WINDOW_COPY_SEARCHDOWN;
data->searchregex = 1;
data->timeout = 0;
for (; np != 0; np--)
window_copy_search_down(wme, 1);
}
@ -1857,6 +1864,7 @@ window_copy_cmd_search_forward_text(struct window_copy_cmd_state *cs)
if (data->searchstr != NULL) {
data->searchtype = WINDOW_COPY_SEARCHDOWN;
data->searchregex = 0;
data->timeout = 0;
for (; np != 0; np--)
window_copy_search_down(wme, 0);
}
@ -1873,6 +1881,8 @@ window_copy_cmd_search_backward_incremental(struct window_copy_cmd_state *cs)
char prefix;
enum window_copy_cmd_action action = WINDOW_COPY_CMD_NOTHING;
data->timeout = 0;
prefix = *argument++;
if (data->searchx == -1 || data->searchy == -1) {
data->searchx = data->cx;
@ -1924,6 +1934,8 @@ window_copy_cmd_search_forward_incremental(struct window_copy_cmd_state *cs)
char prefix;
enum window_copy_cmd_action action = WINDOW_COPY_CMD_NOTHING;
data->timeout = 0;
prefix = *argument++;
if (data->searchx == -1 || data->searchy == -1) {
data->searchx = data->cx;
@ -2721,6 +2733,9 @@ window_copy_search(struct window_mode_entry *wme, int direction, int regex)
if (regex && str[strcspn(str, "^$*+()?[].\\")] == '\0')
regex = 0;
if (data->timeout)
return (0);
free(wp->searchstr);
wp->searchstr = xstrdup(str);
wp->searchregex = regex;
@ -2768,6 +2783,7 @@ window_copy_search_marks(struct window_mode_entry *wme, struct screen *ssp,
u_int ssize = 1;
char *sbuf;
regex_t reg;
time_t tstart, t;
if (ssp == NULL) {
width = screen_write_strlen("%s", data->searchstr);
@ -2797,6 +2813,7 @@ window_copy_search_marks(struct window_mode_entry *wme, struct screen *ssp,
return (0);
}
}
time(&tstart);
for (py = 0; py < gd->hsize + gd->sy; py++) {
px = 0;
for (;;) {
@ -2822,11 +2839,21 @@ window_copy_search_marks(struct window_mode_entry *wme, struct screen *ssp,
px++;
}
time(&t);
if (t - tstart > WINDOW_COPY_SEARCH_TIMEOUT) {
data->timeout = 1;
break;
}
}
if (regex) {
free(sbuf);
regfree(&reg);
}
if (data->timeout) {
window_copy_clear_marks(wme);
return (1);
}
if (which != -1)
data->searchthis = 1 + nfound - which;
@ -2836,7 +2863,7 @@ window_copy_search_marks(struct window_mode_entry *wme, struct screen *ssp,
if (ssp == &ss)
screen_free(&ss);
return (nfound);
return (1);
}
static void
@ -2895,8 +2922,15 @@ window_copy_write_line(struct window_mode_entry *wme,
if (py == 0 && s->rupper < s->rlower && !data->hide_position) {
if (data->searchmark == NULL) {
size = xsnprintf(hdr, sizeof hdr,
"[%u/%u]", data->oy, screen_hsize(data->backing));
if (data->timeout) {
size = xsnprintf(hdr, sizeof hdr,
"(timed out) [%u/%u]", data->oy,
screen_hsize(data->backing));
} else {
size = xsnprintf(hdr, sizeof hdr,
"[%u/%u]", data->oy,
screen_hsize(data->backing));
}
} else {
if (data->searchthis == -1) {
size = xsnprintf(hdr, sizeof hdr,