pthread: Fix null attr for pthread_mutex_init()
authorBarret Rhoden <brho@cs.berkeley.edu>
Mon, 16 Oct 2017 16:09:22 +0000 (12:09 -0400)
committerBarret Rhoden <brho@cs.berkeley.edu>
Mon, 16 Oct 2017 16:16:09 +0000 (12:16 -0400)
Fixes brho/akaros#40.

Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
user/pthread/pthread.c
user/utest/pthread.c [new file with mode: 0644]

index 79108d5..241e89c 100644 (file)
@@ -770,9 +770,13 @@ int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
 
 int pthread_mutex_init(pthread_mutex_t *m, const pthread_mutexattr_t *attr)
 {
-       if (!__pthread_mutex_type_ok(attr->type))
-               return EINVAL;
-       m->type = attr->type;
+       if (attr) {
+               if (!__pthread_mutex_type_ok(attr->type))
+                       return EINVAL;
+               m->type = attr->type;
+       } else {
+               m->type = PTHREAD_MUTEX_NORMAL;
+       }
        switch (m->type) {
        case PTHREAD_MUTEX_NORMAL:
                uth_mutex_init(&m->mtx);
diff --git a/user/utest/pthread.c b/user/utest/pthread.c
new file mode 100644 (file)
index 0000000..e84f79a
--- /dev/null
@@ -0,0 +1,32 @@
+#include <utest/utest.h>
+#include <pthread.h>
+
+TEST_SUITE("PTHREADS");
+
+/* <--- Begin definition of test cases ---> */
+
+bool test_mutex_null_attr(void)
+{
+       pthread_mutex_t mu;
+       int ret;
+
+       ret = pthread_mutex_init(&mu, 0);
+       UT_ASSERT(ret == 0);
+       return TRUE;
+}
+
+/* <--- End definition of test cases ---> */
+
+struct utest utests[] = {
+       UTEST_REG(mutex_null_attr),
+};
+int num_utests = sizeof(utests) / sizeof(struct utest);
+
+int main(int argc, char *argv[])
+{
+       // Run test suite passing it all the args as whitelist of what tests to run.
+       char **whitelist = &argv[1];
+       int whitelist_len = argc - 1;
+
+       RUN_TEST_SUITE(utests, num_utests, whitelist, whitelist_len);
+}