bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next] selftests/bpf: fix a clang compilation error
@ 2022-01-27 16:37 Yonghong Song
  2022-01-27 17:50 ` Alexei Starovoitov
  0 siblings, 1 reply; 4+ messages in thread
From: Yonghong Song @ 2022-01-27 16:37 UTC (permalink / raw)
  To: bpf; +Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team

When building selftests/bpf with clang
  make -j LLVM=1
  make -C tools/testing/selftests/bpf -j LLVM=1
I hit the following compilation error:

  trace_helpers.c:152:9: error: variable 'found' is used uninitialized whenever 'while' loop exits because its condition is false [-Werror,-Wsometimes-uninitialized]
          while (fscanf(f, "%zx-%zx %s %zx %*[^\n]\n", &start, &end, buf, &base) == 4) {
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  trace_helpers.c:161:7: note: uninitialized use occurs here
          if (!found)
               ^~~~~
  trace_helpers.c:152:9: note: remove the condition if it is always true
          while (fscanf(f, "%zx-%zx %s %zx %*[^\n]\n", &start, &end, buf, &base) == 4) {
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                 1
  trace_helpers.c:145:12: note: initialize the variable 'found' to silence this warning
          bool found;
                    ^
                     = false

It is possible that for sane /proc/self/maps we may never hit the above issue
in practice. But let us initialize variable 'found' properly to silence the
compilation error.

Signed-off-by: Yonghong Song <yhs@fb.com>
---
 tools/testing/selftests/bpf/trace_helpers.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/testing/selftests/bpf/trace_helpers.c b/tools/testing/selftests/bpf/trace_helpers.c
index 65ab533c2516..ca6abae9b09c 100644
--- a/tools/testing/selftests/bpf/trace_helpers.c
+++ b/tools/testing/selftests/bpf/trace_helpers.c
@@ -142,7 +142,7 @@ ssize_t get_uprobe_offset(const void *addr)
 {
 	size_t start, end, base;
 	char buf[256];
-	bool found;
+	bool found = false;
 	FILE *f;
 
 	f = fopen("/proc/self/maps", "r");
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf-next] selftests/bpf: fix a clang compilation error
  2022-01-27 16:37 [PATCH bpf-next] selftests/bpf: fix a clang compilation error Yonghong Song
@ 2022-01-27 17:50 ` Alexei Starovoitov
  0 siblings, 0 replies; 4+ messages in thread
From: Alexei Starovoitov @ 2022-01-27 17:50 UTC (permalink / raw)
  To: Yonghong Song
  Cc: bpf, Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, Kernel Team

On Thu, Jan 27, 2022 at 8:37 AM Yonghong Song <yhs@fb.com> wrote:
>
> When building selftests/bpf with clang
>   make -j LLVM=1
>   make -C tools/testing/selftests/bpf -j LLVM=1
> I hit the following compilation error:
>
>   trace_helpers.c:152:9: error: variable 'found' is used uninitialized whenever 'while' loop exits because its condition is false [-Werror,-Wsometimes-uninitialized]
>           while (fscanf(f, "%zx-%zx %s %zx %*[^\n]\n", &start, &end, buf, &base) == 4) {
>                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>   trace_helpers.c:161:7: note: uninitialized use occurs here
>           if (!found)
>                ^~~~~
>   trace_helpers.c:152:9: note: remove the condition if it is always true
>           while (fscanf(f, "%zx-%zx %s %zx %*[^\n]\n", &start, &end, buf, &base) == 4) {
>                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>                  1
>   trace_helpers.c:145:12: note: initialize the variable 'found' to silence this warning
>           bool found;
>                     ^
>                      = false
>
> It is possible that for sane /proc/self/maps we may never hit the above issue
> in practice. But let us initialize variable 'found' properly to silence the
> compilation error.
>
> Signed-off-by: Yonghong Song <yhs@fb.com>

Applied. Thanks

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf-next] selftests/bpf: fix a clang compilation error
  2022-01-26 18:19 Yonghong Song
@ 2022-01-26 20:07 ` Andrii Nakryiko
  0 siblings, 0 replies; 4+ messages in thread
From: Andrii Nakryiko @ 2022-01-26 20:07 UTC (permalink / raw)
  To: Yonghong Song
  Cc: bpf, Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, Kernel Team

On Wed, Jan 26, 2022 at 10:19 AM Yonghong Song <yhs@fb.com> wrote:
>
> Compiling kernel and selftests/bpf with latest llvm like blow:
>   make -j LLVM=1
>   make -C tools/testing/selftests/bpf -j LLVM=1
> I hit the following compilation error:
>   /.../prog_tests/log_buf.c:215:6: error: variable 'log_buf' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized]
>           if (!ASSERT_OK_PTR(raw_btf_data, "raw_btf_data_good"))
>               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>   /.../prog_tests/log_buf.c:264:7: note: uninitialized use occurs here
>           free(log_buf);
>                ^~~~~~~
>   /.../prog_tests/log_buf.c:215:2: note: remove the 'if' if its condition is always false
>           if (!ASSERT_OK_PTR(raw_btf_data, "raw_btf_data_good"))
>           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>   /.../prog_tests/log_buf.c:205:15: note: initialize the variable 'log_buf' to silence this warning
>           char *log_buf;
>                        ^
>                         = NULL
>   1 error generated.
>
> Compiler rightfully detected that log_buf is uninitialized in one of failure path as indicated
> in the above.
>

Yep, strange that GCC didn't detect this. Applied to bpf-next, thanks!

> Proper initialization of 'log_buf' variable fixed the issue.
>
> Signed-off-by: Yonghong Song <yhs@fb.com>
> ---
>  tools/testing/selftests/bpf/prog_tests/log_buf.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/log_buf.c b/tools/testing/selftests/bpf/prog_tests/log_buf.c
> index e469b023962b..1ef377a7e731 100644
> --- a/tools/testing/selftests/bpf/prog_tests/log_buf.c
> +++ b/tools/testing/selftests/bpf/prog_tests/log_buf.c
> @@ -202,7 +202,7 @@ static void bpf_btf_load_log_buf(void)
>         const void *raw_btf_data;
>         __u32 raw_btf_size;
>         struct btf *btf;
> -       char *log_buf;
> +       char *log_buf = NULL;
>         int fd = -1;
>
>         btf = btf__new_empty();
> --
> 2.30.2
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH bpf-next] selftests/bpf: fix a clang compilation error
@ 2022-01-26 18:19 Yonghong Song
  2022-01-26 20:07 ` Andrii Nakryiko
  0 siblings, 1 reply; 4+ messages in thread
From: Yonghong Song @ 2022-01-26 18:19 UTC (permalink / raw)
  To: bpf; +Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team

Compiling kernel and selftests/bpf with latest llvm like blow:
  make -j LLVM=1
  make -C tools/testing/selftests/bpf -j LLVM=1
I hit the following compilation error:
  /.../prog_tests/log_buf.c:215:6: error: variable 'log_buf' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized]
          if (!ASSERT_OK_PTR(raw_btf_data, "raw_btf_data_good"))
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  /.../prog_tests/log_buf.c:264:7: note: uninitialized use occurs here
          free(log_buf);
               ^~~~~~~
  /.../prog_tests/log_buf.c:215:2: note: remove the 'if' if its condition is always false
          if (!ASSERT_OK_PTR(raw_btf_data, "raw_btf_data_good"))
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  /.../prog_tests/log_buf.c:205:15: note: initialize the variable 'log_buf' to silence this warning
          char *log_buf;
                       ^
                        = NULL
  1 error generated.

Compiler rightfully detected that log_buf is uninitialized in one of failure path as indicated
in the above.

Proper initialization of 'log_buf' variable fixed the issue.

Signed-off-by: Yonghong Song <yhs@fb.com>
---
 tools/testing/selftests/bpf/prog_tests/log_buf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/log_buf.c b/tools/testing/selftests/bpf/prog_tests/log_buf.c
index e469b023962b..1ef377a7e731 100644
--- a/tools/testing/selftests/bpf/prog_tests/log_buf.c
+++ b/tools/testing/selftests/bpf/prog_tests/log_buf.c
@@ -202,7 +202,7 @@ static void bpf_btf_load_log_buf(void)
 	const void *raw_btf_data;
 	__u32 raw_btf_size;
 	struct btf *btf;
-	char *log_buf;
+	char *log_buf = NULL;
 	int fd = -1;
 
 	btf = btf__new_empty();
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-01-27 17:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-27 16:37 [PATCH bpf-next] selftests/bpf: fix a clang compilation error Yonghong Song
2022-01-27 17:50 ` Alexei Starovoitov
  -- strict thread matches above, loose matches on Subject: below --
2022-01-26 18:19 Yonghong Song
2022-01-26 20:07 ` Andrii Nakryiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).