On Fri, Apr 26, 2024 at 11:21:35AM +0800, Hongbo Li wrote:
> When compiling the bcachefs-tools, the following compilation warning
> is reported:
> libbcachefs/snapshot.c: In function ‘bch2_reconstruct_snapshots’:
> libbcachefs/snapshot.c:915:19: warning: ‘tree_id’ may be used
> uninitialized in this function [-Wmaybe-uninitialized]
> 915 | snapshot->v.tree = cpu_to_le32(tree_id);
> libbcachefs/snapshot.c:903:6: note: ‘tree_id’ was declared here
> 903 | u32 tree_id;
> | ^~~~~~~
>
> This is a false alert, because @tree_id is changed in
> bch2_snapshot_tree_create after it returns 0. And if this function
> returns other value, @tree_id wouldn't be used. Thus there should
> be nothing wrong in logical.
>
> Although the report itself is a false alert, we can still make it more
> explicit by setting the initial value of @tree_id to 0 (an invalid
> tree ID).
>
> Fixes: a292be3b68f3 ("bcachefs: Reconstruct missing snapshot nodes")
> Signed-off-by: Hongbo Li <[email protected]>
this works - applied
> ---
> v2:
> - Revise the code based on Kent's reviews.
>
> v1:
> https://lore.kernel.org/all/[email protected]/T/#u
> ---
> ---
> fs/bcachefs/snapshot.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/fs/bcachefs/snapshot.c b/fs/bcachefs/snapshot.c
> index 2368218070d4..3aa4686cc71f 100644
> --- a/fs/bcachefs/snapshot.c
> +++ b/fs/bcachefs/snapshot.c
> @@ -900,7 +900,8 @@ static int check_snapshot_exists(struct btree_trans
> *trans, u32 id)
> if (bch2_snapshot_equiv(c, id))
> return 0;
>
> - u32 tree_id;
> + /* 0 is an invalid tree ID */
> + u32 tree_id = 0;
> int ret = bch2_snapshot_tree_create(trans, id, 0, &tree_id);
> if (ret)
> return ret;
> --
> 2.34.1
>