# \[PATCH 1/2\] tools, bpftool: Avoid array index warnings.

- Subsystems: BPF \[GENERAL\] (Safe Dynamic Programs and Tools), BPF \[TOOLING\] (bpftool), THE REST
- Maintainers: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi, Quentin Monnet, Linus Torvalds
- Status: LANDED (1e6f5dcc1b9e · 2020-10-29 14:22)
- Landed in mainline as 1e6f5dcc1b9e on 2020-10-29.
- List: netdev
- 6 messages, 3 authors
- Page: https://ratatoskr.run/netdev/2020/10/6154621/t

## \[PATCH 1/2\] tools, bpftool: Avoid array index warnings.

- From: Ian Rogers \<irogers@google.com\>
- Date: 2020-10-28 01:47:31 
- Message: https://ratatoskr.run/netdev/2020/10/6154621.md

```text
The bpf_caps array is shorter without CAP_BPF, avoid out of bounds reads
if this isn't defined. Working around this avoids -Wno-array-bounds with
clang.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/bpf/bpftool/feature.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/tools/bpf/bpftool/feature.c b/tools/bpf/bpftool/feature.c
index a43a6f10b564..359960a8f1de 100644
--- a/tools/bpf/bpftool/feature.c
+++ b/tools/bpf/bpftool/feature.c
@@ -843,9 +843,14 @@ static int handle_perms(void)
 		else
 			p_err("missing %s%s%s%s%s%s%s%srequired for full feature probing; run as root or use 'unprivileged'",
 			      capability_msg(bpf_caps, 0),
+#ifdef CAP_BPF
 			      capability_msg(bpf_caps, 1),
 			      capability_msg(bpf_caps, 2),
-			      capability_msg(bpf_caps, 3));
+			      capability_msg(bpf_caps, 3)
+#else
+				"", "", "", "", "", ""
+#endif /* CAP_BPF */
+				);
 		goto exit_free;
 	}
 
-- 
2.29.0.rc2.309.g374f81d7ae-goog

```

## \[PATCH 2/2\] tools, bpftool: Remove two unused variables.

- From: Ian Rogers \<irogers@google.com\>
- Date: 2020-10-28 01:38:59 
- Message: https://ratatoskr.run/netdev/2020/10/6154599.md

```text
Avoid an unused variable warning.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/bpf/bpftool/skeleton/profiler.bpf.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/bpf/bpftool/skeleton/profiler.bpf.c b/tools/bpf/bpftool/skeleton/profiler.bpf.c
index 4e3512f700c0..ce5b65e07ab1 100644
--- a/tools/bpf/bpftool/skeleton/profiler.bpf.c
+++ b/tools/bpf/bpftool/skeleton/profiler.bpf.c
@@ -70,7 +70,7 @@ int BPF_PROG(fentry_XXX)
 static inline void
 fexit_update_maps(u32 id, struct bpf_perf_event_value *after)
 {
-	struct bpf_perf_event_value *before, diff, *accum;
+	struct bpf_perf_event_value *before, diff;
 
 	before = bpf_map_lookup_elem(&fentry_readings, &id);
 	/* only account samples with a valid fentry_reading */
@@ -95,7 +95,7 @@ int BPF_PROG(fexit_XXX)
 {
 	struct bpf_perf_event_value readings[MAX_NUM_MATRICS];
 	u32 cpu = bpf_get_smp_processor_id();
-	u32 i, one = 1, zero = 0;
+	u32 i, zero = 0;
 	int err;
 	u64 *count;
 
-- 
2.29.0.rc2.309.g374f81d7ae-goog

```

## Re: \[PATCH 1/2\] tools, bpftool: Avoid array index warnings.

- From: Andrii Nakryiko \<hidden\>
- Date: 2020-10-28 22:12:19 
- Message: https://ratatoskr.run/netdev/2020/10/6154938.md

```text
On Tue, Oct 27, 2020 at 4:37 PM Ian Rogers [ref] wrote:
>
> The bpf_caps array is shorter without CAP_BPF, avoid out of bounds reads
> if this isn't defined. Working around this avoids -Wno-array-bounds with
> clang.
>
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---

Acked-by: Andrii Nakryiko <andrii@kernel.org>

>  tools/bpf/bpftool/feature.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/tools/bpf/bpftool/feature.c b/tools/bpf/bpftool/feature.c
> index a43a6f10b564..359960a8f1de 100644
> --- a/tools/bpf/bpftool/feature.c
> +++ b/tools/bpf/bpftool/feature.c
> @@ -843,9 +843,14 @@ static int handle_perms(void)
>                 else
>                         p_err("missing %s%s%s%s%s%s%s%srequired for full feature probing; run as root or use 'unprivileged'",
>                               capability_msg(bpf_caps, 0),
> +#ifdef CAP_BPF
>                               capability_msg(bpf_caps, 1),
>                               capability_msg(bpf_caps, 2),
> -                             capability_msg(bpf_caps, 3));
> +                             capability_msg(bpf_caps, 3)
> +#else
> +                               "", "", "", "", "", ""
> +#endif /* CAP_BPF */
> +                               );
>                 goto exit_free;
>         }
>
> --
> 2.29.0.rc2.309.g374f81d7ae-goog
>
```

## Re: \[PATCH 2/2\] tools, bpftool: Remove two unused variables.

- From: Andrii Nakryiko \<hidden\>
- Date: 2020-10-28 22:48:11 
- Message: https://ratatoskr.run/netdev/2020/10/6155231.md

```text
On Tue, Oct 27, 2020 at 4:37 PM Ian Rogers [ref] wrote:
>
> Avoid an unused variable warning.
>
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---

Acked-by: Andrii Nakryiko <andrii@kernel.org>

>  tools/bpf/bpftool/skeleton/profiler.bpf.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/tools/bpf/bpftool/skeleton/profiler.bpf.c b/tools/bpf/bpftool/skeleton/profiler.bpf.c
> index 4e3512f700c0..ce5b65e07ab1 100644
> --- a/tools/bpf/bpftool/skeleton/profiler.bpf.c
> +++ b/tools/bpf/bpftool/skeleton/profiler.bpf.c
> @@ -70,7 +70,7 @@ int BPF_PROG(fentry_XXX)
>  static inline void
>  fexit_update_maps(u32 id, struct bpf_perf_event_value *after)
>  {
> -       struct bpf_perf_event_value *before, diff, *accum;
> +       struct bpf_perf_event_value *before, diff;
>
>         before = bpf_map_lookup_elem(&fentry_readings, &id);
>         /* only account samples with a valid fentry_reading */
> @@ -95,7 +95,7 @@ int BPF_PROG(fexit_XXX)
>  {
>         struct bpf_perf_event_value readings[MAX_NUM_MATRICS];
>         u32 cpu = bpf_get_smp_processor_id();
> -       u32 i, one = 1, zero = 0;
> +       u32 i, zero = 0;
>         int err;
>         u64 *count;
>
> --
> 2.29.0.rc2.309.g374f81d7ae-goog
>
```

## Re: \[PATCH 2/2\] tools, bpftool: Remove two unused variables.

- From: Tobias Klauser \<tklauser@distanz.ch\>
- Date: 2020-10-28 23:14:33 
- Message: https://ratatoskr.run/netdev/2020/10/6155305.md

```text
On 2020-10-28 at 00:36:46 +0100, Ian Rogers [ref] wrote:
> Avoid an unused variable warning.
> 
> Signed-off-by: Ian Rogers <irogers@google.com>

Reviewed-by: Tobias Klauser <tklauser@distanz.ch>
```

## Re: \[PATCH 1/2\] tools, bpftool: Avoid array index warnings.

- From: Tobias Klauser \<tklauser@distanz.ch\>
- Date: 2020-10-28 23:14:35 
- Message: https://ratatoskr.run/netdev/2020/10/6155306.md

```text
On 2020-10-28 at 00:36:45 +0100, Ian Rogers [ref] wrote:
> The bpf_caps array is shorter without CAP_BPF, avoid out of bounds reads
> if this isn't defined. Working around this avoids -Wno-array-bounds with
> clang.
> 
> Signed-off-by: Ian Rogers <irogers@google.com>

Reviewed-by: Tobias Klauser <tklauser@distanz.ch>
```
