public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
From: Ihor Solodrai <ihor.solodrai@linux.dev>
To: Eduard Zingerman <eddyz87@gmail.com>,
	bpf@vger.kernel.org, ast@kernel.org, andrii@kernel.org
Cc: daniel@iogearbox.net, martin.lau@linux.dev, kernel-team@fb.com,
	yonghong.song@linux.dev, cupertino.miranda@oracle.com
Subject: Re: [PATCH 2/4] selftests/bpf: make str_has_pfx return pointer past the prefix
Date: Fri, 27 Mar 2026 14:54:18 -0700	[thread overview]
Message-ID: <e3db8a52-e526-403f-9eaa-215e4f364bb7@linux.dev> (raw)
In-Reply-To: <20260326-selftests-global-tags-ordering-v1-2-5dd2ced5d9ad@gmail.com>

On 3/26/26 10:39 AM, Eduard Zingerman wrote:
> Change str_has_pfx() to return a pointer to the first character after
> the prefix, thus eliminating the repetitive (s + sizeof(PFX) - 1)
> patterns.
> 
> Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
> ---
>  tools/testing/selftests/bpf/test_loader.c | 41 ++++++++++++++-----------------
>  1 file changed, 18 insertions(+), 23 deletions(-)
> 
> diff --git a/tools/testing/selftests/bpf/test_loader.c b/tools/testing/selftests/bpf/test_loader.c
> index 338c035c36884c02ac57cd17110122c10b4858e4..f1eb99f829e112f0e738bfe44e7e61a49120ec85 100644
> --- a/tools/testing/selftests/bpf/test_loader.c
> +++ b/tools/testing/selftests/bpf/test_loader.c
> @@ -11,8 +11,12 @@
>  #include "cap_helpers.h"
>  #include "jit_disasm_helpers.h"
>  
> -#define str_has_pfx(str, pfx) \
> -	(strncmp(str, pfx, __builtin_constant_p(pfx) ? sizeof(pfx) - 1 : strlen(pfx)) == 0)
> +static inline const char *str_has_pfx(const char *str, const char *pfx)
> +{
> +	size_t len = strlen(pfx);
> +
> +	return strncmp(str, pfx, len) == 0 ? str + len : NULL;
> +}
>  
>  #define TEST_LOADER_LOG_BUF_SZ 2097152
>  
> @@ -452,8 +456,8 @@ static int parse_test_spec(struct test_loader *tester,
>  			continue;
>  
>  		s = btf__str_by_offset(btf, t->name_off);
> -		if (str_has_pfx(s, TEST_TAG_DESCRIPTION_PFX)) {
> -			description = s + sizeof(TEST_TAG_DESCRIPTION_PFX) - 1;
> +		if ((val = str_has_pfx(s, TEST_TAG_DESCRIPTION_PFX))) {

Personally, I don't like assignments in the if expressions.
For example, this is annoying when you are stepping in the debugger.
But who does that in 2026, right?..

Acked-by: Ihor Solodrai <ihor.solodrai@linux.dev>

> +			description = val;
>  		} else if (strcmp(s, TEST_TAG_EXPECT_FAILURE) == 0) {
>  			spec->priv.expect_failure = true;
>  			spec->mode_mask |= PRIV;
> @@ -530,29 +534,24 @@ static int parse_test_spec(struct test_loader *tester,
>  			if (err)
>  				goto cleanup;
>  			spec->mode_mask |= UNPRIV;
> -		} else if (str_has_pfx(s, TEST_TAG_RETVAL_PFX)) {
> -			val = s + sizeof(TEST_TAG_RETVAL_PFX) - 1;
> +		} else if ((val = str_has_pfx(s, TEST_TAG_RETVAL_PFX))) {
>  			err = parse_retval(val, &spec->priv.retval, "__retval");
>  			if (err)
>  				goto cleanup;
>  			spec->priv.execute = true;
>  			spec->mode_mask |= PRIV;
> -		} else if (str_has_pfx(s, TEST_TAG_RETVAL_PFX_UNPRIV)) {
> -			val = s + sizeof(TEST_TAG_RETVAL_PFX_UNPRIV) - 1;
> +		} else if ((val = str_has_pfx(s, TEST_TAG_RETVAL_PFX_UNPRIV))) {
>  			err = parse_retval(val, &spec->unpriv.retval, "__retval_unpriv");
>  			if (err)
>  				goto cleanup;
>  			spec->mode_mask |= UNPRIV;
>  			spec->unpriv.execute = true;
>  			has_unpriv_retval = true;
> -		} else if (str_has_pfx(s, TEST_TAG_LOG_LEVEL_PFX)) {
> -			val = s + sizeof(TEST_TAG_LOG_LEVEL_PFX) - 1;
> +		} else if ((val = str_has_pfx(s, TEST_TAG_LOG_LEVEL_PFX))) {
>  			err = parse_int(val, &spec->log_level, "test log level");
>  			if (err)
>  				goto cleanup;
> -		} else if (str_has_pfx(s, TEST_TAG_PROG_FLAGS_PFX)) {
> -			val = s + sizeof(TEST_TAG_PROG_FLAGS_PFX) - 1;
> -
> +		} else if ((val = str_has_pfx(s, TEST_TAG_PROG_FLAGS_PFX))) {
>  			clear = val[0] == '!';
>  			if (clear)
>  				val++;
> @@ -577,8 +576,7 @@ static int parse_test_spec(struct test_loader *tester,
>  					goto cleanup;
>  				update_flags(&spec->prog_flags, flags, clear);
>  			}
> -		} else if (str_has_pfx(s, TEST_TAG_ARCH)) {
> -			val = s + sizeof(TEST_TAG_ARCH) - 1;
> +		} else if ((val = str_has_pfx(s, TEST_TAG_ARCH))) {
>  			if (strcmp(val, "X86_64") == 0) {
>  				arch = ARCH_X86_64;
>  			} else if (strcmp(val, "ARM64") == 0) {
> @@ -596,16 +594,14 @@ static int parse_test_spec(struct test_loader *tester,
>  			collect_jit = get_current_arch() == arch;
>  			unpriv_jit_on_next_line = true;
>  			jit_on_next_line = true;
> -		} else if (str_has_pfx(s, TEST_BTF_PATH)) {
> -			spec->btf_custom_path = s + sizeof(TEST_BTF_PATH) - 1;
> -		} else if (str_has_pfx(s, TEST_TAG_CAPS_UNPRIV)) {
> -			val = s + sizeof(TEST_TAG_CAPS_UNPRIV) - 1;
> +		} else if ((val = str_has_pfx(s, TEST_BTF_PATH))) {
> +			spec->btf_custom_path = val;
> +		} else if ((val = str_has_pfx(s, TEST_TAG_CAPS_UNPRIV))) {
>  			err = parse_caps(val, &spec->unpriv.caps, "test caps");
>  			if (err)
>  				goto cleanup;
>  			spec->mode_mask |= UNPRIV;
> -		} else if (str_has_pfx(s, TEST_TAG_LOAD_MODE_PFX)) {
> -			val = s + sizeof(TEST_TAG_LOAD_MODE_PFX) - 1;
> +		} else if ((val = str_has_pfx(s, TEST_TAG_LOAD_MODE_PFX))) {
>  			if (strcmp(val, "jited") == 0) {
>  				load_mask = JITED;
>  			} else if (strcmp(val, "no_jited") == 0) {
> @@ -635,12 +631,11 @@ static int parse_test_spec(struct test_loader *tester,
>  					      &spec->unpriv.stdout);
>  			if (err)
>  				goto cleanup;
> -		} else if (str_has_pfx(s, TEST_TAG_LINEAR_SIZE)) {
> +		} else if ((val = str_has_pfx(s, TEST_TAG_LINEAR_SIZE))) {
>  			switch (bpf_program__type(prog)) {
>  			case BPF_PROG_TYPE_SCHED_ACT:
>  			case BPF_PROG_TYPE_SCHED_CLS:
>  			case BPF_PROG_TYPE_CGROUP_SKB:
> -				val = s + sizeof(TEST_TAG_LINEAR_SIZE) - 1;
>  				err = parse_int(val, &spec->linear_sz, "test linear size");
>  				if (err)
>  					goto cleanup;
> 


  reply	other threads:[~2026-03-27 21:54 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-26 17:39 [PATCH 0/4] selftests/bpf: impose global ordering for test decl_tags Eduard Zingerman
2026-03-26 17:39 ` [PATCH 1/4] selftests/bpf: fix __jited_unpriv tag name Eduard Zingerman
2026-03-27 21:52   ` Ihor Solodrai
2026-03-26 17:39 ` [PATCH 2/4] selftests/bpf: make str_has_pfx return pointer past the prefix Eduard Zingerman
2026-03-27 21:54   ` Ihor Solodrai [this message]
2026-03-27 21:57     ` Eduard Zingerman
2026-03-26 17:39 ` [PATCH 3/4] selftests/bpf: impose global ordering for test decl_tags Eduard Zingerman
2026-03-26 22:47   ` Andrii Nakryiko
2026-03-26 22:51     ` Eduard Zingerman
2026-03-26 23:00       ` Andrii Nakryiko
2026-03-26 23:30         ` Eduard Zingerman
2026-03-27 22:00   ` Ihor Solodrai
2026-03-27 22:07     ` Eduard Zingerman
2026-03-26 17:39 ` [PATCH 4/4] selftests/bpf: inline TEST_TAG constants in test_loader.c Eduard Zingerman
2026-03-27 22:04   ` Ihor Solodrai
2026-03-26 22:28 ` [PATCH 0/4] selftests/bpf: impose global ordering for test decl_tags Eduard Zingerman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e3db8a52-e526-403f-9eaa-215e4f364bb7@linux.dev \
    --to=ihor.solodrai@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=cupertino.miranda@oracle.com \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=kernel-team@fb.com \
    --cc=martin.lau@linux.dev \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox