Linux: consolidate chown implementation
[glibc.git] / rt / tst-bz28213.c
blobedf208317b945245331dee0d2ae7a023ca55b3d4
1 /* Bug 28213: test for NULL pointer dereference in mq_notify.
2 Copyright The GNU Toolchain Authors.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <mqueue.h>
25 #include <signal.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <support/check.h>
30 static mqd_t m = -1;
31 static const char msg[] = "hello";
33 static void
34 check_bz28213_cb (union sigval sv)
36 char buf[sizeof (msg)];
38 (void) sv;
40 TEST_VERIFY_EXIT ((size_t) mq_receive (m, buf, sizeof (buf), NULL)
41 == sizeof (buf));
42 TEST_VERIFY_EXIT (memcmp (buf, msg, sizeof (buf)) == 0);
44 exit (0);
47 static void
48 check_bz28213 (void)
50 struct sigevent sev;
52 memset (&sev, '\0', sizeof (sev));
53 sev.sigev_notify = SIGEV_THREAD;
54 sev.sigev_notify_function = check_bz28213_cb;
56 /* Step 1: Register & unregister notifier.
57 Helper thread should receive NOTIFY_REMOVED notification.
58 In a vulnerable version of glibc, NULL pointer dereference follows. */
59 TEST_VERIFY_EXIT (mq_notify (m, &sev) == 0);
60 TEST_VERIFY_EXIT (mq_notify (m, NULL) == 0);
62 /* Step 2: Once again, register notification.
63 Try to send one message.
64 Test is considered successful, if the callback does exit (0). */
65 TEST_VERIFY_EXIT (mq_notify (m, &sev) == 0);
66 TEST_VERIFY_EXIT (mq_send (m, msg, sizeof (msg), 1) == 0);
68 /* Wait... */
69 pause ();
72 static int
73 do_test (void)
75 static const char m_name[] = "/bz28213_queue";
76 struct mq_attr m_attr;
78 memset (&m_attr, '\0', sizeof (m_attr));
79 m_attr.mq_maxmsg = 1;
80 m_attr.mq_msgsize = sizeof (msg);
82 m = mq_open (m_name,
83 O_RDWR | O_CREAT | O_EXCL,
84 0600,
85 &m_attr);
87 if (m < 0)
89 if (errno == ENOSYS)
90 FAIL_UNSUPPORTED ("POSIX message queues are not implemented\n");
91 FAIL_EXIT1 ("Failed to create POSIX message queue: %m\n");
94 TEST_VERIFY_EXIT (mq_unlink (m_name) == 0);
96 check_bz28213 ();
98 return 0;
101 #include <support/test-driver.c>