From patchwork Thu Dec 1 02:51:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhao Gongyi X-Patchwork-Id: 1710832 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4NN10P6hTFz23nS for ; Thu, 1 Dec 2022 13:55:31 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 01AF73CC559 for ; Thu, 1 Dec 2022 03:55:28 +0100 (CET) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-2.smtp.seeweb.it (in-2.smtp.seeweb.it [IPv6:2001:4b78:1:20::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384)) (No client certificate requested) by picard.linux.it (Postfix) with ESMTPS id D611F3CA9B6 for ; Thu, 1 Dec 2022 03:55:25 +0100 (CET) Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-2.smtp.seeweb.it (Postfix) with ESMTPS id 094CE600A79 for ; Thu, 1 Dec 2022 03:55:23 +0100 (CET) Received: from canpemm500005.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4NN0zL01gfzmWLB for ; Thu, 1 Dec 2022 10:54:37 +0800 (CST) Received: from ubuntu1804.huawei.com (10.67.174.63) by canpemm500005.china.huawei.com (7.192.104.229) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Thu, 1 Dec 2022 10:54:45 +0800 To: Date: Thu, 1 Dec 2022 10:51:40 +0800 Message-ID: <20221201025141.71227-2-zhaogongyi@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20221201025141.71227-1-zhaogongyi@huawei.com> References: <20221201025141.71227-1-zhaogongyi@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.174.63] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To canpemm500005.china.huawei.com (7.192.104.229) X-CFilter-Loop: Reflected X-Virus-Scanned: clamav-milter 0.102.4 at in-2.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.0 required=7.0 tests=SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on in-2.smtp.seeweb.it Subject: [LTP] [PATCH v2 1/2] lib/safe_macros: Add SAFE_STRTOF X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Zhao Gongyi via ltp From: Zhao Gongyi Reply-To: Zhao Gongyi Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Add a new macro SAFE_STRTOF, which is a safe mode of strtof(). Signed-off-by: Zhao Gongyi --- include/safe_macros_fn.h | 3 +++ include/tst_safe_macros.h | 3 +++ lib/safe_macros.c | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+) -- 2.17.1 diff --git a/include/safe_macros_fn.h b/include/safe_macros_fn.h index 114d8fd43..d256091b7 100644 --- a/include/safe_macros_fn.h +++ b/include/safe_macros_fn.h @@ -133,6 +133,9 @@ unsigned long safe_strtoul(const char *file, const int lineno, void (cleanup_fn)(void), char *str, unsigned long min, unsigned long max); +float safe_strtof(const char *file, const int lineno, + void (cleanup_fn)(void), char *str, float min, float max); + long safe_sysconf(const char *file, const int lineno, void (cleanup_fn)(void), int name); diff --git a/include/tst_safe_macros.h b/include/tst_safe_macros.h index ab00dd14a..0cf3d7878 100644 --- a/include/tst_safe_macros.h +++ b/include/tst_safe_macros.h @@ -186,6 +186,9 @@ int safe_getgroups(const char *file, const int lineno, int size, gid_t list[]); #define SAFE_STRTOUL(str, min, max) \ safe_strtoul(__FILE__, __LINE__, NULL, (str), (min), (max)) +#define SAFE_STRTOF(str, min, max) \ + safe_strtof(__FILE__, __LINE__, NULL, (str), (min), (max)) + #define SAFE_SYSCONF(name) \ safe_sysconf(__FILE__, __LINE__, NULL, name) diff --git a/lib/safe_macros.c b/lib/safe_macros.c index d8816631f..d00336748 100644 --- a/lib/safe_macros.c +++ b/lib/safe_macros.c @@ -21,6 +21,7 @@ #include #include #include +#include #include "test.h" #include "safe_macros.h" @@ -629,6 +630,39 @@ unsigned long safe_strtoul(const char *file, const int lineno, return rval; } +float safe_strtof(const char *file, const int lineno, + void (cleanup_fn) (void), char *str, + float min, float max) +{ + float rval; + char *endptr; + + errno = 0; + rval = strtof(str, &endptr); + + if ((errno == ERANGE) || (rval == 0) + || (rval == HUGE_VAL) || (rval == -HUGE_VAL)) { + tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn, + "strtof(%s) failed", str); + return rval; + } + + if (rval > max || rval < min) { + tst_brkm_(file, lineno, TBROK, cleanup_fn, + "strtof(%s): %f is out of range %f - %f", + str, rval, min, max); + return 0; + } + + if (endptr == str || (*endptr != '\0' && *endptr != '\n')) { + tst_brkm_(file, lineno, TBROK, cleanup_fn, + "Invalid value: '%s'", str); + return 0; + } + + return rval; +} + long safe_sysconf(const char *file, const int lineno, void (cleanup_fn) (void), int name) { From patchwork Thu Dec 1 02:51:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhao Gongyi X-Patchwork-Id: 1710834 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4NN10l31Tkz23nS for ; Thu, 1 Dec 2022 13:55:51 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 85BEF3CC55D for ; Thu, 1 Dec 2022 03:55:49 +0100 (CET) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-5.smtp.seeweb.it (in-5.smtp.seeweb.it [217.194.8.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by picard.linux.it (Postfix) with ESMTPS id 7D7CE3CC574 for ; Thu, 1 Dec 2022 03:55:25 +0100 (CET) Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-5.smtp.seeweb.it (Postfix) with ESMTPS id 0F7EC600BD4 for ; Thu, 1 Dec 2022 03:55:23 +0100 (CET) Received: from canpemm500005.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4NN0zL0H7SzmWM9 for ; Thu, 1 Dec 2022 10:54:38 +0800 (CST) Received: from ubuntu1804.huawei.com (10.67.174.63) by canpemm500005.china.huawei.com (7.192.104.229) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Thu, 1 Dec 2022 10:54:46 +0800 To: Date: Thu, 1 Dec 2022 10:51:41 +0800 Message-ID: <20221201025141.71227-3-zhaogongyi@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20221201025141.71227-1-zhaogongyi@huawei.com> References: <20221201025141.71227-1-zhaogongyi@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.174.63] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To canpemm500005.china.huawei.com (7.192.104.229) X-CFilter-Loop: Reflected X-Virus-Scanned: clamav-milter 0.102.4 at in-5.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.0 required=7.0 tests=SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on in-5.smtp.seeweb.it Subject: [LTP] [PATCH v2 2/2] lib: Replace atoi/atof with SAFE_STRTOL/SAFE_STRTOF X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Zhao Gongyi via ltp From: Zhao Gongyi Reply-To: Zhao Gongyi Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Replace atoi/atof with SAFE_STRTOL/SAFE_STRTOF in parse_opts(), it is hoped to deal with the abnormal input. Modify the requirement iterations range from '>= 0' to '> 0', when iterations' value equal to 0, the test will not run. Signed-off-by: Zhao Gongyi --- lib/tst_test.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) -- 2.17.1 diff --git a/lib/tst_test.c b/lib/tst_test.c index b62559d75..23d21c825 100644 --- a/lib/tst_test.c +++ b/lib/tst_test.c @@ -14,6 +14,7 @@ #include #include #include +#include #define TST_NO_DEFAULT_MAIN #include "tst_test.h" @@ -677,15 +678,13 @@ static void parse_opts(int argc, char *argv[]) print_test_tags(); exit(0); case 'i': - iterations = atoi(optarg); - if (iterations < 0) - tst_brk(TBROK, "Number of iterations (-i) must be >= 0"); + iterations = SAFE_STRTOL(optarg, 1, INT_MAX); break; case 'I': if (tst_test->max_runtime > 0) - tst_test->max_runtime = atoi(optarg); + tst_test->max_runtime = SAFE_STRTOL(optarg, 1, INT_MAX); else - duration = atof(optarg); + duration = SAFE_STRTOF(optarg, 0.1, HUGE_VALF); break; case 'C': #ifdef UCLINUX