8a984d
Fix use of __pthread_attr_copy in mq_notify (bug 27896)
8a984d
8a984d
__pthread_attr_copy can fail and does not initialize the attribute
8a984d
structure in that case.
8a984d
8a984d
If __pthread_attr_copy is never called and there is no allocated
8a984d
attribute, pthread_attr_destroy should not be called, otherwise
8a984d
there is a null pointer dereference in rt/tst-mqueue6.
8a984d
8a984d
Fixes commit 42d359350510506b87101cf77202fefcbfc790cb
8a984d
("Use __pthread_attr_copy in mq_notify (bug 27896)").
8a984d
8a984d
Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
8a984d
(cherry picked from commit 217b6dc298156bdb0d6aea9ea93e7e394a5ff091)
8a984d
8a984d
diff --git a/sysdeps/unix/sysv/linux/mq_notify.c b/sysdeps/unix/sysv/linux/mq_notify.c
8a984d
index c4091169306ffde8..45449571d14c379f 100644
8a984d
--- a/sysdeps/unix/sysv/linux/mq_notify.c
8a984d
+++ b/sysdeps/unix/sysv/linux/mq_notify.c
8a984d
@@ -260,7 +260,14 @@ mq_notify (mqd_t mqdes, const struct sigevent *notification)
8a984d
       if (data.attr == NULL)
8a984d
 	return -1;
8a984d
 
8a984d
-      __pthread_attr_copy (data.attr, notification->sigev_notify_attributes);
8a984d
+      int ret = __pthread_attr_copy (data.attr,
8a984d
+				     notification->sigev_notify_attributes);
8a984d
+      if (ret != 0)
8a984d
+	{
8a984d
+	  free (data.attr);
8a984d
+	  __set_errno (ret);
8a984d
+	  return -1;
8a984d
+	}
8a984d
     }
8a984d
 
8a984d
   /* Construct the new request.  */
8a984d
@@ -273,7 +280,7 @@ mq_notify (mqd_t mqdes, const struct sigevent *notification)
8a984d
   int retval = INLINE_SYSCALL (mq_notify, 2, mqdes, &se);
8a984d
 
8a984d
   /* If it failed, free the allocated memory.  */
8a984d
-  if (__glibc_unlikely (retval != 0))
8a984d
+  if (retval != 0 && data.attr != NULL)
8a984d
     {
8a984d
       pthread_attr_destroy (data.attr);
8a984d
       free (data.attr);