On 16.11.18 г. 10:22 ч., Qu Wenruo wrote:
> GCC 8.2.1 will report the following warning with "make W=1":
> 
>   ctree.c: In function 'btrfs_next_sibling_tree_block':
>   ctree.c:2990:21: warning: 'slot' may be used uninitialized in this function 
> [-Wmaybe-uninitialized]
>     path->slots[level] = slot;
>     ~~~~~~~~~~~~~~~~~~~^~~~~~
> 
> The culprit is the following code:
> 
>       int slot;               << Not initialized
>       int level = path->lowest_level + 1;
>       BUG_ON(path->lowest_level + 1 >= BTRFS_MAX_LEVEL);
>       while(level < BTRFS_MAX_LEVEL) {
>               slot = path->slots[level] + 1;
>               ^^^^^^ but we initialize @slot here.
>               ...
>       }
>       path->slots[level] = slot;
> 
> Again, it's the stupid compiler needs some hint for the fact that
> we will always enter the while loop for at least once, thus @slot should
> always be initialized.
> 
> Fix it by assign level after the BUG_ON(), so the stupid compiler knows
> we will always go into the while loop.
> 
> Signed-off-by: Qu Wenruo <w...@suse.com>

Much better, thank you:

Reviewed-by: Nikolay Borisov <nbori...@suse.com>

> ---
> changelog:
> v1.1:
>    Better commit message, with the original warning report and
>    how compiler thinks this could be a problem.
> ---
>  ctree.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/ctree.c b/ctree.c
> index 46e2ccedc0bf..9c9cb0dfdbf2 100644
> --- a/ctree.c
> +++ b/ctree.c
> @@ -2961,11 +2961,12 @@ int btrfs_next_sibling_tree_block(struct 
> btrfs_fs_info *fs_info,
>                                 struct btrfs_path *path)
>  {
>       int slot;
> -     int level = path->lowest_level + 1;
> +     int level;
>       struct extent_buffer *c;
>       struct extent_buffer *next = NULL;
>  
>       BUG_ON(path->lowest_level + 1 >= BTRFS_MAX_LEVEL);
> +     level = path->lowest_level + 1;
>       while(level < BTRFS_MAX_LEVEL) {
>               if (!path->nodes[level])
>                       return 1;
> 

Reply via email to