radix tree test suite: Fix uninitialized variable compilation warning

Message ID tencent_DF74099967595DCEA93CBDC28D062026180A@qq.com
State New
Headers
Series radix tree test suite: Fix uninitialized variable compilation warning |

Commit Message

Rong Tao Nov. 9, 2022, 2:34 p.m. UTC
  From: Rong Tao <rongtao@cestc.cn>

We need to set an initial value for offset to eliminate compilation
warning.

How to reproduce warning:

$ make -C tools/testing/radix-tree
radix-tree.c: In function ‘radix_tree_tag_clear’:
radix-tree.c:1046:17: warning: ‘offset’ may be used uninitialized in this function [-Wmaybe-uninitialized]
 1046 |                 node_tag_clear(root, parent, tag, offset);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Rong Tao <rongtao@cestc.cn>
---
 lib/radix-tree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Andrew Morton Nov. 10, 2022, 12:23 a.m. UTC | #1
On Wed,  9 Nov 2022 22:34:25 +0800 Rong Tao <rtoax@foxmail.com> wrote:

> [PATCH] radix tree test suite: Fix uninitialized variable compilation warning

This is not the test suite.

> We need to set an initial value for offset to eliminate compilation
> warning.
> 
> How to reproduce warning:
> 
> $ make -C tools/testing/radix-tree
> radix-tree.c: In function ‘radix_tree_tag_clear’:
> radix-tree.c:1046:17: warning: ‘offset’ may be used uninitialized in this function [-Wmaybe-uninitialized]
>  1046 |                 node_tag_clear(root, parent, tag, offset);
>       |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> ...
>
> --- a/lib/radix-tree.c
> +++ b/lib/radix-tree.c
> @@ -1029,7 +1029,7 @@ void *radix_tree_tag_clear(struct radix_tree_root *root,
>  {
>  	struct radix_tree_node *node, *parent;
>  	unsigned long maxindex;
> -	int offset;
> +	int offset = 0;
>  
>  	radix_tree_load_root(root, &node, &maxindex);
>  	if (index > maxindex)

Are we sure this isn't actually a bug?  What happens if the tree is empty?
  
Matthew Wilcox Nov. 10, 2022, 4:06 p.m. UTC | #2
On Wed, Nov 09, 2022 at 04:23:48PM -0800, Andrew Morton wrote:
> On Wed,  9 Nov 2022 22:34:25 +0800 Rong Tao <rtoax@foxmail.com> wrote:
> 
> > [PATCH] radix tree test suite: Fix uninitialized variable compilation warning
> 
> This is not the test suite.
> 
> > We need to set an initial value for offset to eliminate compilation
> > warning.
> > 
> > How to reproduce warning:
> > 
> > $ make -C tools/testing/radix-tree
> > radix-tree.c: In function ‘radix_tree_tag_clear’:
> > radix-tree.c:1046:17: warning: ‘offset’ may be used uninitialized in this function [-Wmaybe-uninitialized]
> >  1046 |                 node_tag_clear(root, parent, tag, offset);
> >       |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > 
> > ...
> >
> > --- a/lib/radix-tree.c
> > +++ b/lib/radix-tree.c
> > @@ -1029,7 +1029,7 @@ void *radix_tree_tag_clear(struct radix_tree_root *root,
> >  {
> >  	struct radix_tree_node *node, *parent;
> >  	unsigned long maxindex;
> > -	int offset;
> > +	int offset = 0;
> >  
> >  	radix_tree_load_root(root, &node, &maxindex);
> >  	if (index > maxindex)
> 
> Are we sure this isn't actually a bug?  What happens if the tree is empty?

If the tree is empty, then node is NULL and we never use offset.
The compiler is too stupid to know this.  This warning is only observed
when building the test suite and not when building the kernel itself.
I'm not sure the patch is worth it, tbh.
  

Patch

diff --git a/lib/radix-tree.c b/lib/radix-tree.c
index 3c78e1e8b2ad..049ba132f7ef 100644
--- a/lib/radix-tree.c
+++ b/lib/radix-tree.c
@@ -1029,7 +1029,7 @@  void *radix_tree_tag_clear(struct radix_tree_root *root,
 {
 	struct radix_tree_node *node, *parent;
 	unsigned long maxindex;
-	int offset;
+	int offset = 0;
 
 	radix_tree_load_root(root, &node, &maxindex);
 	if (index > maxindex)