]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
uid-range: add assert to prevent underflow in coalesce loop
authorLuca Boccassi <luca.boccassi@gmail.com>
Sat, 11 Apr 2026 21:04:37 +0000 (22:04 +0100)
committerLuca Boccassi <luca.boccassi@gmail.com>
Sun, 12 Apr 2026 12:24:50 +0000 (13:24 +0100)
Coverity flags range->n_entries - j as a potential underflow
in the memmove size calculation. Add assert(range->n_entries > 0)
before decrementing n_entries, which holds since the loop condition
guarantees j < n_entries.

CID#1548015

Follow-up for 8dcc66cefc8ab489568c737adcba960756d76a3c

src/basic/uid-range.c

index 3d8f8445c45599120bd26a0087be96df91c104b0..62c7d7d928eb72ea54b7fc5ed8c77e97050a2dca 100644 (file)
@@ -75,6 +75,8 @@ static void uid_range_coalesce(UIDRange *range) {
                         if (range->n_entries > j + 1)
                                 memmove(y, y + 1, sizeof(UIDRangeEntry) * (range->n_entries - j - 1));
 
+                        /* Silence static analyzers, n_entries > 0 since j < n_entries holds in the loop condition */
+                        assert(range->n_entries > 0);
                         range->n_entries--;
 
                         /* Silence static analyzers, j cannot be 0 here since it starts at i + 1, i.e. >= 1 */