* [LTP] [PATCH v3 0/3] Add handling of abnormal input for parse_opts()
@ 2022-12-05 6:54 Zhao Gongyi via ltp
2022-12-05 6:54 ` [LTP] [PATCH v3 1/3] lib/safe_macros: Add SAFE_STRTOF Zhao Gongyi via ltp
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Zhao Gongyi via ltp @ 2022-12-05 6:54 UTC (permalink / raw)
To: ltp
Changes in v3:
1. Modify the checking of SAFE_STRTOF as suggested by Cyril
2. Adjust the position of the checking of the return value for
SAFE_STRTOUL
Changes in v2:
1. Add range checking for SAFE_STRTOF as suggested by Cyril
Changes in v1:
1. Add SAFE_STRTOF
2. Replace atoi/atof with SAFE_STRTOL/SAFE_STRTOF to deal with abnormal
input for parse_opts()
Zhao Gongyi (3):
lib/safe_macros: Add SAFE_STRTOF
lib: Replace atoi/atof with SAFE_STRTOL/SAFE_STRTOF
lib: Adjust the position of the checking of the return value
include/safe_macros_fn.h | 3 +++
include/tst_safe_macros.h | 3 +++
lib/safe_macros.c | 33 +++++++++++++++++++++++++++++++++
lib/tst_test.c | 9 ++++-----
4 files changed, 43 insertions(+), 5 deletions(-)
--
2.17.1
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 5+ messages in thread
* [LTP] [PATCH v3 1/3] lib/safe_macros: Add SAFE_STRTOF
2022-12-05 6:54 [LTP] [PATCH v3 0/3] Add handling of abnormal input for parse_opts() Zhao Gongyi via ltp
@ 2022-12-05 6:54 ` Zhao Gongyi via ltp
2022-12-05 6:54 ` [LTP] [PATCH v3 2/3] lib: Replace atoi/atof with SAFE_STRTOL/SAFE_STRTOF Zhao Gongyi via ltp
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Zhao Gongyi via ltp @ 2022-12-05 6:54 UTC (permalink / raw)
To: ltp
Add a new macro SAFE_STRTOF, which is a safe mode of strtof().
Signed-off-by: Zhao Gongyi <zhaogongyi@huawei.com>
---
include/safe_macros_fn.h | 3 +++
include/tst_safe_macros.h | 3 +++
lib/safe_macros.c | 33 +++++++++++++++++++++++++++++++++
3 files changed, 39 insertions(+)
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..0fb5580ac 100644
--- a/lib/safe_macros.c
+++ b/lib/safe_macros.c
@@ -21,6 +21,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <malloc.h>
+#include <math.h>
#include "test.h"
#include "safe_macros.h"
@@ -629,6 +630,38 @@ 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) {
+ tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
+ "strtof(%s) failed", str);
+ return rval;
+ }
+
+ if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
+ tst_brkm_(file, lineno, TBROK, cleanup_fn,
+ "Invalid value: '%s'", str);
+ return 0;
+ }
+
+ 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;
+ }
+
+ return rval;
+}
+
long safe_sysconf(const char *file, const int lineno,
void (cleanup_fn) (void), int name)
{
--
2.17.1
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [LTP] [PATCH v3 2/3] lib: Replace atoi/atof with SAFE_STRTOL/SAFE_STRTOF
2022-12-05 6:54 [LTP] [PATCH v3 0/3] Add handling of abnormal input for parse_opts() Zhao Gongyi via ltp
2022-12-05 6:54 ` [LTP] [PATCH v3 1/3] lib/safe_macros: Add SAFE_STRTOF Zhao Gongyi via ltp
@ 2022-12-05 6:54 ` Zhao Gongyi via ltp
2022-12-05 6:54 ` [LTP] [PATCH v3 3/3] lib: Adjust the position of the checking of the return value Zhao Gongyi via ltp
2022-12-07 15:40 ` [LTP] [PATCH v3 0/3] Add handling of abnormal input for parse_opts() Cyril Hrubis
3 siblings, 0 replies; 5+ messages in thread
From: Zhao Gongyi via ltp @ 2022-12-05 6:54 UTC (permalink / raw)
To: 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 <zhaogongyi@huawei.com>
---
lib/tst_test.c | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)
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 <sys/mount.h>
#include <sys/types.h>
#include <sys/wait.h>
+#include <math.h>
#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
--
2.17.1
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [LTP] [PATCH v3 3/3] lib: Adjust the position of the checking of the return value
2022-12-05 6:54 [LTP] [PATCH v3 0/3] Add handling of abnormal input for parse_opts() Zhao Gongyi via ltp
2022-12-05 6:54 ` [LTP] [PATCH v3 1/3] lib/safe_macros: Add SAFE_STRTOF Zhao Gongyi via ltp
2022-12-05 6:54 ` [LTP] [PATCH v3 2/3] lib: Replace atoi/atof with SAFE_STRTOL/SAFE_STRTOF Zhao Gongyi via ltp
@ 2022-12-05 6:54 ` Zhao Gongyi via ltp
2022-12-07 15:40 ` [LTP] [PATCH v3 0/3] Add handling of abnormal input for parse_opts() Cyril Hrubis
3 siblings, 0 replies; 5+ messages in thread
From: Zhao Gongyi via ltp @ 2022-12-05 6:54 UTC (permalink / raw)
To: ltp
we need to check the return value before the checking of the endptr,
otherwise, it will report out of range when calling of
SAFE_STRTOUL("a100", 1, 10000000):
TBROK: strtoul(a100): 0 is out of range 1 - 10000000
and it is expected that reported as:
TBROK: Invalid value: 'a100'
Signed-off-by: Zhao Gongyi <zhaogongyi@huawei.com>
---
lib/safe_macros.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/lib/safe_macros.c b/lib/safe_macros.c
index 0fb5580ac..1ade829aa 100644
--- a/lib/safe_macros.c
+++ b/lib/safe_macros.c
@@ -614,16 +614,16 @@ unsigned long safe_strtoul(const char *file, const int lineno,
return rval;
}
- if (rval > max || rval < min) {
+ if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
tst_brkm_(file, lineno, TBROK, cleanup_fn,
- "strtoul(%s): %lu is out of range %lu - %lu",
- str, rval, min, max);
+ "Invalid value: '%s'", str);
return 0;
}
- if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
+ if (rval > max || rval < min) {
tst_brkm_(file, lineno, TBROK, cleanup_fn,
- "Invalid value: '%s'", str);
+ "strtoul(%s): %lu is out of range %lu - %lu",
+ str, rval, min, max);
return 0;
}
--
2.17.1
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [LTP] [PATCH v3 0/3] Add handling of abnormal input for parse_opts()
2022-12-05 6:54 [LTP] [PATCH v3 0/3] Add handling of abnormal input for parse_opts() Zhao Gongyi via ltp
` (2 preceding siblings ...)
2022-12-05 6:54 ` [LTP] [PATCH v3 3/3] lib: Adjust the position of the checking of the return value Zhao Gongyi via ltp
@ 2022-12-07 15:40 ` Cyril Hrubis
3 siblings, 0 replies; 5+ messages in thread
From: Cyril Hrubis @ 2022-12-07 15:40 UTC (permalink / raw)
To: Zhao Gongyi; +Cc: ltp
Hi!
Patchset pushed, thanks.
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2022-12-07 15:39 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-12-05 6:54 [LTP] [PATCH v3 0/3] Add handling of abnormal input for parse_opts() Zhao Gongyi via ltp
2022-12-05 6:54 ` [LTP] [PATCH v3 1/3] lib/safe_macros: Add SAFE_STRTOF Zhao Gongyi via ltp
2022-12-05 6:54 ` [LTP] [PATCH v3 2/3] lib: Replace atoi/atof with SAFE_STRTOL/SAFE_STRTOF Zhao Gongyi via ltp
2022-12-05 6:54 ` [LTP] [PATCH v3 3/3] lib: Adjust the position of the checking of the return value Zhao Gongyi via ltp
2022-12-07 15:40 ` [LTP] [PATCH v3 0/3] Add handling of abnormal input for parse_opts() Cyril Hrubis
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox