Skip to contentSkip to conversation

bpf: fix selftest/xsk single test selection

PATCH In review Mahdi Faramarzpour 2 participants 4 messages Feb 18 – Feb 25

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: fix selftest/xsk single test selection

From: Mahdi Faramarzpour <[email protected]>

This commit fixes the integer parsing of -t option. The cli parser only relies on errno to detect parsing errors. The manpage for strtol (https://man7.org/linux/man-pages/man3/strtol.3.html)
states that the said function "MAY" set errno to EINVAL in case the conversion fails. Currently on some systems, this leads to a silent failure with return value not being exactly documented in the manpages (probably zero). The reliable way to validate the input is to check whether the endptr has been bumped all the way to the end of the string or not.

Fixes 146e30554a53 ("selftests/xsk: add option to run single test")

---
v2:
- fix style issues
v1: https://lore.kernel.org/all/[email protected]/

---
 tools/testing/selftests/bpf/xskxceiver.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/xskxceiver.c +3 -2
index 05b3cebc5..5e4095fd1 100644
@@ -200,6 +200,7 @@ static void parse_command_line(struct ifobject *ifobj_tx, struct ifobject *ifobj
 	struct ifobject *ifobj;
 	u32 interface_nb = 0;
 	int option_index, c;
+	char *eptr;
 
 	opterr = 0;
 
@@ -248,8 +249,8 @@ static void parse_command_line(struct ifobject *ifobj_tx, struct ifobject *ifobj
 			break;
 		case 't':
 			errno = 0;
-			opt_run_test = strtol(optarg, NULL, 0);
-			if (errno)
+			opt_run_test = strtol(optarg, &eptr, 0);
+			if (errno || *eptr)
 				print_usage(argv);
 			break;
 		case 'h':
Signature · 1 line
2.34.1
#1 lore raw
Quoted 36 lines · Mahdi Faramarzpour

From: Mahdi Faramarzpour <[email protected]>

This commit fixes the integer parsing of -t option. The cli parser only relies on errno to detect parsing errors. The manpage for strtol (https://man7.org/linux/man-pages/man3/strtol.3.html)
states that the said function "MAY" set errno to EINVAL in case the conversion fails. Currently on some systems, this leads to a silent failure with return value not being exactly documented in the manpages (probably zero). The reliable way to validate the input is to check whether the endptr has been bumped all the way to the end of the string or not.

Fixes 146e30554a53 ("selftests/xsk: add option to run single test")

---
v2:
- fix style issues
v1: https://lore.kernel.org/all/[email protected]/

---
 tools/testing/selftests/bpf/xskxceiver.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/xskxceiver.c +1 -0
index 05b3cebc5..5e4095fd1 100644
@@ -200,6 +200,7 @@ static void parse_command_line(struct ifobject *ifobj_tx, struct ifobject *ifobj
        struct ifobject *ifobj;
        u32 interface_nb = 0;
        int option_index, c;
+       char *eptr;
 
        opterr = 0;
 

tools/testing/selftests/bpf/xskxceiver.c static void parse_command_line(struct ifobject *ifobj_tx, struct ifobject *ifobj · line 253

Quoted 7 lines
tools/testing/selftests/bpf/xskxceiver.c +2 -2
@@ -248,8 +249,8 @@ static void parse_command_line(struct ifobject *ifobj_tx, struct ifobject *ifobj
                        break;
                case 't':
                        errno = 0;
-                       opt_run_test = strtol(optarg, NULL, 0);
-                       if (errno)
+                       opt_run_test = strtol(optarg, &eptr, 0);
+                       if (errno || *eptr)

This is unnecessary. It was required we would have hit segfaults by now.

pw-bot: cr

#2 lore raw
Quoted 37 lines · Alexei Starovoitov
Quoted 35 lines · Mahdi Faramarzpour

From: Mahdi Faramarzpour <[email protected]>

This commit fixes the integer parsing of -t option. The cli parser only relies on errno to detect parsing errors. The manpage for strtol (https://man7.org/linux/man-pages/man3/strtol.3.html)
states that the said function "MAY" set errno to EINVAL in case the conversion fails. Currently on some systems, this leads to a silent failure with return value not being exactly documented in the manpages (probably zero). The reliable way to validate the input is to check whether the endptr has been bumped all the way to the end of the string or not.

Fixes 146e30554a53 ("selftests/xsk: add option to run single test")

---
v2:
- fix style issues
v1: https://lore.kernel.org/all/[email protected]/

---
 tools/testing/selftests/bpf/xskxceiver.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/xskxceiver.c +1 -0
index 05b3cebc5..5e4095fd1 100644
@@ -200,6 +200,7 @@ static void parse_command_line(struct ifobject *ifobj_tx, struct ifobject *ifobj
        struct ifobject *ifobj;
        u32 interface_nb = 0;
        int option_index, c;
+       char *eptr;
 
        opterr = 0;
 
Quoted 10 lines

tools/testing/selftests/bpf/xskxceiver.c static void parse_command_line(struct ifobject *ifobj_tx, struct ifobject *ifobj · line 253

Quoted 8 lines
tools/testing/selftests/bpf/xskxceiver.c +2 -2
@@ -248,8 +249,8 @@ static void parse_command_line(struct ifobject *ifobj_tx, struct ifobject *ifobj
                        break;
                case 't':
                        errno = 0;
-                       opt_run_test = strtol(optarg, NULL, 0);
-                       if (errno)
+                       opt_run_test = strtol(optarg, &eptr, 0);
+                       if (errno || *eptr)

This is unnecessary. It was required we would have hit segfaults by now.

This patch does not prevent segfaults; it prevents silent misparsing. On systems where strtol() does not set
errno for non-numeric input, passing -t abc results in opt_run_test being 0 without triggering usage().
Checking endptr is the documented way to validate full-string conversion.

Quoted 2 lines

pw-bot: cr

#3 lore raw
Quoted 50 lines · Mahdi Faramarzpour
Quoted 37 lines · Alexei Starovoitov
Quoted 35 lines · Mahdi Faramarzpour

From: Mahdi Faramarzpour <[email protected]>

This commit fixes the integer parsing of -t option. The cli parser only relies on errno to detect parsing errors. The manpage for strtol (https://man7.org/linux/man-pages/man3/strtol.3.html)
states that the said function "MAY" set errno to EINVAL in case the conversion fails. Currently on some systems, this leads to a silent failure with return value not being exactly documented in the manpages (probably zero). The reliable way to validate the input is to check whether the endptr has been bumped all the way to the end of the string or not.

Fixes 146e30554a53 ("selftests/xsk: add option to run single test")

---
v2:
- fix style issues
v1: https://lore.kernel.org/all/[email protected]/

---
 tools/testing/selftests/bpf/xskxceiver.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/xskxceiver.c +1 -0
index 05b3cebc5..5e4095fd1 100644
@@ -200,6 +200,7 @@ static void parse_command_line(struct ifobject *ifobj_tx, struct ifobject *ifobj
        struct ifobject *ifobj;
        u32 interface_nb = 0;
        int option_index, c;
+       char *eptr;
 
        opterr = 0;
 
Quoted 10 lines

tools/testing/selftests/bpf/xskxceiver.c static void parse_command_line(struct ifobject *ifobj_tx, struct ifobject *ifobj · line 253

Quoted 8 lines
tools/testing/selftests/bpf/xskxceiver.c +2 -2
@@ -248,8 +249,8 @@ static void parse_command_line(struct ifobject *ifobj_tx, struct ifobject *ifobj
                        break;
                case 't':
                        errno = 0;
-                       opt_run_test = strtol(optarg, NULL, 0);
-                       if (errno)
+                       opt_run_test = strtol(optarg, &eptr, 0);
+                       if (errno || *eptr)

This is unnecessary. It was required we would have hit segfaults by now.

Quoted 5 lines

This patch does not prevent segfaults; it prevents silent misparsing. On systems where strtol() does not set
errno for non-numeric input, passing -t abc results in opt_run_test being 0 without triggering usage().
Checking endptr is the documented way to validate full-string conversion.

Prove me wrong with a selftest.