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

Submitted by Chris Packham on Jan. 25, 2013, 12:56 a.m.

Details

Message ID 1359075418-9031-4-git-send-email-judge.packham@gmail.com
State RFC
Delegated to: Joe Hershberger
Headers show

Commit Message

Chris Packham Jan. 25, 2013, 12:56 a.m.
From: Chris Packham <chris.packham@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@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(-)

Patch hide | download patch | download mbox

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));