Re: [PATCH] selftests/mm: Fix compiler -Wmaybe-uninitialized warning

From: Anshuman Khandual
Date: Wed Apr 09 2025 - 06:10:50 EST




On 4/9/25 15:27, David Hildenbrand wrote:
> On 09.04.25 11:50, Anshuman Khandual wrote:
>> Following build warning comes up for cow test as 'transferred' variable has
>> not been initialized. Fix the warning via zero init for the variable.
>>
>>    CC       cow
>> cow.c: In function ‘do_test_vmsplice_in_parent’:
>> cow.c:365:61: warning: ‘transferred’ may be used uninitialized [-Wmaybe-uninitialized]
>>    365 |                 cur = read(fds[0], new + total, transferred - total);
>>        |                                                 ~~~~~~~~~~~~^~~~~~~
>> cow.c:296:29: note: ‘transferred’ was declared here
>>    296 |         ssize_t cur, total, transferred;
>>        |                             ^~~~~~~~~~~
>>    CC       compaction_test
>>    CC       gup_longterm
>>
>> Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
>> Cc: Shuah Khan <shuah@xxxxxxxxxx>
>> Cc: linux-mm@xxxxxxxxx
>> Cc: linux-kselftest@xxxxxxxxxxxxxxx
>> Cc: linux-kernel@xxxxxxxxxxxxxxx
>> Signed-off-by: Anshuman Khandual <anshuman.khandual@xxxxxxx>
>> ---
>>   tools/testing/selftests/mm/cow.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/tools/testing/selftests/mm/cow.c b/tools/testing/selftests/mm/cow.c
>> index f0cb14ea8608..b6cfe0a4b7df 100644
>> --- a/tools/testing/selftests/mm/cow.c
>> +++ b/tools/testing/selftests/mm/cow.c
>> @@ -293,7 +293,7 @@ static void do_test_vmsplice_in_parent(char *mem, size_t size,
>>           .iov_base = mem,
>>           .iov_len = size,
>>       };
>> -    ssize_t cur, total, transferred;
>> +    ssize_t cur, total, transferred = 0;
>>       struct comm_pipes comm_pipes;
>>       char *old, *new;
>>       int ret, fds[2];
>
>
> if (before_fork) {
>     transferred = vmsplice(fds[1], &iov, 1, 0);
> ...
>
> if (!before_fork) {
>     transferred = vmsplice(fds[1], &iov, 1, 0);
> ...
>
> for (total = 0; total < transferred; total += cur) {
> ...
>
>
> And I don't see any jump label that could jump to code that would ve using transferred.
>
> What am I missing?

Probably because both those conditional statements are not mutually
exclusive above with an if-else construct. Hence compiler flags it
rather as a false positive ? Initializing with 0 just works around
that false positive.