From patchwork Fri Jan 25 00:56:54 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Packham X-Patchwork-Id: 215515 X-Patchwork-Delegate: joe.hershberger@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 8284E2C007E for ; Fri, 25 Jan 2013 11:58:59 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id F3EC14A13E; Fri, 25 Jan 2013 01:58:53 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 5xTsGeclAhip; Fri, 25 Jan 2013 01:58:53 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id A449F4A140; Fri, 25 Jan 2013 01:58:28 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 489B54A126 for ; Fri, 25 Jan 2013 01:58:23 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id LzNFtDz7aA5S for ; Fri, 25 Jan 2013 01:58:22 +0100 (CET) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from gate.alliedtelesyn.co.nz (gate.alliedtelesyn.co.nz [202.49.72.33]) by theia.denx.de (Postfix) with SMTP id 571874A11A for ; Fri, 25 Jan 2013 01:58:17 +0100 (CET) Received: (qmail 7242 invoked from network); 25 Jan 2013 00:58:16 -0000 Received: from mmarshal3.atlnz.lc (10.32.18.43) by gate-int.alliedtelesyn.co.nz with SMTP; 25 Jan 2013 00:58:16 -0000 Received: from alliedtelesyn.co.nz (Not Verified[10.32.16.32]) by mmarshal3.atlnz.lc with MailMarshal (v7, 1, 0, 4874) id ; Fri, 25 Jan 2013 13:57:49 +1300 Received: from MAIL/SpoolDir by alliedtelesyn.co.nz (Mercury 1.48); 25 Jan 13 13:02:17 +1200 Received: from SpoolDir by MAIL (Mercury 1.48); 25 Jan 13 13:01:44 +1200 Received: from chrisp-dl.ws.atlnz.lc (10.33.22.46) by alliedtelesyn.co.nz (Mercury 1.48) with ESMTP; 25 Jan 13 13:01:23 +1200 Received: by chrisp-dl.ws.atlnz.lc (Postfix, from userid 1030) id 17423228FD; Fri, 25 Jan 2013 13:57:22 +1300 (NZDT) From: Chris Packham To: u-boot@lists.denx.de, Joe Hershberger Date: Fri, 25 Jan 2013 13:56:54 +1300 Message-Id: <1359075418-9031-4-git-send-email-judge.packham@gmail.com> X-Mailer: git-send-email 1.7.12.rc2.16.g034161a In-Reply-To: <1359075418-9031-1-git-send-email-judge.packham@gmail.com> References: <1359075418-9031-1-git-send-email-judge.packham@gmail.com> Cc: Chris Packham Subject: [U-Boot] [RFC PATCH v3 3/7] lib/net_utils.c: make string_to_ip stricter X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.11 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de From: Chris Packham 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 --- 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));