Re: [PATCH v1] btrfs: discard: fix warning for discard_state
From: Qu Wenruo
Date: Mon Sep 14 2026 - 06:24:25 EST
在 2026/9/14 18:52, Jiahao Li 写道:
From: Jiahao Li <lijiahao1@xxxxxxxxxx>
Building with -Werror=mabe-uninitialized fails:
fs/btrfs/discard.c:553:9: error: 'discard_state' may be used
uninitialized [-Werror=maybe-uninitialized]
The compiler cannot prove that discard_state is assigned on every path
before it is read.
Compiler and its version, a lot of times such false alerts are just compiler version dependent.
In my case, gcc 16.2.1 failed to reproduced this false alert.
Initialize it to BTRFS_DISCARD_EXTENTS, which is the
state the variable holds when the loop is first entered, this does not
change runtime behavior, so the warning is gone and the value is always
well defined.
It's a false alert.
Firstly if peek_discard_list() didn't find a bg, btrfs_discard_workfn() returned immediately.
Secondly if a bg is found in peek_discard_list(), *discard_state is fetched from bg's discard_state.
So all paths should be fine.
Signed-off-by: Jiahao Li <lijiahao1@xxxxxxxxxx>
---
fs/btrfs/discard.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/fs/btrfs/discard.c b/fs/btrfs/discard.c
index 1c304bf473e5..413a804ff46b 100644
--- a/fs/btrfs/discard.c
+++ b/fs/btrfs/discard.c
@@ -519,7 +519,7 @@ static void btrfs_discard_workfn(struct work_struct *work)
{
struct btrfs_discard_ctl *discard_ctl;
struct btrfs_block_group *block_group;
- enum btrfs_discard_state discard_state;
+ enum btrfs_discard_state discard_state = BTRFS_DISCARD_EXTENTS;
int discard_index = 0;
u64 trimmed = 0;
u64 minlen = 0;