This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH] Don't divide by zero when trying to destroy an uninitialised barrier.
- From: Mark Thompson <mark dot thompson at starleaf dot com>
- To: GNU C Library <libc-alpha at sourceware dot org>
- Date: Wed, 20 Apr 2016 17:48:52 +0100
- Subject: [PATCH] Don't divide by zero when trying to destroy an uninitialised barrier.
- Authentication-results: sourceware.org; auth=none
---
nptl/pthread_barrier_destroy.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/nptl/pthread_barrier_destroy.c b/nptl/pthread_barrier_destroy.c
index 92d2027..d114084 100644
--- a/nptl/pthread_barrier_destroy.c
+++ b/nptl/pthread_barrier_destroy.c
@@ -36,6 +36,15 @@ pthread_barrier_destroy (pthread_barrier_t *barrier)
they have exited as well. To get the notification, pretend that we have
reached the reset threshold. */
unsigned int count = bar->count;
+
+ /* For an initialised barrier, count must be greater than zero here. An
+ uninitialised barrier may still have zero, however, and in this case it is
+ preferable to fail immediately rather than to invoke undefined behaviour
+ by dividing by zero on the next line. (POSIX allows the implementation to
+ diagnose invalid state and return EINVAL in this case.) */
+ if (__glibc_unlikely (count == 0))
+ return EINVAL;
+
unsigned int max_in_before_reset = BARRIER_IN_THRESHOLD
- BARRIER_IN_THRESHOLD % count;
/* Relaxed MO sufficient because the program must have ensured that all
--
1.9.1