[U-Boot] [RFC PATCH v3 3/7] lib/net_utils.c: make string_to_ip stricter

Chris Packham judge.packham at gmail.com
Fri Jan 25 01:56:54 CET 2013


From: Chris Packham <chris.packham at alliedtelesis.co.nz>

Previously values greater than 255 were implicitly truncated. Add some
stricter checking to reject addresses with components >255.

With the input "1234192.168.1.1" the old behaviour would truncate the
address to 192.168.1.1. New behaviour rejects the string outright and
returns 0, which for the purposes of IP addresses can be considered an
error.

Signed-off-by: Chris Packham <chris.packham at alliedtelesis.co.nz>

---
Changes in v3:
 -Fix a what should have been a glaringly obvious omission that cause
  all addresses to be interpreted as 0. Made even stricter so
  that v6 addresses that start with numbers aren't accepted.

Changes in v2: None

 lib/net_utils.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/lib/net_utils.c b/lib/net_utils.c
index b425a68..afe0c16 100644
--- a/lib/net_utils.c
+++ b/lib/net_utils.c
@@ -38,12 +38,17 @@ IPaddr_t string_to_ip(const char *s)
 		return(0);
 
 	for (addr=0, i=0; i<4; ++i) {
-		ulong val = s ? simple_strtoul(s, &e, 10) : 0;
+		ulong val = simple_strtoul(s, &e, 10);
+		if (val > 255)
+			return 0;
 		addr <<= 8;
-		addr |= (val & 0xFF);
-		if (s) {
-			s = (*e) ? e+1 : e;
-		}
+		addr |= val;
+		if (*e == '.')
+			s = e+1;
+		else if (*e == '\0')
+			break;
+		else
+			return 0;
 	}
 
 	return (htonl(addr));
-- 
1.7.12.rc2.16.g034161a



More information about the U-Boot mailing list