[PATCH] perf test: Fix undefined behavior in bp_account

Ian Rogers posted 1 patch 3 years, 10 months ago
tools/perf/tests/bp_account.c | 16 ++++++++++++++--
1 file changed, 14 insertions(+), 2 deletions(-)
[PATCH] perf test: Fix undefined behavior in bp_account
Posted by Ian Rogers 3 years, 10 months ago
Fix:
tests/bp_account.c:154:9: runtime error: variable length array bound evaluates to non-positive value 0
by switching from a variable length to an allocated array.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/tests/bp_account.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/tools/perf/tests/bp_account.c b/tools/perf/tests/bp_account.c
index d1ebb5561e5b..6f921db33cf9 100644
--- a/tools/perf/tests/bp_account.c
+++ b/tools/perf/tests/bp_account.c
@@ -151,11 +151,21 @@ static int detect_ioctl(void)
 static int detect_share(int wp_cnt, int bp_cnt)
 {
 	struct perf_event_attr attr;
-	int i, fd[wp_cnt + bp_cnt], ret;
+	int i, *fd = NULL, ret = -1;
+
+	if (wp_cnt + bp_cnt == 0)
+		return 0;
+
+	fd = malloc(sizeof(int) * (wp_cnt + bp_cnt));
+	if (!fd)
+		return -1;
 
 	for (i = 0; i < wp_cnt; i++) {
 		fd[i] = wp_event((void *)&the_var, &attr);
-		TEST_ASSERT_VAL("failed to create wp\n", fd[i] != -1);
+		if (fd[i] == -1) {
+			pr_err("failed to create wp\n");
+			goto out;
+		}
 	}
 
 	for (; i < (bp_cnt + wp_cnt); i++) {
@@ -166,9 +176,11 @@ static int detect_share(int wp_cnt, int bp_cnt)
 
 	ret = i != (bp_cnt + wp_cnt);
 
+out:
 	while (i--)
 		close(fd[i]);
 
+	free(fd);
 	return ret;
 }
 
-- 
2.36.1.476.g0c4daa206d-goog
Re: [PATCH] perf test: Fix undefined behavior in bp_account
Posted by Arnaldo Carvalho de Melo 3 years, 10 months ago
Em Fri, Jun 10, 2022 at 11:02:47AM -0700, Ian Rogers escreveu:
> Fix:
> tests/bp_account.c:154:9: runtime error: variable length array bound evaluates to non-positive value 0
> by switching from a variable length to an allocated array.

Thanks, applied.

- Arnaldo

 
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
>  tools/perf/tests/bp_account.c | 16 ++++++++++++++--
>  1 file changed, 14 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/perf/tests/bp_account.c b/tools/perf/tests/bp_account.c
> index d1ebb5561e5b..6f921db33cf9 100644
> --- a/tools/perf/tests/bp_account.c
> +++ b/tools/perf/tests/bp_account.c
> @@ -151,11 +151,21 @@ static int detect_ioctl(void)
>  static int detect_share(int wp_cnt, int bp_cnt)
>  {
>  	struct perf_event_attr attr;
> -	int i, fd[wp_cnt + bp_cnt], ret;
> +	int i, *fd = NULL, ret = -1;
> +
> +	if (wp_cnt + bp_cnt == 0)
> +		return 0;
> +
> +	fd = malloc(sizeof(int) * (wp_cnt + bp_cnt));
> +	if (!fd)
> +		return -1;
>  
>  	for (i = 0; i < wp_cnt; i++) {
>  		fd[i] = wp_event((void *)&the_var, &attr);
> -		TEST_ASSERT_VAL("failed to create wp\n", fd[i] != -1);
> +		if (fd[i] == -1) {
> +			pr_err("failed to create wp\n");
> +			goto out;
> +		}
>  	}
>  
>  	for (; i < (bp_cnt + wp_cnt); i++) {
> @@ -166,9 +176,11 @@ static int detect_share(int wp_cnt, int bp_cnt)
>  
>  	ret = i != (bp_cnt + wp_cnt);
>  
> +out:
>  	while (i--)
>  		close(fd[i]);
>  
> +	free(fd);
>  	return ret;
>  }
>  
> -- 
> 2.36.1.476.g0c4daa206d-goog

-- 

- Arnaldo