BPF List
 help / color / mirror / Atom feed
From: Eduard Zingerman <eddyz87@gmail.com>
To: Mykyta Yatsenko <mykyta.yatsenko5@gmail.com>,
	bpf@vger.kernel.org,  ast@kernel.org, andrii@kernel.org,
	daniel@iogearbox.net, kafai@meta.com,  kernel-team@meta.com
Cc: Mykyta Yatsenko <yatsenko@meta.com>
Subject: Re: [PATCH bpf-next v5 1/2] selftests/bpf: implement setting global variables in veristat
Date: Tue, 25 Feb 2025 11:04:26 -0800	[thread overview]
Message-ID: <b8b36cfa2700a753e85468591ac3ec458b3a5fa5.camel@gmail.com> (raw)
In-Reply-To: <20250225163101.121043-2-mykyta.yatsenko5@gmail.com>

On Tue, 2025-02-25 at 16:31 +0000, Mykyta Yatsenko wrote:

New warning for trying to set non-enums from enumerators works fine.
This still can be abused if numeric value outside of the supported
range is specified, but that's fine, I think.

Acked-by: Eduard Zingerman <eddyz87@gmail.com>

[...]

> @@ -1292,6 +1320,268 @@ static int process_prog(const char *filename, struct bpf_object *obj, struct bpf
>  	return 0;
>  };
>  
> +static int append_var_preset(struct var_preset **presets, int *cnt, const char *expr)
> +{
> +	void *tmp;
> +	struct var_preset *cur;
> +	char var[256], val[256];
> +	long long value;
> +	int r, n, val_len;
> +
> +	tmp = realloc(*presets, (*cnt + 1) * sizeof(**presets));
> +	if (!tmp)
> +		return -ENOMEM;
> +	*presets = tmp;
> +	cur = &(*presets)[*cnt];
> +	cur->applied = false;
> +
> +	if (sscanf(expr, "%s = %s\n", var, val) != 2) {
> +		fprintf(stderr, "Could not parse expression %s\n", expr);
> +		return -EINVAL;
> +	}
> +
> +	val_len = strlen(val);
> +	errno = 0;
> +	r = sscanf(val, "%lli %n", &value, &n);
> +	if (r == 1 && n == val_len) {
> +		if (errno == ERANGE) {
> +			/* Try parsing as unsigned */
> +			errno = 0;
> +			r = sscanf(val, "%llu %n", &value, &n);
> +			/* Try hex if not all chars consumed */
> +			if (n != val_len) {
> +				errno = 0;
> +				r = sscanf(val, "%llx %n", &value, &n);

The discrepancy between %lli accepting 0x but %llu not accepting 0x is
annoying unfortunate. Does not look simpler then before, but let's
merge this already.

> +			}
> +		}
> +		if (errno || r != 1 || n != val_len) {
> +			fprintf(stderr, "Could not parse value %s\n", val);
> +			return -EINVAL;
> +		}
> +		cur->ivalue = value;
> +		cur->type = INTEGRAL;
> +	} else {
> +		/* If not a number, consider it enum value */
> +		cur->svalue = strdup(val);
> +		if (!cur->svalue)
> +			return -ENOMEM;
> +
> +		cur->type = ENUMERATOR;
> +	}
> +
> +	cur->name = strdup(var);
> +	if (!cur->name)
> +		return -ENOMEM;
> +
> +	(*cnt)++;
> +	return 0;
> +}

[...]


  reply	other threads:[~2025-02-25 19:04 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-25 16:30 [PATCH bpf-next v5 0/2] selftests/bpf: implement setting global variables in veristat Mykyta Yatsenko
2025-02-25 16:31 ` [PATCH bpf-next v5 1/2] " Mykyta Yatsenko
2025-02-25 19:04   ` Eduard Zingerman [this message]
2025-02-25 21:56     ` Andrii Nakryiko
2025-02-26 18:47       ` Andrii Nakryiko
2025-02-25 16:31 ` [PATCH bpf-next v5 2/2] selftests/bpf: introduce veristat test Mykyta Yatsenko
2025-02-25 18:10   ` Eduard Zingerman
2025-02-26 18:50 ` [PATCH bpf-next v5 0/2] selftests/bpf: implement setting global variables in veristat patchwork-bot+netdevbpf

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=b8b36cfa2700a753e85468591ac3ec458b3a5fa5.camel@gmail.com \
    --to=eddyz87@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kafai@meta.com \
    --cc=kernel-team@meta.com \
    --cc=mykyta.yatsenko5@gmail.com \
    --cc=yatsenko@meta.com \
    /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