mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Peter Mamonov <pmamonov@gmail.com>
Cc: barebox@lists.infradead.org
Subject: Re: [PATCH] lib: parse_area_spec: don't modify *start and *size values if parse failed
Date: Thu, 11 Jan 2018 09:15:31 +0100	[thread overview]
Message-ID: <20180111081531.l4yu7xdxyxrtl7gx@pengutronix.de> (raw)
In-Reply-To: <20180109142120.5498-1-pmamonov@gmail.com>

Hi Peter,

On Tue, Jan 09, 2018 at 05:21:20PM +0300, Peter Mamonov wrote:
> Signed-off-by: Peter Mamonov <pmamonov@gmail.com>
> ---
>  lib/misc.c | 46 ++++++++++++++++++++++++++++++++--------------
>  1 file changed, 32 insertions(+), 14 deletions(-)
> 
> diff --git a/lib/misc.c b/lib/misc.c
> index 62ddd6677..c7d5a0ca5 100644
> --- a/lib/misc.c
> +++ b/lib/misc.c
> @@ -79,38 +79,56 @@ EXPORT_SYMBOL(strtoul_suffix);
>  int parse_area_spec(const char *str, loff_t *start, loff_t *size)
>  {
>  	char *endp;
> -	loff_t end;
> +	loff_t end, _start, _size;
> +	int ret = -1;
>  
>  	if (!isdigit(*str))
>  		return -1;
>  
> -	*start = strtoull_suffix(str, &endp, 0);
> +	_start = strtoull_suffix(str, &endp, 0);
>  
>  	str = endp;
>  
>  	if (!*str) {
>  		/* beginning given, but no size, assume maximum size */
> -		*size = ~0;
> -		return 0;
> +		_size = ~0;
> +		ret = 0;
>  	}
>  
> -	if (*str == '-') {
> +	if (ret && *str == '-') {
>  		/* beginning and end given */
> -		end = strtoull_suffix(str + 1, NULL, 0);
> -		if (end < *start) {
> +		if (!isdigit(*(str + 1)))
> +			return ret;
> +
> +		end = strtoull_suffix(str + 1, &endp, 0);
> +		str = endp;
> +		if (end < _start) {
>  			printf("end < start\n");
> -			return -1;
> +			return ret;
>  		}
> -		*size = end - *start + 1;
> -		return 0;
> +		_size = end - _start + 1;
> +		ret = 0;
>  	}
>  
> -	if (*str == '+') {
> +	if (ret && *str == '+') {
>  		/* beginning and size given */
> -		*size = strtoull_suffix(str + 1, NULL, 0);
> -		return 0;
> +		if (!isdigit(*(str + 1)))
> +			return ret;
> +
> +		_size = strtoull_suffix(str + 1, &endp, 0);
> +		str = endp;
> +		ret = 0;
> +	}
> +
> +	if (!ret && *str)
> +		/* trailing symbols indicate invalid area spec */
> +		ret = -1;

Is this correct? I would assume a whitespace should be fine. We only
do not get trailing whitespaces in here because current users pass in
argv[] elements which are split up at whitespaces. The check would
also deserve a separate patch.

> +
> +	if (!ret) {
> +		*start = _start;
> +		*size = _size;
>  	}
>  
> -	return -1;
> +	return ret;

I find this patch unnecessarily hard to review and also the end result
doesn't look optimal. Could you create a 'success:' label and jump to it
when everything is fine? That would make the additional if(ret) and
if(!ret) checks unnecessary.

Sascha

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  reply	other threads:[~2018-01-11  8:15 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-20 10:48 command: hashsum: md5sum reports wrong sum for the file named 4k.bin Peter Mamonov
2017-12-21 10:48 ` Peter Mamonov
2018-01-05 11:44   ` Sascha Hauer
2018-01-05 18:17     ` Peter Mamonov
2018-01-09 11:10       ` Sascha Hauer
2018-01-09 14:21         ` [PATCH] lib: parse_area_spec: don't modify *start and *size values if parse failed Peter Mamonov
2018-01-11  8:15           ` Sascha Hauer [this message]
2018-01-11 17:28             ` Peter Mamonov
2018-01-15 11:32             ` [PATCH v2 1/3] lib: parse_area_spec: don't modify *start value " Peter Mamonov
2018-01-15 11:32               ` [PATCH v2 2/3] lib: parse_area_spec: part of the area spec after -/+ should start with a digit Peter Mamonov
2018-01-15 11:32               ` [PATCH v2 3/3] lib: parse_area_spec: no extra symbols after area spec are allowed except for spaces Peter Mamonov
2018-01-17  8:11               ` [PATCH v2 1/3] lib: parse_area_spec: don't modify *start value if parse failed Sascha Hauer
2018-01-17  9:31                 ` Peter Mamonov

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=20180111081531.l4yu7xdxyxrtl7gx@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=pmamonov@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