From 397b5c4bd09115d0df0846fee1b06797b68ae11c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Wed, 4 Nov 2020 17:46:24 +0100 Subject: Introduce spaceship comparison for Processes If currently two unsigned values are compared via `a - b`, in the case b is actually bigger than a, the result will not be an negative number (as -1 is expected) but a huge positive number as the subtraction is an unsigned subtraction. Avoid over-/underflow affected operations; use comparisons. Modern compilers will generate sane code, like: xor eax, eax cmp rdi, rsi seta al sbb eax, 0 ret --- freebsd/FreeBSDProcess.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'freebsd') diff --git a/freebsd/FreeBSDProcess.c b/freebsd/FreeBSDProcess.c index baad5d13..695594e8 100644 --- a/freebsd/FreeBSDProcess.c +++ b/freebsd/FreeBSDProcess.c @@ -111,6 +111,7 @@ static void FreeBSDProcess_writeField(const Process* this, RichString* str, Proc static long FreeBSDProcess_compare(const void* v1, const void* v2) { const FreeBSDProcess *p1, *p2; const Settings *settings = ((const Process*)v1)->settings; + if (settings->direction == 1) { p1 = (const FreeBSDProcess*)v1; p2 = (const FreeBSDProcess*)v2; @@ -118,14 +119,15 @@ static long FreeBSDProcess_compare(const void* v1, const void* v2) { p2 = (const FreeBSDProcess*)v1; p1 = (const FreeBSDProcess*)v2; } + switch ((int) settings->sortKey) { // add FreeBSD-specific fields here case JID: - return (p1->jid - p2->jid); + return SPACESHIP_NUMBER(p1->jid, p2->jid); case JAIL: - return strcmp(p1->jname ? p1->jname : "", p2->jname ? p2->jname : ""); + return SPACESHIP_NULLSTR(p1->jname, p2->jname); case TTY_NR: - return strcmp(p1->ttyPath ? p1->ttyPath : "", p2->ttyPath ? p2->ttyPath : ""); + return SPACESHIP_NULLSTR(p1->ttyPath, p2->ttyPath); default: return Process_compare(v1, v2); } -- cgit v1.2.3