diff options
author | Eric W. Biederman <ebiederm@xmission.com> | 2021-04-30 13:00:26 -0500 |
---|---|---|
committer | Eric W. Biederman <ebiederm@xmission.com> | 2021-04-30 14:25:40 -0500 |
commit | f928ef685db5d9b82c1c1e24e229c167426c5a1f (patch) | |
tree | 01cb5cfe5162b826533f25fa05b738b8eb9652e9 | |
parent | 9b624988221b7cb259da77dd67ef0ee4d6b56d12 (diff) | |
download | linux-f928ef685db5d9b82c1c1e24e229c167426c5a1f.tar.bz2 |
ucounts: Silence warning in dec_rlimit_ucounts
Dan Carpenter <dan.carpenter@oracle.com> wrote:
>
> url: https://github.com/0day-ci/linux/commits/legion-kernel-org/Count-rlimits-in-each-user-namespace/20210427-162857
> base: https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git next
> config: arc-randconfig-m031-20210426 (attached as .config)
> compiler: arceb-elf-gcc (GCC) 9.3.0
>
> If you fix the issue, kindly add following tag as appropriate
> Reported-by: kernel test robot <lkp@intel.com>
> Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
>
> smatch warnings:
> kernel/ucount.c:270 dec_rlimit_ucounts() error: uninitialized symbol 'new'.
>
> vim +/new +270 kernel/ucount.c
>
> 176ec2b092cc22 Alexey Gladkov 2021-04-22 260 bool dec_rlimit_ucounts(struct ucounts *ucounts, enum ucount_type type, long v)
> 176ec2b092cc22 Alexey Gladkov 2021-04-22 261 {
> 176ec2b092cc22 Alexey Gladkov 2021-04-22 262 struct ucounts *iter;
> 176ec2b092cc22 Alexey Gladkov 2021-04-22 263 long new;
> ^^^^^^^^
>
> 176ec2b092cc22 Alexey Gladkov 2021-04-22 264 for (iter = ucounts; iter; iter = iter->ns->ucounts) {
> 176ec2b092cc22 Alexey Gladkov 2021-04-22 265 long dec = atomic_long_add_return(-v, &iter->ucount[type]);
> 176ec2b092cc22 Alexey Gladkov 2021-04-22 266 WARN_ON_ONCE(dec < 0);
> 176ec2b092cc22 Alexey Gladkov 2021-04-22 267 if (iter == ucounts)
> 176ec2b092cc22 Alexey Gladkov 2021-04-22 268 new = dec;
> 176ec2b092cc22 Alexey Gladkov 2021-04-22 269 }
> 176ec2b092cc22 Alexey Gladkov 2021-04-22 @270 return (new == 0);
> ^^^^^^^^
> I don't know if this is a bug or not, but I can definitely tell why the
> static checker complains about it.
>
> 176ec2b092cc22 Alexey Gladkov 2021-04-22 271 }
In the only two cases that care about the return value of
dec_rlimit_ucounts the code first tests to see that ucounts is not
NULL. In those cases it is guaranteed at least one iteration of the
loop will execute guaranteeing the variable new will be initialized.
Initialize new to -1 so that the return value is well defined even
when the loop does not execute and the static checker is silenced.
Link: https://lkml.kernel.org/r/m1tunny77w.fsf@fess.ebiederm.org
Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
-rw-r--r-- | kernel/ucount.c | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/kernel/ucount.c b/kernel/ucount.c index d316bac3e520..df84a2a63926 100644 --- a/kernel/ucount.c +++ b/kernel/ucount.c @@ -263,7 +263,7 @@ long inc_rlimit_ucounts(struct ucounts *ucounts, enum ucount_type type, long v) bool dec_rlimit_ucounts(struct ucounts *ucounts, enum ucount_type type, long v) { struct ucounts *iter; - long new; + long new = -1; /* Silence compiler warning */ for (iter = ucounts; iter; iter = iter->ns->ucounts) { long dec = atomic_long_add_return(-v, &iter->ucount[type]); WARN_ON_ONCE(dec < 0); |