Do not use subtraction for comparators, this doesn't really matter for

IDs but it breaks key sorting. GitHub issue 5646 from Alexandre Fiori.
This commit is contained in:
nicm
2026-09-28 10:42:01 +00:00
committed by tmux update bot
parent a16e62849d
commit db3950ed07
3 changed files with 50 additions and 26 deletions

View File

@@ -1,4 +1,4 @@
/* $OpenBSD: hyperlinks.c,v 1.5 2026/06/29 16:44:06 nicm Exp $ */ /* $OpenBSD: hyperlinks.c,v 1.6 2026/09/28 10:42:01 nicm Exp $ */
/* /*
* Copyright (c) 2021 Will <author@will.party> * Copyright (c) 2021 Will <author@will.party>
@@ -88,7 +88,11 @@ hyperlinks_by_uri_cmp(struct hyperlinks_uri *left, struct hyperlinks_uri *right)
return (-1); return (-1);
if (*right->internal_id != '\0') if (*right->internal_id != '\0')
return (1); return (1);
return (left->inner - right->inner); if (left->inner < right->inner)
return (-1);
if (left->inner > right->inner)
return (1);
return (0);
} }
r = strcmp(left->internal_id, right->internal_id); r = strcmp(left->internal_id, right->internal_id);
@@ -105,7 +109,11 @@ static int
hyperlinks_by_inner_cmp(struct hyperlinks_uri *left, hyperlinks_by_inner_cmp(struct hyperlinks_uri *left,
struct hyperlinks_uri *right) struct hyperlinks_uri *right)
{ {
return (left->inner - right->inner); if (left->inner < right->inner)
return (-1);
if (left->inner > right->inner)
return (1);
return (0);
} }
RB_PROTOTYPE_STATIC(hyperlinks_by_inner_tree, hyperlinks_uri, by_inner_entry, RB_PROTOTYPE_STATIC(hyperlinks_by_inner_tree, hyperlinks_uri, by_inner_entry,
hyperlinks_by_inner_cmp); hyperlinks_by_inner_cmp);

48
sort.c
View File

@@ -1,4 +1,4 @@
/* $OpenBSD: sort.c,v 1.11 2026/09/28 09:41:53 nicm Exp $ */ /* $OpenBSD: sort.c,v 1.12 2026/09/28 10:42:01 nicm Exp $ */
/* /*
* Copyright (c) 2026 Dane Jensen <dhcjensen@gmail.com> * Copyright (c) 2026 Dane Jensen <dhcjensen@gmail.com>
@@ -25,6 +25,16 @@
static struct sort_criteria *sort_criteria; static struct sort_criteria *sort_criteria;
static int
sort_number_cmp(unsigned long long a, unsigned long long b)
{
if (a < b)
return (-1);
if (a > b)
return (1);
return (0);
}
static void static void
sort_qsort(void *l, u_int len, u_int size, int (*cmp)(const void *, sort_qsort(void *l, u_int len, u_int size, int (*cmp)(const void *,
const void *), struct sort_criteria *sort_crit) const void *), struct sort_criteria *sort_crit)
@@ -73,7 +83,7 @@ sort_buffer_cmp(const void *a0, const void *b0)
result = 0; result = 0;
break; break;
case SORT_SIZE: case SORT_SIZE:
result = pa->size - pb->size; result = sort_number_cmp(pa->size, pb->size);
break; break;
case SORT_ACTIVITY: case SORT_ACTIVITY:
case SORT_INDEX: case SORT_INDEX:
@@ -107,9 +117,9 @@ sort_client_cmp(const void *a0, const void *b0)
result = strcmp(ca->name, cb->name); result = strcmp(ca->name, cb->name);
break; break;
case SORT_SIZE: case SORT_SIZE:
result = ca->tty.sx - cb->tty.sx; result = sort_number_cmp(ca->tty.sx, cb->tty.sx);
if (result == 0) if (result == 0)
result = ca->tty.sy - cb->tty.sy; result = sort_number_cmp(ca->tty.sy, cb->tty.sy);
break; break;
case SORT_CREATION: case SORT_CREATION:
if (timercmp(&ca->creation_time, &cb->creation_time, >)) if (timercmp(&ca->creation_time, &cb->creation_time, >))
@@ -151,7 +161,7 @@ sort_session_cmp(const void *a0, const void *b0)
switch (sort_crit->order) { switch (sort_crit->order) {
case SORT_INDEX: case SORT_INDEX:
result = sa->id - sb->id; result = sort_number_cmp(sa->id, sb->id);
break; break;
case SORT_CREATION: case SORT_CREATION:
if (timercmp(&sa->creation_time, &sb->creation_time, >)) { if (timercmp(&sa->creation_time, &sb->creation_time, >)) {
@@ -203,18 +213,18 @@ sort_pane_cmp(const void *a0, const void *b0)
switch (sort_crit->order) { switch (sort_crit->order) {
case SORT_ACTIVITY: case SORT_ACTIVITY:
result = a->active_point - b->active_point; result = sort_number_cmp(a->active_point, b->active_point);
break; break;
case SORT_CREATION: case SORT_CREATION:
result = a->id - b->id; result = sort_number_cmp(a->id, b->id);
break; break;
case SORT_SIZE: case SORT_SIZE:
result = a->sx * a->sy - b->sx * b->sy; result = sort_number_cmp(a->sx * a->sy, b->sx * b->sy);
break; break;
case SORT_INDEX: case SORT_INDEX:
window_pane_index(a, &ai); window_pane_index(a, &ai);
window_pane_index(b, &bi); window_pane_index(b, &bi);
result = ai - bi; result = sort_number_cmp(ai, bi);
break; break;
case SORT_NAME: case SORT_NAME:
result = strcmp(a->screen->title, b->screen->title); result = strcmp(a->screen->title, b->screen->title);
@@ -222,7 +232,7 @@ sort_pane_cmp(const void *a0, const void *b0)
case SORT_Z: case SORT_Z:
window_pane_zindex(a, &ai); window_pane_zindex(a, &ai);
window_pane_zindex(b, &bi); window_pane_zindex(b, &bi);
result = ai - bi; result = sort_number_cmp(ai, bi);
break; break;
case SORT_MODIFIER: case SORT_MODIFIER:
case SORT_ORDER: case SORT_ORDER:
@@ -278,7 +288,7 @@ sort_winlink_cmp(const void *a0, const void *b0)
result = strcmp(wa->name, wb->name); result = strcmp(wa->name, wb->name);
break; break;
case SORT_SIZE: case SORT_SIZE:
result = wa->sx * wa->sy - wb->sx * wb->sy; result = sort_number_cmp(wa->sx * wa->sy, wb->sx * wb->sy);
break; break;
case SORT_MODIFIER: case SORT_MODIFIER:
case SORT_ORDER: case SORT_ORDER:
@@ -302,14 +312,16 @@ sort_key_binding_cmp(const void *a0, const void *b0)
const struct key_binding *a = *(struct key_binding **)a0; const struct key_binding *a = *(struct key_binding **)a0;
const struct key_binding *b = *(struct key_binding **)b0; const struct key_binding *b = *(struct key_binding **)b0;
int result = 0; int result = 0;
key_code ak, bk;
switch (sort_crit->order) { switch (sort_crit->order) {
case SORT_INDEX: case SORT_INDEX:
result = a->key - b->key; result = sort_number_cmp(a->key, b->key);
break; break;
case SORT_MODIFIER: case SORT_MODIFIER:
result = (a->key & KEYC_MASK_MODIFIERS) - ak = (a->key & KEYC_MASK_MODIFIERS);
(b->key & KEYC_MASK_MODIFIERS); bk = (b->key & KEYC_MASK_MODIFIERS);
result = sort_number_cmp(ak, bk);
break; break;
case SORT_NAME: case SORT_NAME:
result = strcasecmp(a->tablename, b->tablename); result = strcasecmp(a->tablename, b->tablename);
@@ -325,12 +337,8 @@ sort_key_binding_cmp(const void *a0, const void *b0)
if (result == 0) if (result == 0)
result = strcasecmp(a->tablename, b->tablename); result = strcasecmp(a->tablename, b->tablename);
if (result == 0) { if (result == 0)
if (a->key < b->key) result = sort_number_cmp(a->key, b->key);
result = -1;
else if (a->key > b->key)
result = 1;
}
if (sort_crit->reversed) if (sort_crit->reversed)
result = -result; result = -result;

View File

@@ -1,4 +1,4 @@
/* $OpenBSD: window.c,v 1.382 2026/09/28 10:25:43 nicm Exp $ */ /* $OpenBSD: window.c,v 1.383 2026/09/28 10:42:01 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -94,7 +94,11 @@ struct window_pane_prompt {
int int
window_cmp(struct window *w1, struct window *w2) window_cmp(struct window *w1, struct window *w2)
{ {
return (w1->id - w2->id); if (w1->id < w2->id)
return (-1);
if (w1->id > w2->id)
return (1);
return (0);
} }
static void static void
@@ -201,7 +205,11 @@ winlink_cmp(struct winlink *wl1, struct winlink *wl2)
int int
window_pane_cmp(struct window_pane *wp1, struct window_pane *wp2) window_pane_cmp(struct window_pane *wp1, struct window_pane *wp2)
{ {
return (wp1->id - wp2->id); if (wp1->id < wp2->id)
return (-1);
if (wp1->id > wp2->id)
return (1);
return (0);
} }
struct winlink * struct winlink *