diff mbox series

btrfs-progs: use alloc_dummy_extent_buffer() for temporaray super block

Message ID 80b02fbfe6796c85322244f2e3110295787df3a6.1679098721.git.wqu@suse.com (mailing list archive)
State New, archived
Headers show
Series btrfs-progs: use alloc_dummy_extent_buffer() for temporaray super block | expand

Commit Message

Qu Wenruo March 18, 2023, 12:18 a.m. UTC
[FALSE ALERT]
There is a false alert when compiling btrfs-progs using gcc 12.2.1:

 $ make D=1
 kernel-shared/print-tree.c: In function 'print_sys_chunk_array':
 kernel-shared/print-tree.c:1797:9: warning: 'buf' may be used uninitialized [-Wmaybe-uninitialized]
  1797 |         write_extent_buffer(buf, sb, 0, sizeof(*sb));
       |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 In file included from ./kernel-shared/ctree.h:27,
                  from kernel-shared/print-tree.c:24:
 ./kernel-shared/extent_io.h:148:6: note: by argument 1 of type 'const struct extent_buffer *' to 'write_extent_buffer' declared here
   148 | void write_extent_buffer(const struct extent_buffer *eb, const void *src,
       |      ^~~~~~~~~~~~~~~~~~~

[CAUSE]
This is a false alert, the uninitialized part of buf will not be
utilized at all during write_extent_buffer().

[FIX]
Instead of allocating such adhoc buf, go a more formal way by calling
alloc_dummy_extent_buffer(), which would properly setting all the
members.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 kernel-shared/print-tree.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Comments

Anand Jain March 19, 2023, 2:58 a.m. UTC | #1
On 3/18/23 08:18, Qu Wenruo wrote:
> [FALSE ALERT]
> There is a false alert when compiling btrfs-progs using gcc 12.2.1:
> 
>   $ make D=1
>   kernel-shared/print-tree.c: In function 'print_sys_chunk_array':
>   kernel-shared/print-tree.c:1797:9: warning: 'buf' may be used uninitialized [-Wmaybe-uninitialized]
>    1797 |         write_extent_buffer(buf, sb, 0, sizeof(*sb));
>         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>   In file included from ./kernel-shared/ctree.h:27,
>                    from kernel-shared/print-tree.c:24:
>   ./kernel-shared/extent_io.h:148:6: note: by argument 1 of type 'const struct extent_buffer *' to 'write_extent_buffer' declared here
>     148 | void write_extent_buffer(const struct extent_buffer *eb, const void *src,
>         |      ^~~~~~~~~~~~~~~~~~~
> 
> [CAUSE]
> This is a false alert, the uninitialized part of buf will not be
> utilized at all during write_extent_buffer().
> 
> [FIX]
> Instead of allocating such adhoc buf, go a more formal way by calling
> alloc_dummy_extent_buffer(), which would properly setting all the
> members.
> 
> Signed-off-by: Qu Wenruo <wqu@suse.com>

looks good.

Reviewed-by: Anand Jain <anand.jain@oracle.com>
David Sterba March 21, 2023, 2:11 a.m. UTC | #2
On Sat, Mar 18, 2023 at 08:18:42AM +0800, Qu Wenruo wrote:
> [FALSE ALERT]
> There is a false alert when compiling btrfs-progs using gcc 12.2.1:
> 
>  $ make D=1
>  kernel-shared/print-tree.c: In function 'print_sys_chunk_array':
>  kernel-shared/print-tree.c:1797:9: warning: 'buf' may be used uninitialized [-Wmaybe-uninitialized]
>   1797 |         write_extent_buffer(buf, sb, 0, sizeof(*sb));
>        |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>  In file included from ./kernel-shared/ctree.h:27,
>                   from kernel-shared/print-tree.c:24:
>  ./kernel-shared/extent_io.h:148:6: note: by argument 1 of type 'const struct extent_buffer *' to 'write_extent_buffer' declared here
>    148 | void write_extent_buffer(const struct extent_buffer *eb, const void *src,
>        |      ^~~~~~~~~~~~~~~~~~~
> 
> [CAUSE]
> This is a false alert, the uninitialized part of buf will not be
> utilized at all during write_extent_buffer().
> 
> [FIX]
> Instead of allocating such adhoc buf, go a more formal way by calling
> alloc_dummy_extent_buffer(), which would properly setting all the
> members.
> 
> Signed-off-by: Qu Wenruo <wqu@suse.com>

Added to devel, thanks.
diff mbox series

Patch

diff --git a/kernel-shared/print-tree.c b/kernel-shared/print-tree.c
index a57e9a4d7593..3fb6a37c118b 100644
--- a/kernel-shared/print-tree.c
+++ b/kernel-shared/print-tree.c
@@ -1789,7 +1789,7 @@  static void print_sys_chunk_array(struct btrfs_super_block *sb)
 	struct btrfs_key key;
 	int item;
 
-	buf = malloc(sizeof(*buf) + sizeof(*sb));
+	buf = alloc_dummy_extent_buffer(NULL, 0, BTRFS_SUPER_INFO_SIZE);
 	if (!buf) {
 		error_msg(ERROR_MSG_MEMORY, NULL);
 		return;
@@ -1860,13 +1860,13 @@  static void print_sys_chunk_array(struct btrfs_super_block *sb)
 	}
 
 out:
-	free(buf);
+	free_extent_buffer(buf);
 	return;
 
 out_short_read:
 	error("sys_array too short to read %u bytes at offset %u",
 			len, cur_offset);
-	free(buf);
+	free_extent_buffer(buf);
 }
 
 static int empty_backup(struct btrfs_root_backup *backup)