diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2022-09-16 03:29:56 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2022-09-16 13:50:00 +0200 |
commit | b5739767dac70a97abee65e555bded55b1f38c03 (patch) | |
tree | 6d3f78022fe89c643ddbfce3b11b524841ad479d | |
parent | uid-range: sort uid range entries in uid_range_coalesce() (diff) | |
download | systemd-b5739767dac70a97abee65e555bded55b1f38c03.tar.xz systemd-b5739767dac70a97abee65e555bded55b1f38c03.zip |
uid-range: escape from loop earlier
The array of uid range entries are already sorted. Hence, if x and y are
does not have intersection, then the remaining entries neither have
intersection with x.
-rw-r--r-- | src/basic/uid-range.c | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/src/basic/uid-range.c b/src/basic/uid-range.c index 2f16bb8916..73dd4b2892 100644 --- a/src/basic/uid-range.c +++ b/src/basic/uid-range.c @@ -45,24 +45,26 @@ static void uid_range_coalesce(UidRange **p, size_t *n) { typesafe_qsort(*p, *n, uid_range_compare); for (size_t i = 0; i < *n; i++) { + UidRange *x = (*p) + i; + for (size_t j = i + 1; j < *n; j++) { - UidRange *x = (*p)+i, *y = (*p)+j; + UidRange *y = (*p)+j; + uid_t begin, end; - if (uid_range_intersect(x, y->start, y->nr)) { - uid_t begin, end; + if (!uid_range_intersect(x, y->start, y->nr)) + break; - begin = MIN(x->start, y->start); - end = MAX(x->start + x->nr, y->start + y->nr); + begin = MIN(x->start, y->start); + end = MAX(x->start + x->nr, y->start + y->nr); - x->start = begin; - x->nr = end - begin; + x->start = begin; + x->nr = end - begin; - if (*n > j+1) - memmove(y, y+1, sizeof(UidRange) * (*n - j -1)); + if (*n > j+1) + memmove(y, y+1, sizeof(UidRange) * (*n - j -1)); - (*n)--; - j--; - } + (*n)--; + j--; } } } |