Fix BZ 18036 buffer overflow (read past end of buffer) in internal_fnmatch
[glibc.git] / nptl / tst-cond2.c
blobb7227b51dfa736588eaa7944b73842786a721eb7
1 /* Copyright (C) 2002-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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 <http://www.gnu.org/licenses/>. */
19 #include <error.h>
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <stdlib.h>
25 static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
26 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
28 static pthread_barrier_t bar;
31 static void *
32 tf (void *a)
34 int i = (long int) a;
35 int err;
37 printf ("child %d: lock\n", i);
39 err = pthread_mutex_lock (&mut);
40 if (err != 0)
41 error (EXIT_FAILURE, err, "locking in child failed");
43 printf ("child %d: sync\n", i);
45 int e = pthread_barrier_wait (&bar);
46 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
48 puts ("child: barrier_wait failed");
49 exit (1);
52 printf ("child %d: wait\n", i);
54 err = pthread_cond_wait (&cond, &mut);
55 if (err != 0)
56 error (EXIT_FAILURE, err, "child %d: failed to wait", i);
58 printf ("child %d: woken up\n", i);
60 err = pthread_mutex_unlock (&mut);
61 if (err != 0)
62 error (EXIT_FAILURE, err, "child %d: unlock[2] failed", i);
64 printf ("child %d: done\n", i);
66 return NULL;
70 #define N 10
73 static int
74 do_test (void)
76 pthread_t th[N];
77 int i;
78 int err;
80 printf ("&cond = %p\n&mut = %p\n", &cond, &mut);
82 if (pthread_barrier_init (&bar, NULL, 2) != 0)
84 puts ("barrier_init failed");
85 exit (1);
88 pthread_attr_t at;
90 if (pthread_attr_init (&at) != 0)
92 puts ("attr_init failed");
93 return 1;
96 if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
98 puts ("attr_setstacksize failed");
99 return 1;
102 for (i = 0; i < N; ++i)
104 printf ("create thread %d\n", i);
106 err = pthread_create (&th[i], &at, tf, (void *) (long int) i);
107 if (err != 0)
108 error (EXIT_FAILURE, err, "cannot create thread %d", i);
110 printf ("wait for child %d\n", i);
112 /* Wait for the child to start up and get the mutex for the
113 conditional variable. */
114 int e = pthread_barrier_wait (&bar);
115 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
117 puts ("barrier_wait failed");
118 exit (1);
122 if (pthread_attr_destroy (&at) != 0)
124 puts ("attr_destroy failed");
125 return 1;
128 puts ("get lock outselves");
130 err = pthread_mutex_lock (&mut);
131 if (err != 0)
132 error (EXIT_FAILURE, err, "mut locking failed");
134 puts ("broadcast");
136 /* Wake up all threads. */
137 err = pthread_cond_broadcast (&cond);
138 if (err != 0)
139 error (EXIT_FAILURE, err, "parent: broadcast failed");
141 err = pthread_mutex_unlock (&mut);
142 if (err != 0)
143 error (EXIT_FAILURE, err, "mut unlocking failed");
145 /* Join all threads. */
146 for (i = 0; i < N; ++i)
148 printf ("join thread %d\n", i);
150 err = pthread_join (th[i], NULL);
151 if (err != 0)
152 error (EXIT_FAILURE, err, "join of child %d failed", i);
155 puts ("done");
157 return 0;
161 #define TEST_FUNCTION do_test ()
162 #include "../test-skeleton.c"