Skip to contentSkip to conversation

selftests/sched_ext: Fix unused-result warning for read()

PATCH Applied Cheng-Yang Chou 3 participants 5 messages Feb 21 – Feb 23

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

cover selftests/sched_ext: Fix unused-result warning for read()

The build fails under -Werror because read() ignores its return value, triggering a warn_unused_result compiler error:

init_enable_count.c: In function ‘run_test’:
init_enable_count.c:60:25: error: ignoring return value of ‘read’ declared with attribute ‘warn_unused_result’ [-Werror=unused-result]

   60 |                         read(pipe_fds[0], &buf, 1);
      |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~

cc1: all warnings being treated as error

Check the return value of read() to satisfy the compiler and catch unexpected pipe failures.

Thanks,
cheng-yang

---

Cheng-Yang Chou (1):
selftests/sched_ext: Fix unused-result warning for read()

 tools/testing/selftests/sched_ext/init_enable_count.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH 1/1 selftests/sched_ext: Fix unused-result warning for read()

The read() call in run_test() triggers a warn_unused_result compiler warning, which breaks the build under -Werror.

Check the return value of read() and exit the child process upon failure to satisfy the compiler and prevent unexpected pipe errors.

---
 tools/testing/selftests/sched_ext/init_enable_count.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/sched_ext/init_enable_count.c +2 -1
index 82c71653977b..44577e30e764 100644
@@ -57,7 +57,8 @@ static enum scx_test_status run_test(bool global)
 			char buf;
 
 			close(pipe_fds[1]);
-			read(pipe_fds[0], &buf, 1);
+			if (read(pipe_fds[0], &buf, 1) < 0)
+				exit(1);
 			close(pipe_fds[0]);
 			exit(0);
 		}
Signature · 1 line
2.48.1
reviewer Reviewed-by on v1 1/1
#2 lore raw
Quoted 5 lines · Cheng-Yang Chou

The read() call in run_test() triggers a warn_unused_result compiler warning, which breaks the build under -Werror.

Check the return value of read() and exit the child process upon failure to satisfy the compiler and prevent unexpected pipe errors.

To be really picky I'd rephrase this part as:

Check the return value of read() and exit the child process on failure to satisfy the compiler and handle pipe read errors.

"Prevent" suggests avoiding the error, which isn't what the patch does. But it also looks good as it is.

Thanks,
-Andrea

Quoted 23 lines
---
 tools/testing/selftests/sched_ext/init_enable_count.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/sched_ext/init_enable_count.c +2 -1
index 82c71653977b..44577e30e764 100644
@@ -57,7 +57,8 @@ static enum scx_test_status run_test(bool global)
 			char buf;
 
 			close(pipe_fds[1]);
-			read(pipe_fds[0], &buf, 1);
+			if (read(pipe_fds[0], &buf, 1) < 0)
+				exit(1);
 			close(pipe_fds[0]);
 			exit(0);
 		}
Signature · 1 line
2.48.1
#3 lore raw

PATCH 1/1 selftests/sched_ext: Fix unused-result warning for read()

The read() call in run_test() triggers a warn_unused_result compiler warning, which breaks the build under -Werror.

Check the return value of read() and exit the child process on failure to satisfy the compiler and handle pipe read errors.

---
v2: - Clarify error handling in commit message (Andrea Righi)

 tools/testing/selftests/sched_ext/init_enable_count.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/sched_ext/init_enable_count.c +2 -1
index 82c71653977b..44577e30e764 100644
@@ -57,7 +57,8 @@ static enum scx_test_status run_test(bool global)
 			char buf;
 
 			close(pipe_fds[1]);
-			read(pipe_fds[0], &buf, 1);
+			if (read(pipe_fds[0], &buf, 1) < 0)
+				exit(1);
 			close(pipe_fds[0]);
 			exit(0);
 		}
Signature · 1 line
2.48.1
reviewer Applied
#4 lore raw
In reply to Cheng-Yang Chou
Quoted 2 lines

Cheng-Yang Chou (1):
selftests/sched_ext: Fix unused-result warning for read()

Applied to sched_ext/for-7.0-fixes.

Thanks.

--
tejun