diff mbox series

btrfs-progs: corrupt-block: fix the mismatch in --root and -r options

Message ID 45593abf29f76663fa9b18c5ddc52556df5464f6.1670387695.git.wqu@suse.com (mailing list archive)
State New, archived
Headers show
Series btrfs-progs: corrupt-block: fix the mismatch in --root and -r options | expand

Commit Message

Qu Wenruo Dec. 7, 2022, 4:35 a.m. UTC
[BUG]

The following command will crash:

 $ btrfs-corrupt-block --value 4308598784 --root 5 --inode 256 --file-extent 0 \
	-f disk_bytenr ~/test.img

[CAUSE]
The backtrace is at the following code:

			case 'r':
				root_objectid = arg_strtou64(optarg);
				break;

And @optarg is NULL.

The root cause is, for short option "-r" it indeed requires an argument.
But unfortunately for the longer version, it goes:

			{ "root", no_argument, NULL, 'r'},

Thus it gave @optarg as NULL if we go the longer option and crash.

[FIX]
Just fix the argument requirement for "--root" option.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 btrfs-corrupt-block.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

David Sterba Dec. 20, 2022, 7:49 p.m. UTC | #1
On Wed, Dec 07, 2022 at 12:35:01PM +0800, Qu Wenruo wrote:
> [BUG]
> 
> The following command will crash:
> 
>  $ btrfs-corrupt-block --value 4308598784 --root 5 --inode 256 --file-extent 0 \
> 	-f disk_bytenr ~/test.img
> 
> [CAUSE]
> The backtrace is at the following code:
> 
> 			case 'r':
> 				root_objectid = arg_strtou64(optarg);
> 				break;
> 
> And @optarg is NULL.
> 
> The root cause is, for short option "-r" it indeed requires an argument.
> But unfortunately for the longer version, it goes:
> 
> 			{ "root", no_argument, NULL, 'r'},
> 
> Thus it gave @optarg as NULL if we go the longer option and crash.
> 
> [FIX]
> Just fix the argument requirement for "--root" option.
> 
> Signed-off-by: Qu Wenruo <wqu@suse.com>

Added to devel, thanks.
diff mbox series

Patch

diff --git a/btrfs-corrupt-block.c b/btrfs-corrupt-block.c
index 6cf5dc520d8b..b4e2b06efa43 100644
--- a/btrfs-corrupt-block.c
+++ b/btrfs-corrupt-block.c
@@ -1346,7 +1346,7 @@  int main(int argc, char **argv)
 			{ "item", no_argument, NULL, 'I'},
 			{ "dir-item", no_argument, NULL, 'D'},
 			{ "delete", no_argument, NULL, 'd'},
-			{ "root", no_argument, NULL, 'r'},
+			{ "root", required_argument, NULL, 'r'},
 			{ "csum", required_argument, NULL, 'C'},
 			{ "block-group", required_argument, NULL, GETOPT_VAL_BLOCK_GROUP},
 			{ "value", required_argument, NULL, GETOPT_VAL_VALUE},