[bpf-next,2/2] bpftool: Check argc first before "file" in do_batch()

Message ID 1668396484-4596-3-git-send-email-yangtiezhu@loongson.cn
State New
Headers
Series Some small changes about bpftool |

Commit Message

Tiezhu Yang Nov. 14, 2022, 3:28 a.m. UTC
  If the parameters for batch are more than 2, check argc first can
return immediately, no need to use strcmp() to check "file" with
a little overhead and then check argc, it is better to check "file"
only when the parameters for batch are 2.

Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 tools/bpf/bpftool/main.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
  

Comments

Stanislav Fomichev Nov. 14, 2022, 5:30 p.m. UTC | #1
On 11/14, Tiezhu Yang wrote:
> If the parameters for batch are more than 2, check argc first can
> return immediately, no need to use strcmp() to check "file" with
> a little overhead and then check argc, it is better to check "file"
> only when the parameters for batch are 2.

Seems fine if you respin with is_prefix instead of strcmp.
Has the potential of breaking some buggy users which pass
more than one file, but I don't think it's a good justification
no to do the fix? Quentin?


> Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
> ---
>   tools/bpf/bpftool/main.c | 6 +++---
>   1 file changed, 3 insertions(+), 3 deletions(-)

> diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c
> index 4ef87c2..27d6dbf 100644
> --- a/tools/bpf/bpftool/main.c
> +++ b/tools/bpf/bpftool/main.c
> @@ -337,12 +337,12 @@ static int do_batch(int argc, char **argv)
>   	if (argc < 2) {
>   		p_err("too few parameters for batch");
>   		return -1;
> -	} else if (strcmp(*argv, "file")) {
> -		p_err("expected 'file', got: %s", *argv);
> -		return -1;
>   	} else if (argc > 2) {
>   		p_err("too many parameters for batch");
>   		return -1;
> +	} else if (strcmp(*argv, "file")) {
> +		p_err("expected 'file', got: %s", *argv);
> +		return -1;
>   	}
>   	NEXT_ARG();

> --
> 2.1.0
  
Quentin Monnet Nov. 14, 2022, 8:53 p.m. UTC | #2
On Mon, 14 Nov 2022 at 17:30, <sdf@google.com> wrote:
>
> On 11/14, Tiezhu Yang wrote:
> > If the parameters for batch are more than 2, check argc first can
> > return immediately, no need to use strcmp() to check "file" with
> > a little overhead and then check argc, it is better to check "file"
> > only when the parameters for batch are 2.

Thanks for the patch

> Seems fine if you respin with is_prefix instead of strcmp.
> Has the potential of breaking some buggy users which pass
> more than one file, but I don't think it's a good justification
> no to do the fix? Quentin?

I don't think it could break, the argc check is already enforced
(currently after the check on "file") and no more than one batch file
can be passed. I'm not sure it's super useful to swap the checks
either, because you can similarly argue that there's no need to check
argc is <= 2 if the first arg for "bpftool batch" is different from
"file" (or a prefix). The argc check is faster than the is_prefix()
comparison, but that's a minor optimization for one specific error
case. I don't really see the point, but I'm not opposed to the patch
either if you repost with is_prefix() as suggested by Stanislav.
  

Patch

diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c
index 4ef87c2..27d6dbf 100644
--- a/tools/bpf/bpftool/main.c
+++ b/tools/bpf/bpftool/main.c
@@ -337,12 +337,12 @@  static int do_batch(int argc, char **argv)
 	if (argc < 2) {
 		p_err("too few parameters for batch");
 		return -1;
-	} else if (strcmp(*argv, "file")) {
-		p_err("expected 'file', got: %s", *argv);
-		return -1;
 	} else if (argc > 2) {
 		p_err("too many parameters for batch");
 		return -1;
+	} else if (strcmp(*argv, "file")) {
+		p_err("expected 'file', got: %s", *argv);
+		return -1;
 	}
 	NEXT_ARG();