linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: <Viktor.Rosendahl@bmw.de>
To: <jing.yangyang@zte.com.cn>, <rostedt@goodmis.org>, <cgel.zte@gmail.com>
Cc: <vulab@iscas.ac.cn>, <colin.king@canonical.com>,
	<linux-kernel@vger.kernel.org>, <zealci@zte.com.cn>
Subject: Re: [PATCH linux-next] tools/tracing: fix application of sizeof to pointer
Date: Fri, 20 Aug 2021 09:00:09 +0000	[thread overview]
Message-ID: <e43ee2b8ed6e69bdda1ab859a4a16bc80bbba8d6.camel@bmw.de> (raw)
In-Reply-To: <8fd4bb65ef3da67feac9ce3258cdbe9824752cf1.1629198502.git.jing.yangyang@zte.com.cn>

Hi Yangyang,

On Thu, 2021-08-19 at 23:00 -0700, CGEL wrote:
> From: jing yangyang <jing.yangyang@zte.com.cn>
> 
> sizeof when applied to a pointer typed expression gives the size of
> the pointer.
> 
> ./tools/tracing/latency/latency-collector.c:1541:10-16:ERROR application of
> sizeof to pointer
> 
> This issue was detected with the help of Coccinelle.
> 
> Reported-by: Zeal Robot <zealci@zte.com.cn>
> Signed-off-by: jing yangyang <jing.yangyang@zte.com.cn>
> ---
>  tools/tracing/latency/latency-collector.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/tools/tracing/latency/latency-collector.c
> b/tools/tracing/latency/latency-collector.c
> index 3a2e6bb..64d531d 100644
> --- a/tools/tracing/latency/latency-collector.c
> +++ b/tools/tracing/latency/latency-collector.c
> @@ -1538,7 +1538,7 @@ static void tracing_loop(void)
>  				mutex_lock(&print_mtx);
>  				check_signals();
>  				write_or_die(fd_stdout, queue_full_warning,
> -					     sizeof(queue_full_warning));
> +					     sizeof(*queue_full_warning));

The old code would give a size of 8, i.e. the size of the pointer. Your
suggestion will give a size of 1, i.e. the size of the first character of the
error message. So instead of ouputing "Could no" we would only write out "C".

What we want is the length of the error message. This could be achieved in two
ways:

1. By changing the sizeof(queue_full_warning) to strlen(queue_full_warning).

2. By changing the definition of queue_full_warning to be an array, in that case
we would like to use sizeof(queue_full_warning) - 1, the "- 1" comes from the
fact that we don't want to write out the terminating null character.

I think the first approach with strlen() is the better solution because it's
shorter and modern compilers will do the strlen() calculation of constant
strings at compile time anyway.

best regards,

Viktor 


>  				mutex_unlock(&print_mtx);
>  			}
>  			modified--;

  reply	other threads:[~2021-08-20  9:07 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-20  6:00 [PATCH linux-next] tools/tracing: fix application of sizeof to pointer CGEL
2021-08-20  9:00 ` Viktor.Rosendahl [this message]
2021-08-20 14:24   ` 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=e43ee2b8ed6e69bdda1ab859a4a16bc80bbba8d6.camel@bmw.de \
    --to=viktor.rosendahl@bmw.de \
    --cc=cgel.zte@gmail.com \
    --cc=colin.king@canonical.com \
    --cc=jing.yangyang@zte.com.cn \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=vulab@iscas.ac.cn \
    --cc=zealci@zte.com.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;
as well as URLs for NNTP newsgroup(s).