Skip to contentSkip to conversation

rt-tests: hackbench: removed extra use of optind

PATCH In review Anubhav Shelat 2 participants 4 messages May 22, 2024

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 rt-tests: hackbench: removed extra use of optind

Currently, using the -s option displays the usage message, even if the option is properly used.

This is because Commit 778a02b7c519 ("rt-tests: hackbench: drop incorrect and unnecessary usage of optind") forgot to drop a use of optind when processing option 's' which was fixed in this commit.

Now the -s option works correctly with the proper arguments.

Note: The next commit in this patchset fixes "ERROR: do not use assignment in if condition" on line 459.

---
 src/hackbench/hackbench.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/hackbench/hackbench.c +1 -1
index d4924b3cc129..fec8357bef81 100644
@@ -456,7 +456,7 @@ static void process_options(int argc, char *argv[])
 			use_inet = 1;
 			break;
 		case 's':
-			if (!(argv[optind] && (datasize = atoi(optarg)) > 0)) {
+			if ((datasize = atoi(optarg)) <= 0) {
 				fprintf(stderr, "%s: --datasize|-s requires an integer > 0\n", argv[0]);
 				print_usage_exit(1);
 			}
Signature · 1 line
2.45.0
#1 lore raw

PATCH v2 2/2 rt-tests: hackbench: properly recognize when integer arguments are negative

hackbench is supposed to catch when the user passes negative arguments to options -f, -g, -l, and -s.

Previously hackbench would allow options to accept negative arguments, resulting in undefined behavior.

Now process_options() assigns variables outside of the if considiton where they are used. hackbench will output a usage message if the user inputs a negative argument.

---
 src/hackbench/hackbench.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
src/hackbench/hackbench.c +8 -4
index fec8357bef81..55be325a38df 100644
@@ -426,7 +426,8 @@ static void process_options(int argc, char *argv[])
 		}
 		switch (c) {
 		case 'f':
-			if ((num_fds = atoi(optarg)) <= 0) {
+			num_fds = atoi(optarg);
+			if (atoi(optarg) <= 0) {
 				fprintf(stderr, "%s: --fds|-f requires an integer > 0\n", argv[0]);
 				print_usage_exit(1);
 			}
@@ -435,7 +436,8 @@ static void process_options(int argc, char *argv[])
 			fifo = 1;
 			break;
 		case 'g':
-			if ((num_groups = atoi(optarg)) <= 0) {
+			num_groups = atoi(optarg);
+			if (atoi(optarg) <= 0) {
 				fprintf(stderr, "%s: --groups|-g requires an integer > 0\n", argv[0]);
 				print_usage_exit(1);
 			}
@@ -444,7 +446,8 @@ static void process_options(int argc, char *argv[])
 			print_usage_exit(0);
 			break;
 		case 'l':
-			if ((loops = atoi(optarg)) <= 0) {
+			loops = atoi(optarg);
+			if (atoi(optarg) <= 0) {
 				fprintf(stderr, "%s: --loops|-l requires an integer > 0\n", argv[0]);
 				print_usage_exit(1);
 			}
@@ -456,7 +459,8 @@ static void process_options(int argc, char *argv[])
 			use_inet = 1;
 			break;
 		case 's':
-			if ((datasize = atoi(optarg)) <= 0) {
+			datasize = atoi(optarg);
+			if (atoi(optarg) <= 0) {
 				fprintf(stderr, "%s: --datasize|-s requires an integer > 0\n", argv[0]);
 				print_usage_exit(1);
 			}
Signature · 1 line
2.45.0
#2 lore raw

Re: [PATCH v2 2/2] rt-tests: hackbench: properly recognize when integer arguments are negative

Quoted 52 lines · Anubhav Shelat

hackbench is supposed to catch when the user passes negative arguments to options -f, -g, -l, and -s.

Previously hackbench would allow options to accept negative arguments, resulting in undefined behavior.

Now process_options() assigns variables outside of the if considiton where they are used. hackbench will output a usage message if the user inputs a negative argument.

---
 src/hackbench/hackbench.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
src/hackbench/hackbench.c +6 -3
index fec8357bef81..55be325a38df 100644
@@ -426,7 +426,8 @@ static void process_options(int argc, char *argv[])
 		}
 		switch (c) {
 		case 'f':
-			if ((num_fds = atoi(optarg)) <= 0) {
+			num_fds = atoi(optarg);
+			if (atoi(optarg) <= 0) {
 				fprintf(stderr, "%s: --fds|-f requires an integer > 0\n", argv[0]);
 				print_usage_exit(1);
 			}
@@ -435,7 +436,8 @@ static void process_options(int argc, char *argv[])
 			fifo = 1;
 			break;
 		case 'g':
-			if ((num_groups = atoi(optarg)) <= 0) {
+			num_groups = atoi(optarg);
+			if (atoi(optarg) <= 0) {
 				fprintf(stderr, "%s: --groups|-g requires an integer > 0\n", argv[0]);
 				print_usage_exit(1);
 			}
@@ -444,7 +446,8 @@ static void process_options(int argc, char *argv[])
 			print_usage_exit(0);
 			break;
 		case 'l':
-			if ((loops = atoi(optarg)) <= 0) {
+			loops = atoi(optarg);
+			if (atoi(optarg) <= 0) {
 				fprintf(stderr, "%s: --loops|-l requires an integer > 0\n", argv[0]);
 				print_usage_exit(1);
 			}

src/hackbench/hackbench.c static void process_options(int argc, char *argv[]) · line 466

Quoted 9 lines
src/hackbench/hackbench.c +2 -1
@@ -456,7 +459,8 @@ static void process_options(int argc, char *argv[])
 			use_inet = 1;
 			break;
 		case 's':
-			if ((datasize = atoi(optarg)) <= 0) {
+			datasize = atoi(optarg);
+			if (atoi(optarg) <= 0) {
 				fprintf(stderr, "%s: --datasize|-s requires an integer > 0\n", argv[0]);
 				print_usage_exit(1);
 			}
#3 lore raw
In reply to Anubhav Shelat
Quoted 23 lines · Anubhav Shelat

Currently, using the -s option displays the usage message, even if the option is properly used.

This is because Commit 778a02b7c519 ("rt-tests: hackbench: drop incorrect and unnecessary usage of optind") forgot to drop a use of optind when processing option 's' which was fixed in this commit.

Now the -s option works correctly with the proper arguments.

Note: The next commit in this patchset fixes "ERROR: do not use assignment in if condition" on line 459.

---
 src/hackbench/hackbench.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/hackbench/hackbench.c +0 -0
index d4924b3cc129..fec8357bef81 100644

src/hackbench/hackbench.c static void process_options(int argc, char *argv[]) · line 462

Quoted 8 lines
src/hackbench/hackbench.c +1 -1
@@ -456,7 +456,7 @@ static void process_options(int argc, char *argv[])
 			use_inet = 1;
 			break;
 		case 's':
-			if (!(argv[optind] && (datasize = atoi(optarg)) > 0)) {
+			if ((datasize = atoi(optarg)) <= 0) {
 				fprintf(stderr, "%s: --datasize|-s requires an integer > 0\n", argv[0]);
 				print_usage_exit(1);
 			}