linux-i2c.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jean Delvare <khali-PUYAD+kWke1g9hUCZPvPmw@public.gmane.org>
To: Guenter Roeck <guenter.roeck-IzeFyvvaP7pWk0Htik3J/w@public.gmane.org>
Cc: linux-i2c-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Subject: Re: [PATCH v2 6/6] i2cset: Get command/mode before reading data
Date: Mon, 14 Feb 2011 10:43:41 +0100	[thread overview]
Message-ID: <20110214104341.3791fddb@endymion.delvare> (raw)
In-Reply-To: <1297628246-19367-7-git-send-email-guenter.roeck-IzeFyvvaP7pWk0Htik3J/w@public.gmane.org>

On Sun, 13 Feb 2011 12:17:26 -0800, Guenter Roeck wrote:
> Get and validate the command/mode parameter for all commands
> before reading and evaluating actual data.
> 
> Signed-off-by: Guenter Roeck <guenter.roeck-IzeFyvvaP7pWk0Htik3J/w@public.gmane.org>
> ---
>  tools/i2cset.c |   99 +++++++++++++++++++++++++++++++-------------------------
>  1 files changed, 55 insertions(+), 44 deletions(-)
> 
> diff --git a/tools/i2cset.c b/tools/i2cset.c
> index 33290ef..d8d51a2 100644
> --- a/tools/i2cset.c
> +++ b/tools/i2cset.c
> @@ -207,18 +207,37 @@ int main(int argc, char *argv[])
>  		help();
>  	}
>  
> -	/* check for block data */
> -	len = 0;
> -	if (argc > flags + 5) {
> +	/* check for command/mode */
> +	if (argc == flags + 4) {
> +		/* Implicit "c" */
> +		size = I2C_SMBUS_BYTE;
> +	} else if (argc == flags + 5) {
> +		/* "c", "cp",  or implicit "b" */
> +		if (!strcmp(argv[flags+4], "c")
> +		 || !strcmp(argv[flags+4], "cp")) {
> +			size = I2C_SMBUS_BYTE;
> +			pec = argv[flags+4][1] == 'p';
> +		} else {
> +			size = I2C_SMBUS_BYTE_DATA;
> +		}
> +	} else {
> +		/* All other commands */
> +		if (strlen(argv[argc-1]) > 2
> +		    || (strlen(argv[argc-1]) == 2 && argv[argc-1][1] != 'p')) {
> +			fprintf(stderr, "Error: Invalid mode '%s'!\n", argv[argc-1]);
> +			help();
> +		}
>  		switch (argv[argc-1][0]) {
> +		case 'b': size = I2C_SMBUS_BYTE_DATA; break;
> +		case 'w': size = I2C_SMBUS_WORD_DATA; break;
>  		case 's': size = I2C_SMBUS_BLOCK_DATA; break;
>  		case 'i': size = I2C_SMBUS_I2C_BLOCK_DATA; break;
>  		default:
> -			size = 0;
> -			break;
> +			fprintf(stderr, "Error: Invalid mode '%s'!\n", argv[argc-1]);
> +			help();
>  		}
> +		pec = argv[argc-1][1] == 'p';
>  		if (size == I2C_SMBUS_BLOCK_DATA || size == I2C_SMBUS_I2C_BLOCK_DATA) {
> -			pec = argv[argc-1][1] == 'p';
>  			if (argc > (int)sizeof(block) + flags + 5) {
>  				fprintf(stderr, "Error: Bad number of arguments!\n");
>  				help();
> @@ -231,49 +250,48 @@ int main(int argc, char *argv[])
>  				fprintf(stderr, "Error: Mask not supported for block writes!\n");
>  				help();
>  			}
> -			for (len = 0; len + flags + 5 < argc; len++) {
> -				value = strtol(argv[flags + len + 4], &end, 0);
> -				if (*end || value < 0 || value > 0xff) {
> -                                	fprintf(stderr, "Error: Block data value invalid!\n");
> -                                	help();
> -                        	}
> -				block[len] = value;
> -			}
> -			goto dofile;
>  		} else if (argc != flags + 6) {
>  			fprintf(stderr, "Error: Bad number of arguments!\n");
>  			help();
>  		}
>  	}
>  
> -	if (argc > flags + 4) {
> -		if (!strcmp(argv[flags+4], "c")
> -		 || !strcmp(argv[flags+4], "cp")) {
> -			size = I2C_SMBUS_BYTE;
> -			value = -1;
> -			pec = argv[flags+4][1] == 'p';
> -		} else {
> -			size = I2C_SMBUS_BYTE_DATA;
> -			value = strtol(argv[flags+4], &end, 0);
> +	len = 0; /* Must always initialize len since it is passed to confirm() */
> +
> +	/* read values from command line */
> +	switch (size) {
> +	case I2C_SMBUS_BYTE_DATA:
> +	case I2C_SMBUS_WORD_DATA:
> +		value = strtol(argv[flags+4], &end, 0);
> +		if (*end || value < 0) {
> +			fprintf(stderr, "Error: Data value invalid!\n");
> +			help();
> +		}
> +		if ((size == I2C_SMBUS_BYTE_DATA && value > 0xff)
> +		    || (size == I2C_SMBUS_WORD_DATA && value > 0xffff)) {
> +			fprintf(stderr, "Error: Data value out of range!\n");
> +			help();
> +		}
> +		break;
> +	case I2C_SMBUS_BLOCK_DATA:
> +	case I2C_SMBUS_I2C_BLOCK_DATA:
> +		for (len = 0; len + flags + 5 < argc; len++) {
> +			value = strtol(argv[flags + len + 4], &end, 0);
>  			if (*end || value < 0) {
>  				fprintf(stderr, "Error: Data value invalid!\n");
>  				help();
>  			}
> +			if (value > 0xff) {
> +				fprintf(stderr, "Error: Data value out of range!\n");
> +				help();
> +			}
> +			block[len] = value;
>  		}
> -	} else {
> -		size = I2C_SMBUS_BYTE;
>  		value = -1;
> -	}
> -
> -	if (argc > flags + 5) {
> -		switch (argv[flags+5][0]) {
> -		case 'b': size = I2C_SMBUS_BYTE_DATA; break;
> -		case 'w': size = I2C_SMBUS_WORD_DATA; break;
> -		default:
> -			fprintf(stderr, "Error: Invalid mode!\n");
> -			help();
> -		}
> -		pec = argv[flags+5][1] == 'p';
> +		break;

You could use a fall-through here.

> +	default:
> +		value = -1;
> +		break;
>  	}
>  
>  	if (maskp) {
> @@ -284,13 +302,6 @@ int main(int argc, char *argv[])
>  		}
>  	}
>  
> -	if ((size == I2C_SMBUS_BYTE_DATA && value > 0xff)
> -	 || (size == I2C_SMBUS_WORD_DATA && value > 0xffff)) {
> -		fprintf(stderr, "Error: Data value out of range!\n");
> -		help();
> -	}
> -
> -dofile:
>  	file = open_i2c_dev(i2cbus, filename, sizeof(filename), 0);
>  	if (file < 0

>  	 || check_funcs(file, size, pec)

Other than this minor comment (which you may not even agree with), this
patch looks OK to me now. Thanks.

-- 
Jean Delvare

      parent reply	other threads:[~2011-02-14  9:43 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-02-13 20:17 [PATCH v2 0/6] i2cset changes Guenter Roeck
     [not found] ` <1297628246-19367-1-git-send-email-guenter.roeck-IzeFyvvaP7pWk0Htik3J/w@public.gmane.org>
2011-02-13 20:17   ` [PATCH v2 1/6] i2cset: Remove deprecated method to provide the value mask Guenter Roeck
     [not found]     ` <1297628246-19367-2-git-send-email-guenter.roeck-IzeFyvvaP7pWk0Htik3J/w@public.gmane.org>
2011-02-14  8:48       ` Jean Delvare
     [not found]         ` <20110214094847.6607ebaa-R0o5gVi9kd7kN2dkZ6Wm7A@public.gmane.org>
2011-02-14 15:46           ` Guenter Roeck
     [not found]             ` <20110214154610.GA22833-IzeFyvvaP7pWk0Htik3J/w@public.gmane.org>
2011-02-14 16:17               ` Jean Delvare
     [not found]                 ` <20110214171735.06270f30-R0o5gVi9kd7kN2dkZ6Wm7A@public.gmane.org>
2011-02-14 17:09                   ` Guenter Roeck
2011-02-13 20:17   ` [PATCH v2 2/6] i2cset: Check number of arguments for block data writes Guenter Roeck
     [not found]     ` <1297628246-19367-3-git-send-email-guenter.roeck-IzeFyvvaP7pWk0Htik3J/w@public.gmane.org>
2011-02-14  8:52       ` Jean Delvare
2011-02-13 20:17   ` [PATCH v2 3/6] i2cset: Abort if value mask is set for block commands Guenter Roeck
     [not found]     ` <1297628246-19367-4-git-send-email-guenter.roeck-IzeFyvvaP7pWk0Htik3J/w@public.gmane.org>
2011-02-14  8:56       ` Jean Delvare
2011-02-13 20:17   ` [PATCH v2 4/6] i2cset: Ensure that there is no junk after the command/mode parameter Guenter Roeck
     [not found]     ` <1297628246-19367-5-git-send-email-guenter.roeck-IzeFyvvaP7pWk0Htik3J/w@public.gmane.org>
2011-02-14  8:59       ` Jean Delvare
2011-02-13 20:17   ` [PATCH v2 5/6] i2cset: Replace blanks at beginning of line with tabs Guenter Roeck
     [not found]     ` <1297628246-19367-6-git-send-email-guenter.roeck-IzeFyvvaP7pWk0Htik3J/w@public.gmane.org>
2011-02-14  9:00       ` Jean Delvare
2011-02-13 20:17   ` [PATCH v2 6/6] i2cset: Get command/mode before reading data Guenter Roeck
     [not found]     ` <1297628246-19367-7-git-send-email-guenter.roeck-IzeFyvvaP7pWk0Htik3J/w@public.gmane.org>
2011-02-14  9:43       ` Jean Delvare [this message]

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=20110214104341.3791fddb@endymion.delvare \
    --to=khali-puyad+kwke1g9huczpvpmw@public.gmane.org \
    --cc=guenter.roeck-IzeFyvvaP7pWk0Htik3J/w@public.gmane.org \
    --cc=linux-i2c-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    /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).