Skip to contentSkip to conversation

iio: Do not accept multiple '.' in fixed point numbers

PATCH In review Lars-Peter Clausen 2 participants 6 messages Oct 22, 2012 – Nov 2, 2012 IIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 iio: Do not accept multiple '.' in fixed point numbers

Currently when parsing a fix-point number we silently skip any additional '.' found in the string. E.g. '1.2.3.4' gets parsed as '1.234'. This patch disallows this and returns an error if more than one '.' is encountered.

---
 drivers/iio/industrialio-core.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/industrialio-core.c +1 -1
index 6eb24db..a2e9953 100644
@@ -449,7 +449,7 @@ static ssize_t iio_write_channel_info(struct device *dev,
 				break;
 			else
 				return -EINVAL;
-		} else if (*buf == '.') {
+		} else if (*buf == '.' && integer_part) {
 			integer_part = false;
 		} else {
 			return -EINVAL;
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH 2/3 iio: Reject trailing garbage when parsing fixed point numbers

When parsing a fixed point number IIO stops parsing the string once it has reached the last requested decimal place. This means that the remainder of the string is silently accepted regardless, of whether it is part of a valid number or not. This patch modifies the code to scan the whole string and only accept valid numbers. Since fract_mult is 0 after the last decimal place any digit that may follows won't affect the result.

---
 drivers/iio/industrialio-core.c |    2 --
 1 file changed, 2 deletions(-)
drivers/iio/industrialio-core.c +0 -2
index a2e9953..fe593bf 100644
@@ -440,8 +440,6 @@ static ssize_t iio_write_channel_info(struct device *dev,
 				integer = integer*10 + *buf - '0';
 			else {
 				fract += fract_mult*(*buf - '0');
-				if (fract_mult == 1)
-					break;
 				fract_mult /= 10;
 			}
 		} else if (*buf == '\n') {
Signature · 1 line
1.7.10.4
#3 lore raw

PATCH 3/3 iio: Accept a leading '+' sign when parsing fixed point numbers

In reply to Lars-Peter Clausen

If we encounter a leading '+' sign just skip over it.

---
 drivers/iio/industrialio-core.c |    2 ++
 1 file changed, 2 insertions(+)
drivers/iio/industrialio-core.c +2 -0
index fe593bf..5f8ff0d 100644
@@ -432,6 +432,8 @@ static ssize_t iio_write_channel_info(struct device *dev,
 	if (buf[0] == '-') {
 		negative = true;
 		buf++;
+	} else if (buf[0] == '+') {
+		buf++;
 	}
 
 	while (*buf) {
Signature · 1 line
1.7.10.4
maintainer
#5 lore raw
In reply to Lars-Peter Clausen
Quoted 5 lines · Lars-Peter Clausen

Currently when parsing a fix-point number we silently skip any additional '.' found in the string. E.g. '1.2.3.4' gets parsed as '1.234'. This patch disallows this and returns an error if more than one '.' is encountered.

Added to togreg branch of iio.git

Quoted 18 lines
---
 drivers/iio/industrialio-core.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/industrialio-core.c +1 -1
index 6eb24db..a2e9953 100644
@@ -449,7 +449,7 @@ static ssize_t iio_write_channel_info(struct device *dev,
 				break;
 			else
 				return -EINVAL;
-		} else if (*buf == '.') {
+		} else if (*buf == '.' && integer_part) {
 			integer_part = false;
 		} else {
 			return -EINVAL;
maintainer
#2 lore raw

Re: [PATCH 2/3] iio: Reject trailing garbage when parsing fixed point numbers

In reply to Lars-Peter Clausen
Quoted 8 lines · Lars-Peter Clausen

When parsing a fixed point number IIO stops parsing the string once it has reached the last requested decimal place. This means that the remainder of the string is silently accepted regardless, of whether it is part of a valid number or not. This patch modifies the code to scan the whole string and only accept valid numbers. Since fract_mult is 0 after the last decimal place any digit that may follows won't affect the result.

Nice little trick.

Added to togreg branch of iio.git

Quoted 18 lines
---
 drivers/iio/industrialio-core.c |    2 --
 1 file changed, 2 deletions(-)
drivers/iio/industrialio-core.c +0 -2
index a2e9953..fe593bf 100644
@@ -440,8 +440,6 @@ static ssize_t iio_write_channel_info(struct device *dev,
 				integer = integer*10 + *buf - '0';
 			else {
 				fract += fract_mult*(*buf - '0');
-				if (fract_mult == 1)
-					break;
 				fract_mult /= 10;
 			}
 		} else if (*buf == '\n') {
maintainer
#4 lore raw

Re: [PATCH 3/3] iio: Accept a leading '+' sign when parsing fixed point numbers

In reply to Lars-Peter Clausen
Quoted 3 lines · Lars-Peter Clausen

If we encounter a leading '+' sign just skip over it.

Can't say I care either way on this, but it's helpful to you why not...

Added to togreg branch of iio.git

Quoted 18 lines
---
 drivers/iio/industrialio-core.c |    2 ++
 1 file changed, 2 insertions(+)
drivers/iio/industrialio-core.c +2 -0
index fe593bf..5f8ff0d 100644
@@ -432,6 +432,8 @@ static ssize_t iio_write_channel_info(struct device *dev,
 	if (buf[0] == '-') {
 		negative = true;
 		buf++;
+	} else if (buf[0] == '+') {
+		buf++;
 	}
 
 	while (*buf) {