Do not overflow window indexes at INT_MAX in new-window -a and

renumber-windows. GitHub issue 5637 from Alexandre Fiori.

(cherry picked from commit f9eb405921)
This commit is contained in:
nicm
2026-09-25 08:46:06 +00:00
committed by Nicholas Marriott
parent bbed41b3f5
commit c3bcda3fd6
2 changed files with 14 additions and 6 deletions

View File

@@ -1,4 +1,4 @@
/* $OpenBSD: session.c,v 1.108 2026/09/21 12:43:36 nicm Exp $ */
/* $OpenBSD: session.c,v 1.109 2026/09/25 08:46:06 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -747,14 +747,21 @@ session_renumber_windows(struct session *s)
struct winlink *wl, *wl1, *wl_new;
struct winlinks old_wins;
struct winlink_stack old_lastw;
u_int n;
int new_idx, new_curw_idx, marked_idx = -1;
/* Start renumbering from the base-index if it's set. */
new_idx = options_get_number(s->options, "base-index");
/* Leave the indexes alone if they do not all fit from there. */
n = winlink_count(&s->windows);
if (n > (u_int)INT_MAX - (u_int)new_idx + 1)
return;
/* Save and replace old window list. */
memcpy(&old_wins, &s->windows, sizeof old_wins);
RB_INIT(&s->windows);
/* Start renumbering from the base-index if it's set. */
new_idx = options_get_number(s->options, "base-index");
new_curw_idx = 0;
/* Go through the winlinks and assign new indexes. */
@@ -769,7 +776,8 @@ session_renumber_windows(struct session *s)
if (wl == s->curw)
new_curw_idx = wl_new->idx;
new_idx++;
if (RB_NEXT(winlinks, &old_wins, wl) != NULL)
new_idx++;
}
/* Fix the stack of last windows now. */

View File

@@ -1,4 +1,4 @@
/* $OpenBSD: window.c,v 1.377 2026/09/21 10:33:16 nicm Exp $ */
/* $OpenBSD: window.c,v 1.379 2026/09/25 08:46:06 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -2437,7 +2437,7 @@ winlink_shuffle_up(struct session *s, struct winlink *wl, int before)
{
int idx, last;
if (wl == NULL)
if (wl == NULL || wl->idx == INT_MAX)
return (-1);
if (before)
idx = wl->idx;