diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2022-09-16 03:20:55 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2022-09-16 13:49:55 +0200 |
commit | 34eeba475d6f5fd464357df35012a30498619880 (patch) | |
tree | 26ed3050ab078340d981e73b2ffc7ec988d592db | |
parent | uid-range: use parse_uid_range() (diff) | |
download | systemd-34eeba475d6f5fd464357df35012a30498619880.tar.xz systemd-34eeba475d6f5fd464357df35012a30498619880.zip |
uid-range: sort uid range entries in uid_range_coalesce()
As the logic in uid_range_coalesce() assumes the array of entries are
already sorted.
No functional changes, just refactoring.
-rw-r--r-- | src/basic/uid-range.c | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/src/basic/uid-range.c b/src/basic/uid-range.c index f7a9a070c7..2f16bb8916 100644 --- a/src/basic/uid-range.c +++ b/src/basic/uid-range.c @@ -22,10 +22,28 @@ static bool uid_range_intersect(UidRange *range, uid_t start, uid_t nr) { range->start + range->nr >= start; } +static int uid_range_compare(const UidRange *a, const UidRange *b) { + int r; + + assert(a); + assert(b); + + r = CMP(a->start, b->start); + if (r != 0) + return r; + + return CMP(a->nr, b->nr); +} + static void uid_range_coalesce(UidRange **p, size_t *n) { assert(p); assert(n); + if (*n <= 1) + return; + + typesafe_qsort(*p, *n, uid_range_compare); + for (size_t i = 0; i < *n; i++) { for (size_t j = i + 1; j < *n; j++) { UidRange *x = (*p)+i, *y = (*p)+j; @@ -49,16 +67,6 @@ static void uid_range_coalesce(UidRange **p, size_t *n) { } } -static int uid_range_compare(const UidRange *a, const UidRange *b) { - int r; - - r = CMP(a->start, b->start); - if (r != 0) - return r; - - return CMP(a->nr, b->nr); -} - int uid_range_add(UidRange **p, size_t *n, uid_t start, uid_t nr) { bool found = false; UidRange *x; @@ -102,7 +110,6 @@ int uid_range_add(UidRange **p, size_t *n, uid_t start, uid_t nr) { x->nr = nr; } - typesafe_qsort(*p, *n, uid_range_compare); uid_range_coalesce(p, n); return *n; |