]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
uid-range: escape from loop earlier
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 16 Sep 2022 01:29:56 +0000 (10:29 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 16 Sep 2022 11:50:00 +0000 (20:50 +0900)
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.

src/basic/uid-range.c

index 2f16bb8916caf152fb14e14a112249d89baa9a1d..73dd4b28921cd8cd8115e999ec2c356544d30c6b 100644 (file)
@@ -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--;
                 }
         }
 }