From patchwork Mon Sep 11 20:53:08 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rob Clark X-Patchwork-Id: 812610 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="cmk3GzM9"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 3xrgBF5SD7z9s8J for ; Tue, 12 Sep 2017 06:53:24 +1000 (AEST) Received: by lists.denx.de (Postfix, from userid 105) id 3B0F1C21FC9; Mon, 11 Sep 2017 20:53:18 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 67FB2C21E64; Mon, 11 Sep 2017 20:53:16 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 71718C21E4B; Mon, 11 Sep 2017 20:53:15 +0000 (UTC) Received: from mail-qk0-f195.google.com (mail-qk0-f195.google.com [209.85.220.195]) by lists.denx.de (Postfix) with ESMTPS id EC735C21E01 for ; Mon, 11 Sep 2017 20:53:14 +0000 (UTC) Received: by mail-qk0-f195.google.com with SMTP id r66so5965761qke.4 for ; Mon, 11 Sep 2017 13:53:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=Zs5Tmy0VZCKC9PL3Wuk2u7OMVMm3XCtbW13XH6liFNw=; b=cmk3GzM95+4B2v2SmKexTUtbbGFR2M7S/XFFtMYN2E7oO1jPIhAnF/oCpRz/G7y5EN v2BoXXMmDMp5ipMbChRx0lVe07p/Jinfx9OI3fNAVHIs3j2ZTEcPkUmtH5srLlQOaXLB bZn7Dvl1T/NGOKzZvVMes0d0Zv4ztNyqZx94umBTPR2iRbfTd90G0hiX01UF1vLpYSBP +BvK67SZiMlPBSqzjJ356tfMtYxak4U7lFcyny6x/QskVpor3eooRCsDenLPYuMw/kMC oPXxY9E9sbeX+mYM213RcSqvWHyw76XlEH4wgyr9hLZHAGM5rMNDSf54/pM/xUfm2LF7 d6ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=Zs5Tmy0VZCKC9PL3Wuk2u7OMVMm3XCtbW13XH6liFNw=; b=kyotO6BDowPp3G4yuPbIGB/0HazxMyNGGEIQkOsV9gkcOR4fTnC04xdLgE1YuLJ6dq 5OkuQ/TAxxv/Iv0H0irUjChgUCdJfN/XxLtzq0O3gYjy0Iohv3uTFoMijBJjOUox8E2G /8qhvv5DbG+kA+cmHdmZAxnrpz0r2c+6mfgO8FuPJ76D1kQMQhnET8AmAev6RbspmC3m 9bBlu+MSpaU5lunHg6nVjcjshzOvihAOPXyBa8iSeQZGCtVpEQRvOALcHptJBDJz3h2Y 8KeSkBq3ENlBAlY+Mi1oG/bBpHjtG4Z1q1Ulxpn4FeUZ3UCEfQyJqCl+1VyTTNGb649h qodA== X-Gm-Message-State: AHPjjUiKNQ8SgJxEVuHiS4PUSMhODolVAQ7DaTOePfiUnq7M4m2GwpN8 L2oILE5N7pZwaC2NxnY= X-Google-Smtp-Source: AOwi7QA+aM1je0ERJobi1elholt7vuGHO1QivcjSYe8noWp0dstpgmmmtE5iij7ms/MI+urKnH44cg== X-Received: by 10.55.200.210 with SMTP id t79mr16815711qkl.299.1505163193363; Mon, 11 Sep 2017 13:53:13 -0700 (PDT) Received: from localhost ([144.121.20.162]) by smtp.gmail.com with ESMTPSA id b126sm6610788qka.54.2017.09.11.13.53.12 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 11 Sep 2017 13:53:12 -0700 (PDT) From: Rob Clark To: U-Boot Mailing List Date: Mon, 11 Sep 2017 16:53:08 -0400 Message-Id: <20170911205310.19185-1-robdclark@gmail.com> X-Mailer: git-send-email 2.13.5 Subject: [U-Boot] [PATCH] lib: strto: fix incorrect handling of specified base X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" The strto functions should honor the specified base (if non-zero) rather than permitting a hex or octal string when the user wanted (for example) base 10. This has been fixed somewhere along the way in the upstream linux kernel src tree, at some point after these was copied in to u-boot. And also in a way that duplicates less code. So port _parse_integer_fixup_radix() to u-boot. Signed-off-by: Rob Clark Reviewed-by: Simon Glass --- lib/strto.c | 45 +++++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/lib/strto.c b/lib/strto.c index e93a4f5491..7f6076909a 100644 --- a/lib/strto.c +++ b/lib/strto.c @@ -13,25 +13,30 @@ #include #include +/* from lib/kstrtox.c */ +static const char *_parse_integer_fixup_radix(const char *s, unsigned int *base) +{ + if (*base == 0) { + if (s[0] == '0') { + if (tolower(s[1]) == 'x' && isxdigit(s[2])) + *base = 16; + else + *base = 8; + } else + *base = 10; + } + if (*base == 16 && s[0] == '0' && tolower(s[1]) == 'x') + s += 2; + return s; +} + unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) { unsigned long result = 0; unsigned long value; - if (*cp == '0') { - cp++; - if ((*cp == 'x') && isxdigit(cp[1])) { - base = 16; - cp++; - } - - if (!base) - base = 8; - } - - if (!base) - base = 10; + cp = _parse_integer_fixup_radix(cp, &base); while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp) ? toupper(*cp) : *cp)-'A'+10) < base) { @@ -128,19 +133,7 @@ unsigned long long simple_strtoull(const char *cp, char **endp, { unsigned long long result = 0, value; - if (*cp == '0') { - cp++; - if ((*cp == 'x') && isxdigit(cp[1])) { - base = 16; - cp++; - } - - if (!base) - base = 8; - } - - if (!base) - base = 10; + cp = _parse_integer_fixup_radix(cp, &base); while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp - '0' : (islower(*cp) ? toupper(*cp) : *cp) - 'A' + 10) < base) {