Skip to contentSkip to conversation

i2cset changes

PATCH In review Guenter Roeck 2 participants 8 messages Feb 14, 2011 – Feb 15, 2011 1 message not in the archive I2C subsystem host drivers

lore
Share & tools
lore
#0 lore raw

v3 cover i2cset changes

The following series of patches removes the obsolete means to specify the value mask, improves parameter error detection for the i2cset command, and cleans up the code a bit.

Note: Patch revision in headline was updated only if the individual patch is either new or was changed.

v2:
- Split single patch into multiple individual patches
- Included code review feedback

v3:
- For block commands, check for number of arguments only after checking if pec is supported.
- Report "Too many arguments" instead of "Bad number of arguments".
- Committed blank replacement patch separately
- Check valid range for data value mask (patch #6)

A message that is not in the archive (sent off-list or not yet received): 1297706773-26389-1-git-send-email-guenter.roeck-IzeFyvvaP7pWk0Htik3J/[email protected]

#2 lore raw

PATCH v2 1/6 i2cset: Remove deprecated method to provide the value mask

The old method to provide the value mask has long since been deprecated, remove it.

---
 CHANGES        |    1 +
 tools/i2cset.c |   12 ------------
 2 files changed, 1 insertions(+), 12 deletions(-)
CHANGES +1 -0
index 5aee418..999ff26 100644
@@ -4,6 +4,7 @@ i2c-tools CHANGES
 SVN
   i2c-dev.h: Make value arrays const for block write functions
   i2cset: Add support for SMBus and I2C block writes
+          Remove obsolete means to specify value mask
 
 3.0.3 (2010-12-12)
   Makefile: Let the environment set CC and CFLAGS
tools/i2cset.c +0 -12
index 6195633..e47c9d4 100644
@@ -265,18 +265,6 @@ int main(int argc, char *argv[])
 		pec = argv[flags+5][1] == 'p';
 	}
 
-	/* Old method to provide the value mask, deprecated and no longer
-	   documented but still supported for compatibility */
-	if (argc > flags + 6) {
-		if (maskp) {
-			fprintf(stderr, "Error: Data value mask provided twice!\n");
-			help();
-		}
-		fprintf(stderr, "Warning: Using deprecated way to set the data value mask!\n");
-		fprintf(stderr, "         Please switch to using -m.\n");
-		maskp = argv[flags+6];
-	}
-
 	if (maskp) {
 		vmask = strtol(maskp, &end, 0);
 		if (*end || vmask == 0) {
Signature · 1 line
1.7.0.4
#3 lore raw

PATCH v3 2/6 i2cset: Check number of arguments for block data writes

In reply to a message not in the archive
---
 tools/i2cset.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
tools/i2cset.c +5 -1
index e47c9d4..e835465 100644
@@ -223,7 +223,11 @@ int main(int argc, char *argv[])
 				fprintf(stderr, "Error: PEC not supported for I2C block writes!\n");
 				help();
 			}
-			for (len = 0; len < (int)sizeof(block) && len + flags + 5 < argc; len++) {
+			if (argc > (int)sizeof(block) + flags + 5) {
+				fprintf(stderr, "Error: Too many arguments!\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");
Signature · 1 line
1.7.0.4
#4 lore raw

PATCH v2 3/6 i2cset: Abort if value mask is set for block commands

In reply to a message not in the archive

Specifying the value mask is not supported for block commands, abort if it is specified anyway.

---
 tools/i2cset.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
tools/i2cset.c +4 -0
index e835465..7750c03 100644
@@ -223,6 +223,10 @@ int main(int argc, char *argv[])
 				fprintf(stderr, "Error: PEC not supported for I2C block writes!\n");
 				help();
 			}
+			if (maskp) {
+				fprintf(stderr, "Error: Mask not supported for block writes!\n");
+				help();
+			}
 			if (argc > (int)sizeof(block) + flags + 5) {
 				fprintf(stderr, "Error: Too many arguments!\n");
 				help();
Signature · 1 line
1.7.0.4
#5 lore raw

PATCH v3 4/6 i2cset: Ensure that there is no junk after the command/mode parameter

In reply to a message not in the archive

Since the value mask can no longer be set after the mode command, we can make sure that there is no junk after the command/mode parameter in the command line.

---
 CHANGES        |    1 +
 tools/i2cset.c |    3 +++
 2 files changed, 4 insertions(+), 0 deletions(-)
CHANGES +1 -0
index 999ff26..1ffe18f 100644
@@ -5,6 +5,7 @@ SVN
   i2c-dev.h: Make value arrays const for block write functions
   i2cset: Add support for SMBus and I2C block writes
           Remove obsolete means to specify value mask
+          More stringent parameter validation
 
 3.0.3 (2010-12-12)
   Makefile: Let the environment set CC and CFLAGS
tools/i2cset.c +3 -0
index 7750c03..a45a829 100644
@@ -240,6 +240,9 @@ int main(int argc, char *argv[])
 				block[len] = value;
 			}
 			goto dofile;
+		} else if (argc != flags + 6) {
+			fprintf(stderr, "Error: Too many arguments!\n");
+			help();
 		}
 	}
 
Signature · 1 line
1.7.0.4
#6 lore raw

PATCH v2 5/6 i2cset: Get command/mode before reading data

In reply to a message not in the archive

Get and validate the command/mode parameter for all commands before reading and evaluating actual data.

---
 tools/i2cset.c |   99 +++++++++++++++++++++++++++++++-------------------------
 1 files changed, 55 insertions(+), 44 deletions(-)
tools/i2cset.c +55 -44
index a45a829..ed6ada2 100644
@@ -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 (pec && size == I2C_SMBUS_I2C_BLOCK_DATA) {
 				fprintf(stderr, "Error: PEC not supported for I2C block writes!\n");
 				help();
@@ -231,49 +250,48 @@ int main(int argc, char *argv[])
 				fprintf(stderr, "Error: Too many arguments!\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: Too many 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;
+	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)
Signature · 1 line
1.7.0.4
#7 lore raw

PATCH v3 6/6 i2cset: Check range for data value mask

In reply to a message not in the archive
---
 tools/i2cset.c |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
tools/i2cset.c +5 -0
index ed6ada2..b512081 100644
@@ -300,6 +300,11 @@ int main(int argc, char *argv[])
 			fprintf(stderr, "Error: Data value mask invalid!\n");
 			help();
 		}
+		if (((size == I2C_SMBUS_BYTE || size == I2C_SMBUS_BYTE_DATA)
+		     && vmask > 0xff) || vmask > 0xffff) {
+			fprintf(stderr, "Error: Data value mask out of range!\n");
+			help();
+		}
 	}
 
 	file = open_i2c_dev(i2cbus, filename, sizeof(filename), 0);
Signature · 1 line
1.7.0.4
#8 lore raw
In reply to a message not in the archive
Quoted 10 lines · Guenter Roeck

The following series of patches removes the obsolete means to specify the value mask, improves parameter error detection for the i2cset command, and cleans up the code a bit.

Note: Patch revision in headline was updated only if the individual patch is either new or was changed.

v2:
- Split single patch into multiple individual patches
- Included code review feedback

Quoted 6 lines

v3:
- For block commands, check for number of arguments only after checking if pec is supported.
- Report "Too many arguments" instead of "Bad number of arguments".
- Committed blank replacement patch separately
- Check valid range for data value mask (patch #6)

This version looks pretty good, please commit. Thanks!

Signature · 1 line
Jean Delvare