[LTP] [PATCH 3/7] openposix: sem_destroy/3-1: Fix

Cyril Hrubis chrubis@suse.cz
Mon Jun 20 11:21:42 CEST 2022


There were several problems in the test and it actually didn't even call
the sem_destroy() function at all.

- The main thread did call thread_exit() before actually attempting to
  destroy the semaphores

- The program exit value was undefined (random garbage) in the unlikely
  case that pthread_join() failed for one of the threads

Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
 .../conformance/interfaces/sem_destroy/3-1.c  | 31 +++++++++++++------
 1 file changed, 22 insertions(+), 9 deletions(-)

diff --git a/testcases/open_posix_testsuite/conformance/interfaces/sem_destroy/3-1.c b/testcases/open_posix_testsuite/conformance/interfaces/sem_destroy/3-1.c
index 2a02a7532..bca21371f 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/sem_destroy/3-1.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/sem_destroy/3-1.c
@@ -19,11 +19,11 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <pthread.h>
+#include <string.h>
 #include "posixtest.h"
 
 #define TEST "3-1"
 #define FUNCTION "sem_destroy"
-#define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": "
 
 static sem_t psem, csem;
 static int n;
@@ -33,6 +33,7 @@ static void *consumer(void *);
 int main(void)
 {
 	pthread_t prod, cons;
+	int err;
 	long cnt = 3;
 
 	n = 0;
@@ -40,28 +41,40 @@ int main(void)
 		perror("sem_init");
 		return PTS_UNRESOLVED;
 	}
+
 	if (sem_init(&psem, 0, 1) < 0) {
 		perror("sem_init");
 		return PTS_UNRESOLVED;
 	}
+
 	if (pthread_create(&prod, NULL, producer, (void *)cnt) != 0) {
 		perror("pthread_create");
 		return PTS_UNRESOLVED;
 	}
+
 	if (pthread_create(&cons, NULL, consumer, (void *)cnt) != 0) {
 		perror("pthread_create");
 		return PTS_UNRESOLVED;
 	}
 
-	if (pthread_join(prod, NULL) == 0 && pthread_join(cons, NULL) == 0) {
+	err = pthread_join(prod, NULL);
+	if (err) {
+		printf("Failed to join thread: %s", strerror(err));
+		return PTS_UNRESOLVED;
+	}
+
+	err = pthread_join(cons, NULL);
+	if (err) {
+		printf("Failed to join thread: %s", strerror(err));
+		return PTS_UNRESOLVED;
+	}
+
+	if (sem_destroy(&psem) == 0 && sem_destroy(&csem) == 0) {
 		puts("TEST PASS");
-		pthread_exit(NULL);
-		if ((sem_destroy(&psem) == 0) && sem_destroy(&csem) == 0) {
-			return PTS_PASS;
-		} else {
-			puts("TEST FAILED");
-			return PTS_FAIL;
-		}
+		return PTS_PASS;
+	} else {
+		puts("TEST FAILED");
+		return PTS_FAIL;
 	}
 }
 
-- 
2.35.1



More information about the ltp mailing list