From patchwork Sat Mar 20 13:37:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 405443 Delivered-To: patch@linaro.org Received: by 2002:a02:8562:0:0:0:0:0 with SMTP id g89csp2192724jai; Sat, 20 Mar 2021 06:49:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzoPAySwGfzto4B9+FU1jnexwm7QIqOmkVrZRg7TaVoA4lj3OUz6LprXQL8KozqONtxXhb1 X-Received: by 2002:a25:c2c4:: with SMTP id s187mr13280344ybf.292.1616248187671; Sat, 20 Mar 2021 06:49:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616248187; cv=none; d=google.com; s=arc-20160816; b=DedUSXmXX14A+caUHXOI9pNAVR5C2ZOnLx+khpxxqaCleDJBSyVd5zNMF8i0+AqGIK FCQq8LyFIdJYJ/3p6H2LCjpyoP3YMXcflLQ2Vhaxbktv8JtkG52TgGuDvIhyZG49zeD3 oNNRqTlrr+VtlzRKZl7ZPg4wjTE2xwYnqXz1xOo0tFvT7rs5essenbLiAo9zfCo1ev9q w1trlWmllLGATNoyewSHUbIVNNDoy+U7neVhQuulHWiI70HenLA+/CaM5pBf5mek5jIb CpeLAxXQuppyOYQZSFg83VGf83T/l1U/zetC+HG/r3zKyUVsURpW8z8kxmPCq0yVmwfU lI3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=uL96Uis9yF2paA0n5GhgWk7dCyVBjyOpNhr7RDd2Ljw=; b=M34GweOBymjhGyeUfizKKyA1CS+cvgUpYmkYYxI+Ed3TW4C8Ygv95dpQa3gCj2jmE/ gQpSJOFK2f6vj2nHC+C1h97GJjKXc/R8Kot47jauPcmEsLSKw+qH6GXUqoabd3wXtq39 WMROmDMj1Eskwg5tZXT3Vd8R3rghYGuvj6BQYjEEuOFHmpc5K4DG3UJnHdeXSmn6FFoW LUWIFa9H+W0r1ffADsa7MoSrK1JT8O3zvqfZnRbFJJJE+7YII/OntvgbWPYrAnADLBAm SfuTiGsKow0J/N0/ZPG1qN3R36cDde9yWdrQw04Gup0aY9p9yswMd+KXZnxlDbF+lAWD +tdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=vNgvsdRZ; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id x17si8148011ybj.139.2021.03.20.06.49.47 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sat, 20 Mar 2021 06:49:47 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=vNgvsdRZ; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:47944 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lNbz8-0007Ye-Qs for patch@linaro.org; Sat, 20 Mar 2021 09:49:46 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:60326) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lNbuT-000323-Ab for qemu-devel@nongnu.org; Sat, 20 Mar 2021 09:44:57 -0400 Received: from mail-ej1-x62f.google.com ([2a00:1450:4864:20::62f]:43797) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lNbuP-0007yg-44 for qemu-devel@nongnu.org; Sat, 20 Mar 2021 09:44:57 -0400 Received: by mail-ej1-x62f.google.com with SMTP id l4so14058587ejc.10 for ; Sat, 20 Mar 2021 06:44:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=uL96Uis9yF2paA0n5GhgWk7dCyVBjyOpNhr7RDd2Ljw=; b=vNgvsdRZRpu65j5WOpju5KTzkw6uCxbQAsUj1/FhmdxO4g61GvNPg5piF4pY3DBwT7 SA+3RxhHCzSlbBujrC9ShlcRsPPfvfeJeX0vmKbBZlyDtT1GgtwFp8xBQvk+/WkM9r3x wpygDw4v56E9JCLWJAJHTfDGzudcIV1+SbJOEqrFc0kSgDy7si/bLCRO+2wNoKmsqaPq w88gOJ0ewE9beDLp81BlwjrN+OwwfuVjuN4GcLeghRoyjHoVP0MGwHrhXr4G/STP9GW0 msM2Y6VroqBsxKkLYZCUBQzBIL+nTCM+4kQCR/vu7EajU1bBEJ//gXPLmfitd6LnK3Sm S5IQ== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=uL96Uis9yF2paA0n5GhgWk7dCyVBjyOpNhr7RDd2Ljw=; b=NeL/3wColLIYAzI5Vq63DNatCLGe+jhVG0RI246baf0lLaGW76Kwfeao1tHyiXLb8I Du895YgayCT2WemambpjanIYsuEepXz/uNyOCAD2YbWuuRGYebUA/ZFUZKTNu/chLo4n yvAnnfzXqcBgIEf6AcQTKighIp5nloneeE6c/DaU0bg7Dl+WcR6xoiOcHDke9BO1aKzK jCSPnPQW1FrUaU6IPKL4TWTt/hRO6+6Z1s6V5rCqoa5gBhTJg1Xypix33u/wNUhhm0YZ kZtodlbapwGTaA0HbSxve1OArCTcicDi2/3trLq+Ha40YarRES/pa8HcQdYEfil7MAKI 3r1A== X-Gm-Message-State: AOAM531RNBce9WqhZy3T7VJMKJKHeQKNoZXw2981itSe4PZ37o7DmrRJ uWoauZdhGM9Hh2iapX5g0d5jAw== X-Received: by 2002:a17:906:9152:: with SMTP id y18mr9872282ejw.19.1616247891424; Sat, 20 Mar 2021 06:44:51 -0700 (PDT) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id g21sm5335707ejd.6.2021.03.20.06.44.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Mar 2021 06:44:49 -0700 (PDT) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 1168B1FF9D; Sat, 20 Mar 2021 13:37:08 +0000 (GMT) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Subject: [PATCH v1 14/14] utils: Work around mingw strto*l bug with 0x Date: Sat, 20 Mar 2021 13:37:06 +0000 Message-Id: <20210320133706.21475-15-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210320133706.21475-1-alex.bennee@linaro.org> References: <20210320133706.21475-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::62f; envelope-from=alex.bennee@linaro.org; helo=mail-ej1-x62f.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?utf-8?q?Alex_Benn=C3=A9e?= , Thomas Huth Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: Eric Blake Mingw recognizes that "0x" has value 0 without setting errno, but fails to advance endptr to the trailing garbage 'x'. This in turn showed up in our recent testsuite additions for qemu_strtosz (commit 1657ba44b4 utils: Enhance testsuite for do_strtosz()); adjust our remaining tests to show that we now work around this windows bug. This patch intentionally fails check-syntax for use of strtol. Signed-off-by: Eric Blake Reviewed-by: Thomas Huth Message-Id: <20210317143325.2165821-3-eblake@redhat.com> Signed-off-by: Alex Bennée --- tests/unit/test-cutils.c | 54 ++++++++++++++++++++++++++++++++++++++++ util/cutils.c | 29 +++++++++++++++------ 2 files changed, 75 insertions(+), 8 deletions(-) -- 2.20.1 diff --git a/tests/unit/test-cutils.c b/tests/unit/test-cutils.c index 5908de4fd0..98671f1ac3 100644 --- a/tests/unit/test-cutils.c +++ b/tests/unit/test-cutils.c @@ -378,6 +378,15 @@ static void test_qemu_strtoi_hex(void) g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, 0x123); g_assert(endptr == str + strlen(str)); + + str = "0x"; + res = 999; + endptr = &f; + err = qemu_strtoi(str, &endptr, 16, &res); + + g_assert_cmpint(err, ==, 0); + g_assert_cmpint(res, ==, 0); + g_assert(endptr == str + 1); } static void test_qemu_strtoi_max(void) @@ -669,6 +678,15 @@ static void test_qemu_strtoui_hex(void) g_assert_cmpint(err, ==, 0); g_assert_cmphex(res, ==, 0x123); g_assert(endptr == str + strlen(str)); + + str = "0x"; + res = 999; + endptr = &f; + err = qemu_strtoui(str, &endptr, 16, &res); + + g_assert_cmpint(err, ==, 0); + g_assert_cmphex(res, ==, 0); + g_assert(endptr == str + 1); } static void test_qemu_strtoui_max(void) @@ -955,6 +973,15 @@ static void test_qemu_strtol_hex(void) g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, 0x123); g_assert(endptr == str + strlen(str)); + + str = "0x"; + res = 999; + endptr = &f; + err = qemu_strtol(str, &endptr, 16, &res); + + g_assert_cmpint(err, ==, 0); + g_assert_cmpint(res, ==, 0); + g_assert(endptr == str + 1); } static void test_qemu_strtol_max(void) @@ -1244,6 +1271,15 @@ static void test_qemu_strtoul_hex(void) g_assert_cmpint(err, ==, 0); g_assert_cmphex(res, ==, 0x123); g_assert(endptr == str + strlen(str)); + + str = "0x"; + res = 999; + endptr = &f; + err = qemu_strtoul(str, &endptr, 16, &res); + + g_assert_cmpint(err, ==, 0); + g_assert_cmphex(res, ==, 0); + g_assert(endptr == str + 1); } static void test_qemu_strtoul_max(void) @@ -1528,6 +1564,15 @@ static void test_qemu_strtoi64_hex(void) g_assert_cmpint(err, ==, 0); g_assert_cmpint(res, ==, 0x123); g_assert(endptr == str + strlen(str)); + + str = "0x"; + endptr = &f; + res = 999; + err = qemu_strtoi64(str, &endptr, 16, &res); + + g_assert_cmpint(err, ==, 0); + g_assert_cmpint(res, ==, 0); + g_assert(endptr == str + 1); } static void test_qemu_strtoi64_max(void) @@ -1815,6 +1860,15 @@ static void test_qemu_strtou64_hex(void) g_assert_cmpint(err, ==, 0); g_assert_cmphex(res, ==, 0x123); g_assert(endptr == str + strlen(str)); + + str = "0x"; + endptr = &f; + res = 999; + err = qemu_strtou64(str, &endptr, 16, &res); + + g_assert_cmpint(err, ==, 0); + g_assert_cmphex(res, ==, 0); + g_assert(endptr == str + 1); } static void test_qemu_strtou64_max(void) diff --git a/util/cutils.c b/util/cutils.c index b425ed6570..ee908486da 100644 --- a/util/cutils.c +++ b/util/cutils.c @@ -396,9 +396,22 @@ int qemu_strtosz_metric(const char *nptr, const char **end, uint64_t *result) * Helper function for error checking after strtol() and the like */ static int check_strtox_error(const char *nptr, char *ep, - const char **endptr, int libc_errno) + const char **endptr, bool check_zero, + int libc_errno) { assert(ep >= nptr); + + /* Windows has a bug in that it fails to parse 0 from "0x" in base 16 */ + if (check_zero && ep == nptr && libc_errno == 0) { + char *tmp; + + errno = 0; + if (strtol(nptr, &tmp, 10) == 0 && errno == 0 && + (*tmp == 'x' || *tmp == 'X')) { + ep = tmp; + } + } + if (endptr) { *endptr = ep; } @@ -465,7 +478,7 @@ int qemu_strtoi(const char *nptr, const char **endptr, int base, } else { *result = lresult; } - return check_strtox_error(nptr, ep, endptr, errno); + return check_strtox_error(nptr, ep, endptr, lresult == 0, errno); } /** @@ -524,7 +537,7 @@ int qemu_strtoui(const char *nptr, const char **endptr, int base, *result = lresult; } } - return check_strtox_error(nptr, ep, endptr, errno); + return check_strtox_error(nptr, ep, endptr, lresult == 0, errno); } /** @@ -566,7 +579,7 @@ int qemu_strtol(const char *nptr, const char **endptr, int base, errno = 0; *result = strtol(nptr, &ep, base); - return check_strtox_error(nptr, ep, endptr, errno); + return check_strtox_error(nptr, ep, endptr, *result == 0, errno); } /** @@ -613,7 +626,7 @@ int qemu_strtoul(const char *nptr, const char **endptr, int base, if (errno == ERANGE) { *result = -1; } - return check_strtox_error(nptr, ep, endptr, errno); + return check_strtox_error(nptr, ep, endptr, *result == 0, errno); } /** @@ -639,7 +652,7 @@ int qemu_strtoi64(const char *nptr, const char **endptr, int base, QEMU_BUILD_BUG_ON(sizeof(int64_t) != sizeof(long long)); errno = 0; *result = strtoll(nptr, &ep, base); - return check_strtox_error(nptr, ep, endptr, errno); + return check_strtox_error(nptr, ep, endptr, *result == 0, errno); } /** @@ -668,7 +681,7 @@ int qemu_strtou64(const char *nptr, const char **endptr, int base, if (errno == ERANGE) { *result = -1; } - return check_strtox_error(nptr, ep, endptr, errno); + return check_strtox_error(nptr, ep, endptr, *result == 0, errno); } /** @@ -708,7 +721,7 @@ int qemu_strtod(const char *nptr, const char **endptr, double *result) errno = 0; *result = strtod(nptr, &ep); - return check_strtox_error(nptr, ep, endptr, errno); + return check_strtox_error(nptr, ep, endptr, false, errno); } /**