Skip to contentSkip to conversation

lib: Replace atoi/atof with SAFE_STRTOL/SAFE_STRTOF

PATCH zhaogongyi via ltp 1 participant 1 message Dec 1, 2022

lore
Share & tools
lore
#0 lore raw

Hi Cyril,

Quoted 38 lines · Zhao Gongyi via ltp
Quoted 16 lines

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.

---
 lib/tst_test.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
lib/tst_test.c index renamed from lib/tst_test.c +0 -0

b62559d75..254229d96 100644

lib/tst_test.c static void parse_opts(int argc, char *argv[]) · line 682

Quoted 6 lines
lib/tst_test.c +0 -3
@@ -677,15 +677,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");

lib/tst_test.c static void parse_opts(int argc, char *argv[]) · line 684

Quoted 6 lines
lib/tst_test.c +2 -1
+			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);

lib/tst_test.c static void parse_opts(int argc, char *argv[]) · line 686

Quoted 3 lines
lib/tst_test.c +1 -1
 			else
-				duration = atof(optarg);
+				duration = SAFE_STRTOF(optarg);

Shouldn't we limit the duration to positive numbers here as well?

Quoted 3 lines

I guess that we should add min and max option to the SAFE_STRTOF() and call the conversion with SAFE_STRTOF(optarg, 0.1, HUGE_VALF); here instead.

Agree, it is better to add range checking for SAFE_STRTOF, I have resubmit a new patch to fix it, please see: https://patchwork.ozlabs.org/project/ltp/patch/[email protected]/

Regards,
Gongyi

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp