All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Christian Göttsche" <cgzones@googlemail.com>
To: selinux@vger.kernel.org
Subject: [PATCH 3/3] mcstrans: ensure transitivity in compare functions
Date: Wed, 31 Jan 2024 13:56:12 +0100	[thread overview]
Message-ID: <20240131125623.45758-3-cgzones@googlemail.com> (raw)
In-Reply-To: <20240131125623.45758-1-cgzones@googlemail.com>

Ensure comparison functions used by qsort(3) fulfill transitivity, since
otherwise the resulting array might not be sorted correctly or worse[1]
in case of integer overflows.

[1]: https://www.qualys.com/2024/01/30/qsort.txt

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
 mcstrans/src/mcstrans.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/mcstrans/src/mcstrans.c b/mcstrans/src/mcstrans.c
index af3f507e..fded3235 100644
--- a/mcstrans/src/mcstrans.c
+++ b/mcstrans/src/mcstrans.c
@@ -952,9 +952,13 @@ find_in_hashtable(const char *range, domain_t *domain, context_map_node_t **tabl
 	return trans;
 }
 
+#define spaceship_cmp(a, b) (((a) > (b)) - ((a) < (b)))
+
 static int
 string_size(const void *p1, const void *p2) {
-	return strlen(*(char **)p2) - strlen(*(char **)p1);
+	size_t len1 = strlen(*(const char *const *)p2);
+	size_t len2 = strlen(*(const char *const *)p1);
+	return spaceship_cmp(len1, len2);
 }
 
 static int
@@ -965,7 +969,7 @@ word_size(const void *p1, const void *p2) {
 	int w2_len=strlen(w2->text);
 	if (w1_len == w2_len)
 		return strcmp(w1->text, w2->text);
-	return (w2_len - w1_len);
+	return spaceship_cmp(w2_len, w1_len);
 }
 
 static void
-- 
2.43.0


  parent reply	other threads:[~2024-01-31 12:56 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-31 12:56 [PATCH 1/3] libsepol: ensure transitivity in compare functions Christian Göttsche
2024-01-31 12:56 ` [PATCH 2/3] libsepol/cil: " Christian Göttsche
2024-01-31 12:56 ` Christian Göttsche [this message]
2024-02-22 15:14 ` [PATCH 1/3] libsepol: " James Carter
2024-03-04 19:12   ` James Carter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240131125623.45758-3-cgzones@googlemail.com \
    --to=cgzones@googlemail.com \
    --cc=selinux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.