[bpf-next,1/2] bpftool: Use strcmp() instead of is_prefix() to check parameters

Message ID 1668396484-4596-2-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
  In the current code, the parameters check of bpftool seems not correct,
for example, "bpftool batch file FILE" is the expected command format,
but "bpftool b f FILE" is recognized as valid, so use strcmp() instead
of is_prefix() to check parameters.

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

Comments

Stanislav Fomichev Nov. 14, 2022, 5:25 p.m. UTC | #1
On 11/14, Tiezhu Yang wrote:
> In the current code, the parameters check of bpftool seems not correct,
> for example, "bpftool batch file FILE" is the expected command format,
> but "bpftool b f FILE" is recognized as valid, so use strcmp() instead
> of is_prefix() to check parameters.

That's by design and is similar to what iproute2 commands are doing.

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

> diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c
> index 741e50e..4ef87c2 100644
> --- a/tools/bpf/bpftool/main.c
> +++ b/tools/bpf/bpftool/main.c
> @@ -200,7 +200,7 @@ int cmd_select(const struct cmd *cmds, int argc, char  
> **argv,
>   		return cmds[0].func(argc, argv);

>   	for (i = 0; cmds[i].cmd; i++) {
> -		if (is_prefix(*argv, cmds[i].cmd)) {
> +		if (!strcmp(*argv, cmds[i].cmd)) {
>   			if (!cmds[i].func) {
>   				p_err("command '%s' is not supported in bootstrap mode",
>   				      cmds[i].cmd);
> @@ -337,7 +337,7 @@ static int do_batch(int argc, char **argv)
>   	if (argc < 2) {
>   		p_err("too few parameters for batch");
>   		return -1;
> -	} else if (!is_prefix(*argv, "file")) {
> +	} else if (strcmp(*argv, "file")) {
>   		p_err("expected 'file', got: %s", *argv);
>   		return -1;
>   	} else if (argc > 2) {
> --
> 2.1.0
  
Quentin Monnet Nov. 14, 2022, 8:45 p.m. UTC | #2
On Mon, 14 Nov 2022 at 17:25, <sdf@google.com> wrote:
>
> On 11/14, Tiezhu Yang wrote:
> > In the current code, the parameters check of bpftool seems not correct,
> > for example, "bpftool batch file FILE" is the expected command format,
> > but "bpftool b f FILE" is recognized as valid, so use strcmp() instead
> > of is_prefix() to check parameters.
>
> That's by design and is similar to what iproute2 commands are doing.

Agreed with Stanislav, all bpftool commands support argument prefixing
and it's helpful, I see no reason to remove it for the batch command.
But thanks anyway for reporting
  

Patch

diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c
index 741e50e..4ef87c2 100644
--- a/tools/bpf/bpftool/main.c
+++ b/tools/bpf/bpftool/main.c
@@ -200,7 +200,7 @@  int cmd_select(const struct cmd *cmds, int argc, char **argv,
 		return cmds[0].func(argc, argv);
 
 	for (i = 0; cmds[i].cmd; i++) {
-		if (is_prefix(*argv, cmds[i].cmd)) {
+		if (!strcmp(*argv, cmds[i].cmd)) {
 			if (!cmds[i].func) {
 				p_err("command '%s' is not supported in bootstrap mode",
 				      cmds[i].cmd);
@@ -337,7 +337,7 @@  static int do_batch(int argc, char **argv)
 	if (argc < 2) {
 		p_err("too few parameters for batch");
 		return -1;
-	} else if (!is_prefix(*argv, "file")) {
+	} else if (strcmp(*argv, "file")) {
 		p_err("expected 'file', got: %s", *argv);
 		return -1;
 	} else if (argc > 2) {