linux-trace-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: Vladislav Valtchev <vladislav.valtchev@gmail.com>
Cc: linux-trace-devel@vger.kernel.org, linux-kernel@vger.kernel.org,
	y.karadz@gmail.com
Subject: Re: [PATCH v3 1/3] trace-cmd: Make read_proc() to return int status via OUT arg
Date: Tue, 16 Jan 2018 14:42:49 -0500	[thread overview]
Message-ID: <20180116144249.308a15d7@gandalf.local.home> (raw)
In-Reply-To: <1516129836.4042.23.camel@gmail.com>

On Tue, 16 Jan 2018 21:10:36 +0200
Vladislav Valtchev <vladislav.valtchev@gmail.com> wrote:

> On Tue, 2018-01-16 at 12:19 -0500, Steven Rostedt wrote:
> > On Tue, 16 Jan 2018 09:47:42 +0200
> > "Vladislav Valtchev (VMware)" <vladislav.valtchev@gmail.com> wrote:
> >   
> > > +	errno = 0;
> > > +
> > > +	/* Read an integer from buf ignoring any non-digit trailing characters. */
> > > +	num = strtol(buf, NULL, 10);
> > > +
> > > +	/* strtol() returned 0: we have to check for errors */
> > > +	if (!num && (errno == EINVAL || errno == ERANGE))
> > > +		return -1;  
> > 
> > Repeating again here. According to the man page of strtol():  
> 
> v3 addresses only the comments for patch 3/3.
> I'm sorry for that. All the other comments will be addressed in v4.
> 
> > 
> > RETURN VALUE
> >        The  strtol() function returns the result of the conversion, unless the
> >        value would underflow or overflow.  If an  underflow  occurs,  strtol()
> >        returns  LONG_MIN.   If  an overflow occurs, strtol() returns LONG_MAX.
> >        In both cases, errno is set to ERANGE.  Precisely the  same  holds  for
> >        strtoll()  (with  LLONG_MIN  and  LLONG_MAX  instead  of  LONG_MIN  and
> >        LONG_MAX).
> > 
> > and this:
> > 
> >        The implementation may also set errno to EINVAL in case  no  conversion
> >        was performed (no digits seen, and 0 returned).
> > 
> > Thus, !num is not enough. The example in the man page has:
> > 
> >            errno = 0;    /* To distinguish success/failure after call */
> >            val = strtol(str, &endptr, base);
> > 
> >            /* Check for various possible errors */
> > 
> >            if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
> >                    || (errno != 0 && val == 0)) {
> >                perror("strtol");
> >                exit(EXIT_FAILURE);
> >            }
> > 
> > Let's follow this.
> > 
> > -- Steve  
> 
> Sure, I thought that:
> 
> 	errno = 0;
> 	num = strtol(buf, NULL, 10);
> 
> 	/* strtol() returned 0: we have to check for errors */
> 	if (!num && (errno == EINVAL || errno == ERANGE))
> 		return -1;
> 
> 	if (num > INT_MAX || num < INT_MIN)
> 		return -1;
> 
> covered all the cases because the case:
> (val == LONG_MAX || val == LONG_MIN)
> 
> is covered by: if (num > INT_MAX || num < INT_MIN)
> [no matter the errno]
> 
> but that's not true for 32 bit systems where sizeof(long) == sizeof(int).
> It had to be: if (num >= INT_MAX || num <= INT_MIN), but in that
> case it would exclude two valid int32 values.
> 
> Therefore, let's go with:
> 	if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
>             || (errno != 0 && val == 0))
> 
> 
> Just let me keep also the following check:
> 
> 	if (num > INT_MAX || num < INT_MIN)
> 		return -1;
> 
> since [INT_MIN, INT_MAX] is a subset of [LONG_MIN, LONG_MAX].
> 

True. What about just doing:


	if (num > INT_MAX || num < INT_MIN || (!num && errno))

That should cover it all, and match what the man pages have.

-- Steve

  reply	other threads:[~2018-01-16 19:42 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-16  7:47 [PATCH v3 0/3] Integrate stack tracer status in 'stat' Vladislav Valtchev (VMware)
2018-01-16  7:47 ` [PATCH v3 1/3] trace-cmd: Make read_proc() to return int status via OUT arg Vladislav Valtchev (VMware)
2018-01-16 17:19   ` Steven Rostedt
2018-01-16 19:10     ` Vladislav Valtchev
2018-01-16 19:42       ` Steven Rostedt [this message]
2018-01-16 17:30   ` Steven Rostedt
2018-01-16  7:47 ` [PATCH v3 2/3] trace-cmd: Remove the die() call from read_proc() Vladislav Valtchev (VMware)
2018-01-16  7:47 ` [PATCH v3 3/3] trace-cmd: Making stat to report when the stack tracer is ON Vladislav Valtchev (VMware)

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=20180116144249.308a15d7@gandalf.local.home \
    --to=rostedt@goodmis.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-devel@vger.kernel.org \
    --cc=vladislav.valtchev@gmail.com \
    --cc=y.karadz@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).