linux-trace-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ross Zwisler <zwisler@google.com>
To: Steven Rostedt <rostedt@goodmis.org>
Cc: Linux Trace Devel <linux-trace-devel@vger.kernel.org>,
	Stevie Alvarez <stevie.6strings@gmail.com>
Subject: Re: [PATCH] libtraceeval: Fix comparing unsigned against zero
Date: Mon, 2 Oct 2023 12:31:02 -0600	[thread overview]
Message-ID: <20231002183102.GA1512616@google.com> (raw)
In-Reply-To: <20230927041629.43ffbfd9@rorschach.local.home>

On Wed, Sep 27, 2023 at 04:16:29AM -0400, Steven Rostedt wrote:
> From: "Steven Rostedt (Google)" <rostedt@goodmis.org>
> 
> nr_key_types and nr_val_types are both size_t which is an unsized number.
> A compare against zero is always false:
> 
> 	if (teval->nr_key_types < 0)
> 
> Typecast it to signed for these comparisons.
> 
> Also change the few places the "size_t i" is used where it does:
> 
> 	for (; i >= 0; i--)
> 
> as that too needs to be signed to work.
> 
> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
> ---
>  src/histograms.c | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)
> 
> diff --git a/src/histograms.c b/src/histograms.c
> index 96f0926f062c..42959c154a11 100644
> --- a/src/histograms.c
> +++ b/src/histograms.c
> @@ -159,7 +159,7 @@ static size_t type_alloc(const struct traceeval_type *defs,
>  {
>  	struct traceeval_type *new_defs = NULL;
>  	size_t size;
> -	size_t i;
> +	ssize_t i;
>  
>  	*copy = NULL;
>  
> @@ -196,7 +196,7 @@ fail:
>  	else
>  		print_err("traceeval_type list missing a name");
>  
> -	for (; i >=0; i--)
> +	for (; i >= 0; i--)
>  		free(new_defs[i].name);
>  	free(new_defs);
>  	return -1;
> @@ -303,14 +303,14 @@ struct traceeval *traceeval_init(struct traceeval_type *keys,
>  
>  	/* alloc key types */
>  	teval->nr_key_types = type_alloc(keys, &teval->key_types);
> -	if (teval->nr_key_types <= 0) {
> +	if ((ssize_t)teval->nr_key_types <= 0) {

Rather than casting, wouldn't it be better to just change the type of
nr_key_types and nr_val_types to ssize_t?  I think this should be the return
type of type_alloc() as well, since this returns -1 on error.

>  		err_msg = "Failed to allocate user defined keys";
>  		goto fail_release;
>  	}
>  
>  	/* alloc val types */
>  	teval->nr_val_types = type_alloc(vals, &teval->val_types);
> -	if (teval->nr_val_types < 0) {
> +	if ((ssize_t)teval->nr_val_types < 0) {
>  		err_msg = "Failed to allocate user defined values";
>  		goto fail_release;
>  	}
> @@ -785,7 +785,7 @@ static int update_entry(struct traceeval *teval, struct entry *entry,
>  	union traceeval_data *copy = entry->vals;
>  	union traceeval_data old[teval->nr_val_types];
>  	size_t size = teval->nr_val_types;
> -	size_t i;
> +	ssize_t i;
>  
>  	if (!size)
>  		return 0;
> -- 
> 2.40.1
> 

  reply	other threads:[~2023-10-02 18:31 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-27  8:16 [PATCH] libtraceeval: Fix comparing unsigned against zero Steven Rostedt
2023-10-02 18:31 ` Ross Zwisler [this message]
2023-10-02 18:37   ` Steven Rostedt
2023-10-03 13:07     ` 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=20231002183102.GA1512616@google.com \
    --to=zwisler@google.com \
    --cc=linux-trace-devel@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=stevie.6strings@gmail.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;
as well as URLs for NNTP newsgroup(s).