Patchwork [U-Boot,1/3] fs: fix do_fsload() handling of optional arguments

login
register
mail settings
Submitter Stephen Warren
Date Oct. 30, 2012, 10:04 p.m.
Message ID <1351634659-16107-1-git-send-email-swarren@wwwdotorg.org>
Download mbox | patch
Permalink /patch/195635/
State Accepted
Delegated to: Tom Rini
Headers show

Comments

Stephen Warren - Oct. 30, 2012, 10:04 p.m.
From: Stephen Warren <swarren@nvidia.com>

Most arguments to the shell command do_fsload() implements are optional.
Fix the minimum argc check to respect that. Cater for the situation
where argv[2] is not provided.

Enhance both do_fsload() and do_ls() to check the maximum number of
arguments too. While this check would typically be implemented via
U_BOOT_CMD()'s max_args parameter, if these functions are called
directly, then that check won't exist.

Finally, alter do_ls() to check (argc >= 4) rather than (argc == 4) so
that if the function is enhanced to allow extra arguments in the future,
this test won't need to be changed at that time.

Signed-off-by: Stephen Warren <swarren@nvidia.com>
---
 fs/fs.c |   10 +++++++---
 1 files changed, 7 insertions(+), 3 deletions(-)
Benoît Thébaudeau - Oct. 30, 2012, 10:28 p.m.
On Tuesday, October 30, 2012 11:04:17 PM, Stephen Warren wrote:
> From: Stephen Warren <swarren@nvidia.com>
> 
> Most arguments to the shell command do_fsload() implements are
> optional.
> Fix the minimum argc check to respect that. Cater for the situation
> where argv[2] is not provided.
> 
> Enhance both do_fsload() and do_ls() to check the maximum number of
> arguments too. While this check would typically be implemented via
> U_BOOT_CMD()'s max_args parameter, if these functions are called
> directly, then that check won't exist.
> 
> Finally, alter do_ls() to check (argc >= 4) rather than (argc == 4)
> so
> that if the function is enhanced to allow extra arguments in the
> future,
> this test won't need to be changed at that time.
> 
> Signed-off-by: Stephen Warren <swarren@nvidia.com>
[--snip--]

For the series:
Reviewed-by: Benoît Thébaudeau <benoit.thebaudeau@advansee.com>

Best regards,
Benoît
Tom Rini - Nov. 4, 2012, 6:30 p.m.
On Tue, Oct 30, 2012 at 11:28:55PM +0100, Beno??t Th??baudeau wrote:
> On Tuesday, October 30, 2012 11:04:17 PM, Stephen Warren wrote:
> > From: Stephen Warren <swarren@nvidia.com>
> > 
> > Most arguments to the shell command do_fsload() implements are
> > optional.
> > Fix the minimum argc check to respect that. Cater for the situation
> > where argv[2] is not provided.
> > 
> > Enhance both do_fsload() and do_ls() to check the maximum number of
> > arguments too. While this check would typically be implemented via
> > U_BOOT_CMD()'s max_args parameter, if these functions are called
> > directly, then that check won't exist.
> > 
> > Finally, alter do_ls() to check (argc >= 4) rather than (argc == 4)
> > so
> > that if the function is enhanced to allow extra arguments in the
> > future,
> > this test won't need to be changed at that time.
> > 
> > Signed-off-by: Stephen Warren <swarren@nvidia.com>
> [--snip--]
> 
> For the series:
> Reviewed-by: Beno??t Th??baudeau <benoit.thebaudeau@advansee.com>

And for the series, applied to u-boot/master, thanks!

Patch

diff --git a/fs/fs.c b/fs/fs.c
index e148a07..f570312 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -258,10 +258,12 @@  int do_fsload(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
 	int len_read;
 	char buf[12];
 
-	if (argc < 5)
+	if (argc < 2)
+		return CMD_RET_USAGE;
+	if (argc > 7)
 		return CMD_RET_USAGE;
 
-	if (fs_set_blk_dev(argv[1], argv[2], fstype))
+	if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
 		return 1;
 
 	if (argc >= 4) {
@@ -308,11 +310,13 @@  int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
 {
 	if (argc < 2)
 		return CMD_RET_USAGE;
+	if (argc > 4)
+		return CMD_RET_USAGE;
 
 	if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
 		return 1;
 
-	if (fs_ls(argc == 4 ? argv[3] : "/"))
+	if (fs_ls(argc >= 4 ? argv[3] : "/"))
 		return 1;
 
 	return 0;