public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
* [U-Boot] [PATCH] lib: net_utils: make string_to_ip stricter
@ 2016-12-19 22:01 Chris Packham
  2016-12-26  5:23 ` Simon Glass
  0 siblings, 1 reply; 13+ messages in thread
From: Chris Packham @ 2016-12-19 22:01 UTC (permalink / raw)
  To: u-boot

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.0.0.0, which for the purposes of IP addresses can be
considered an error.

Signed-off-by: Chris Packham <judge.packham@gmail.com>
---
This was part of my long running IPv6 patchset (which I promise I'll get
back to someday). But I feel this stands on it's own merits.

 lib/net_utils.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/lib/net_utils.c b/lib/net_utils.c
index cfae84275241..f148b8a70a7d 100644
--- a/lib/net_utils.c
+++ b/lib/net_utils.c
@@ -24,11 +24,16 @@ struct in_addr string_to_ip(const char *s)
 
 	for (addr.s_addr = 0, i = 0; i < 4; ++i) {
 		ulong val = s ? simple_strtoul(s, &e, 10) : 0;
+		if (val > 255) {
+			addr.s_addr = 0;
+			return addr;
+		}
 		addr.s_addr <<= 8;
 		addr.s_addr |= (val & 0xFF);
-		if (s) {
-			s = (*e) ? e+1 : e;
-		}
+		if (*e == '.')
+			s = e + 1;
+		else
+			break;
 	}
 
 	addr.s_addr = htonl(addr.s_addr);
-- 
2.11.0.24.ge6920cf

^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2017-01-15 18:30 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-12-19 22:01 [U-Boot] [PATCH] lib: net_utils: make string_to_ip stricter Chris Packham
2016-12-26  5:23 ` Simon Glass
2016-12-26  8:56   ` Chris Packham
2017-01-04  0:36     ` [U-Boot] [PATCH v2 1/2] " Chris Packham
2017-01-04  0:36       ` [U-Boot] [PATCH v2 2/2] lib: net_utils: enforce '.' as octet separator in string_to_ip Chris Packham
2017-01-04 10:09         ` Wolfgang Denk
2017-01-05  8:17           ` Chris Packham
2017-01-08 18:45             ` Wolfgang Denk
2017-01-09  8:21               ` Chris Packham
2017-01-15 18:30         ` [U-Boot] [U-Boot, v2, " Tom Rini
2017-01-04 10:04       ` [U-Boot] [PATCH v2 1/2] lib: net_utils: make string_to_ip stricter Wolfgang Denk
2017-01-05  7:58         ` Chris Packham
2017-01-15 18:30       ` [U-Boot] [U-Boot, v2, " Tom Rini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox