]> git.ipfire.org Git - thirdparty/shadow.git/commit
lib/idmapping.c: get_map_ranges(): Merge two input checks into a simpler one
authorAlejandro Colomar <alx@kernel.org>
Thu, 11 Jan 2024 17:28:07 +0000 (18:28 +0100)
committerSerge Hallyn <serge@hallyn.com>
Wed, 13 Mar 2024 15:55:00 +0000 (10:55 -0500)
commit26deef6945a8bd2a36d7fb9d281ee2188cec99c5
tree3917c0c88c82bbae8f0a20f81d394d1ef594ccca
parentd2f2c1877a30849912e1f3490a21808fa87fcd4c
lib/idmapping.c: get_map_ranges(): Merge two input checks into a simpler one

Previously, we were performing the following two checks:

-       if (ranges != ((argc + 2) / 3)) {
-       if ((ranges * 3) > argc) {

Let's draw a table of the possible input that would pass the first check:

argc: 0 1 2 3 4 5 6 7 8 9
rng: 0 1 1 1 2 2 2 3 3 3
a+2/3*3:0 3 3 3 6 6 6 9 9 9 <-- this is  roundup(argc, 3);
a+2/3: 0 1 1 1 2 2 2 3 3 3 <-- this is  roundup(argc, 3) / 3;
rng*3: 0 3 3 3 6 6 6 9 9 9

From those, let's extract those that would also pass the second check:

argc: 0     3     6     9
rng: 0     1     2     3
rng*3: 0     3     6     9

We can see that there's a simple check for this input:

+       if (ranges * 3 != argc) {

As a sanity check, let's draw a table of the acceptable input with that
check:

rng: 0     1     2     3
rng*3: 0     3     6     9
argc: 0     3     6     9

Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/idmapping.c