Skip to contentSkip to conversation

selftests: posix_timers: Use common error handling code in two functions

PATCH In review SF Markus Elfring 2 participants 3 messages Nov 21, 2017

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

PATCH 1/1 selftests: posix_timers: Use common error handling code in two functions

From: Markus Elfring <[email protected]>
Date: Tue, 21 Nov 2017 10:50:32 +0100

Add jump targets so that a bit of exception handling can be better reused at the end of these functions.

This issue was detected by using the Coccinelle software.

---
 tools/testing/selftests/timers/posix_timers.c | 32 +++++++++++++--------------
 1 file changed, 16 insertions(+), 16 deletions(-)
tools/testing/selftests/timers/posix_timers.c +16 -16
index 15cf56d32155..5c9fbb06194f 100644
@@ -104,10 +104,8 @@ static int check_itimer(int which)
 		signal(SIGALRM, sig_handler);
 
 	err = gettimeofday(&start, NULL);
-	if (err < 0) {
-		perror("Can't call gettimeofday()\n");
-		return -1;
-	}
+	if (err < 0)
+		goto report_failure;
 
 	err = setitimer(which, &val, NULL);
 	if (err < 0) {
@@ -123,10 +121,8 @@ static int check_itimer(int which)
 		idle_loop();
 
 	err = gettimeofday(&end, NULL);
-	if (err < 0) {
-		perror("Can't call gettimeofday()\n");
-		return -1;
-	}
+	if (err < 0)
+		goto report_failure;
 
 	if (!check_diff(start, end))
 		printf("[OK]\n");
@@ -134,6 +130,10 @@ static int check_itimer(int which)
 		printf("[FAIL]\n");
 
 	return 0;
+
+report_failure:
+	perror("Can't call gettimeofday()\n");
+	return -1;
 }
 
 static int check_timer_create(int which)
@@ -162,10 +162,8 @@ static int check_timer_create(int which)
 	signal(SIGALRM, sig_handler);
 
 	err = gettimeofday(&start, NULL);
-	if (err < 0) {
-		perror("Can't call gettimeofday()\n");
-		return -1;
-	}
+	if (err < 0)
+		goto report_failure;
 
 	err = timer_settime(id, 0, &val, NULL);
 	if (err < 0) {
@@ -176,10 +174,8 @@ static int check_timer_create(int which)
 	user_loop();
 
 	err = gettimeofday(&end, NULL);
-	if (err < 0) {
-		perror("Can't call gettimeofday()\n");
-		return -1;
-	}
+	if (err < 0)
+		goto report_failure;
 
 	if (!check_diff(start, end))
 		printf("[OK]\n");
@@ -187,6 +183,10 @@ static int check_timer_create(int which)
 		printf("[FAIL]\n");
 
 	return 0;
+
+report_failure:
+	perror("Can't call gettimeofday()\n");
+	return -1;
 }
 
 int main(int argc, char **argv)
Signature · 1 line
2.15.0
maintainer
#1 lore raw
Quoted 7 lines · SF Markus Elfring

From: Markus Elfring <[email protected]>
Date: Tue, 21 Nov 2017 10:50:32 +0100

Add jump targets so that a bit of exception handling can be better reused at the end of these functions.

This issue was detected by using the Coccinelle software.

Please include Coccinelle report in the change log.

Quoted 11 lines
---
 tools/testing/selftests/timers/posix_timers.c | 32 +++++++++++++--------------
 1 file changed, 16 insertions(+), 16 deletions(-)
tools/testing/selftests/timers/posix_timers.c +0 -0
index 15cf56d32155..5c9fbb06194f 100644

tools/testing/selftests/timers/posix_timers.c static int check_itimer(int which) · line 108

Quoted 9 lines
tools/testing/selftests/timers/posix_timers.c +2 -4
@@ -104,10 +104,8 @@ static int check_itimer(int which)
 		signal(SIGALRM, sig_handler);
 
 	err = gettimeofday(&start, NULL);
-	if (err < 0) {
-		perror("Can't call gettimeofday()\n");
-		return -1;
-	}
+	if (err < 0)
+		goto report_failure;

Instead of report_failure, makes this specific to the failure. Something like, gettimeofday_error

Quoted 55 lines
tools/testing/selftests/timers/posix_timers.c +10 -12
 	err = setitimer(which, &val, NULL);
 	if (err < 0) {
@@ -123,10 +121,8 @@ static int check_itimer(int which)
 		idle_loop();
 
 	err = gettimeofday(&end, NULL);
-	if (err < 0) {
-		perror("Can't call gettimeofday()\n");
-		return -1;
-	}
+	if (err < 0)
+		goto report_failure;
 
 	if (!check_diff(start, end))
 		printf("[OK]\n");
@@ -134,6 +130,10 @@ static int check_itimer(int which)
 		printf("[FAIL]\n");
 
 	return 0;
+
+report_failure:
+	perror("Can't call gettimeofday()\n");
+	return -1;
 }
 
 static int check_timer_create(int which)
@@ -162,10 +162,8 @@ static int check_timer_create(int which)
 	signal(SIGALRM, sig_handler);
 
 	err = gettimeofday(&start, NULL);
-	if (err < 0) {
-		perror("Can't call gettimeofday()\n");
-		return -1;
-	}
+	if (err < 0)
+		goto report_failure;
 
 	err = timer_settime(id, 0, &val, NULL);
 	if (err < 0) {
@@ -176,10 +174,8 @@ static int check_timer_create(int which)
 	user_loop();
 
 	err = gettimeofday(&end, NULL);
-	if (err < 0) {
-		perror("Can't call gettimeofday()\n");
-		return -1;
-	}
+	if (err < 0)
+		goto report_failure;
 
 	if (!check_diff(start, end))
 		printf("[OK]\n");

tools/testing/selftests/timers/posix_timers.c static int check_timer_create(int which) · line 192

Quoted 10 lines
tools/testing/selftests/timers/posix_timers.c +4 -0
@@ -187,6 +183,10 @@ static int check_timer_create(int which)
 		printf("[FAIL]\n");
 
 	return 0;
+
+report_failure:
+	perror("Can't call gettimeofday()\n");
+	return -1;
 }
 
 int main(int argc, char **argv)

thanks,
-- Shuah

#2 lore raw
Quoted 6 lines
Quoted 4 lines

Add jump targets so that a bit of exception handling can be better reused at the end of these functions.

This issue was detected by using the Coccinelle software.

Please include Coccinelle report in the change log.

I guess that I can not append the kind of report you might be looking for so far. This small update suggestion is just another result from one of my evolving scripts for the semantic patch language.

It was discussed under the topic “Comparing statement lists with SmPL” to some degree.
https://systeme.lip6.fr/pipermail/cocci/2017-August/004388.html

Quoted 13 lines

tools/testing/selftests/timers/posix_timers.c static int check_itimer(int which) · line 108

Quoted 10 lines
tools/testing/selftests/timers/posix_timers.c +2 -4
@@ -104,10 +104,8 @@ static int check_itimer(int which)
 		signal(SIGALRM, sig_handler);
 
 	err = gettimeofday(&start, NULL);
-	if (err < 0) {
-		perror("Can't call gettimeofday()\n");
-		return -1;
-	}
+	if (err < 0)
+		goto report_failure;

Instead of report_failure, makes this specific to the failure. Something like, gettimeofday_error

Would you like to achieve unique error messages in this use case?

Regards,
Markus