Linux Trace Kernel
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: Pengpeng Hou <pengpeng@iscas.ac.cn>
Cc: Masami Hiramatsu <mhiramat@kernel.org>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v4 2/2] tracing: Bound histogram expression strings with seq_buf
Date: Thu, 21 May 2026 10:44:45 -0400	[thread overview]
Message-ID: <20260521104445.29827219@gandalf.local.home> (raw)
In-Reply-To: <20260521022817.38453-2-pengpeng@iscas.ac.cn>

On Thu, 21 May 2026 10:28:17 +0800
Pengpeng Hou <pengpeng@iscas.ac.cn> wrote:

> @@ -1778,47 +1783,56 @@ static char *expr_str(struct hist_field *field, unsigned int level)
>  	if (!expr)
>  		return ERR_PTR(-ENOMEM);
>  
> +	seq_buf_init(&s, expr, MAX_FILTER_STR_VAL);
> +
>  	if (!field->operands[0]) {
> -		expr_field_str(field, expr);
> +		if (!expr_field_str(field, &s))
> +			return ERR_PTR(-E2BIG);
> +
>  		return_ptr(expr);
>  	}
>  
>  	if (field->operator == FIELD_OP_UNARY_MINUS) {
> -		char *subexpr;
> +		char *subexpr __free(kfree) = NULL;
>  
> -		strcat(expr, "-(");
> +		seq_buf_puts(&s, "-(");
>  		subexpr = expr_str(field->operands[0], ++level);
>  		if (IS_ERR(subexpr))
>  			return subexpr;
>  
> -		strcat(expr, subexpr);
> -		strcat(expr, ")");
> +		seq_buf_puts(&s, subexpr);
> +		seq_buf_putc(&s, ')');
> +		seq_buf_str(&s);
>  
> -		kfree(subexpr);
> +		if (seq_buf_has_overflowed(&s))
> +			return ERR_PTR(-E2BIG);
>  
>  		return_ptr(expr);
>  	}

Wouldn't the above if statement be a lot nicer as:

 	if (field->operator == FIELD_OP_UNARY_MINUS) {
		char *subexpr;

		subexpr = expr_str(field->operands[0], ++level);
		if (IS_ERR(subexpr))
			return subexpr;

		seq_buf_printf(&s, "-(%s)", subexpr); 
		seq_buf_str(&s);
  		kfree(subexpr);

		if (seq_buf_has_overflowed(&s))
			return ERR_PTR(-E2BIG);
 
		return_ptr(expr);
 	}

In fact, the above is so simple, you don't even need to use the __free()
guard on subexpr.

BTW, because currently seq_buf_printf() does add a '\0' to the string, I
may update the API for seq_buf to state that you don't need to terminate
after calling that function. So you can leave out the sub_buf_str() after
calling seq_buf_printf().

-- Steve

  reply	other threads:[~2026-05-21 14:44 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-21  2:28 [PATCH v4 1/2] tracing: Return ERR_PTR() from expr_str() Pengpeng Hou
2026-05-21  2:28 ` [PATCH v4 2/2] tracing: Bound histogram expression strings with seq_buf Pengpeng Hou
2026-05-21 14:44   ` Steven Rostedt [this message]
2026-05-21 14:30 ` [PATCH v4 1/2] tracing: Return ERR_PTR() from expr_str() Steven Rostedt

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=20260521104445.29827219@gandalf.local.home \
    --to=rostedt@goodmis.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mhiramat@kernel.org \
    --cc=pengpeng@iscas.ac.cn \
    /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