diff mbox

[i2c-tools,1/2] i2ctransfer: replace broken "force" handling

Message ID 20170403173613.2238-1-wsa+renesas@sang-engineering.com
State Accepted
Headers show

Commit Message

Wolfram Sang April 3, 2017, 5:36 p.m. UTC
Probably because of a bad rebase, the old and bogus handling (v1) of "force"
slipped into the latest version of the patch (v3). Apply the better
version from v2 which does the error handling correct and makes the code
a lot easier to understand.

Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
---
 tools/i2ctransfer.c | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)

Comments

Wolfram Sang April 7, 2017, 12:13 p.m. UTC | #1
On Mon, Apr 03, 2017 at 07:36:12PM +0200, Wolfram Sang wrote:
> Probably because of a bad rebase, the old and bogus handling (v1) of "force"
> slipped into the latest version of the patch (v3). Apply the better
> version from v2 which does the error handling correct and makes the code
> a lot easier to understand.
> 
> Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>

Pushed out to i2c-tools/master.
diff mbox

Patch

diff --git a/tools/i2ctransfer.c b/tools/i2ctransfer.c
index 0f7ddd5..bc027c3 100644
--- a/tools/i2ctransfer.c
+++ b/tools/i2ctransfer.c
@@ -199,8 +199,6 @@  int main(int argc, char *argv[])
 
 			arg_ptr = end;
 			if (*arg_ptr) {
-				int whole_range_ok;
-
 				if (*arg_ptr++ != '@') {
 					fprintf(stderr, "Error: Unknown seperator after length\n");
 					goto err_out_with_arg;
@@ -211,16 +209,22 @@  int main(int argc, char *argv[])
 				 * the address here.
 				 */
 
-				address = parse_i2c_address(arg_ptr);
-				/* With 'force', allow whole address range */
-				whole_range_ok = force && address == -2;
-				if (address < 0 && !whole_range_ok)
-					goto err_out_with_arg;
-
-				/* Ensure address is not busy */
-				if (!force && set_slave_addr(file, address, 0))
-					goto err_out_with_arg;
-
+				if (!force) {
+					address = parse_i2c_address(arg_ptr);
+					if (address < 0)
+						goto err_out_with_arg;
+
+					/* Ensure address is not busy */
+					if (set_slave_addr(file, address, 0))
+						goto err_out_with_arg;
+				} else {
+					/* 'force' allows whole address range */
+					address = strtol(arg_ptr, &end, 0);
+					if (arg_ptr == end || *end || address > 0x7f) {
+						fprintf(stderr, "Error: Invalid chip address\n");
+						goto err_out_with_arg;
+					}
+				}
 			} else {
 				/* Reuse last address if possible */
 				if (address < 0) {