public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Don't divide by zero when trying to destroy an uninitialised barrier.
@ 2016-04-20 16:49 Mark Thompson
  2016-04-20 17:03 ` Szabolcs Nagy
  0 siblings, 1 reply; 11+ messages in thread
From: Mark Thompson @ 2016-04-20 16:49 UTC (permalink / raw)
  To: GNU C Library

---
 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

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2016-04-27 14:27 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-04-20 16:49 [PATCH] Don't divide by zero when trying to destroy an uninitialised barrier Mark Thompson
2016-04-20 17:03 ` Szabolcs Nagy
2016-04-20 19:16   ` Mark Thompson
2016-04-20 19:47     ` Adhemerval Zanella
2016-04-26 14:38       ` Florian Weimer
2016-04-26 14:44         ` Adhemerval Zanella
2016-04-26 17:24           ` Mike Frysinger
2016-04-26 18:04           ` Torvald Riegel
2016-04-26 19:47             ` Adhemerval Zanella
2016-04-21 16:24     ` Torvald Riegel
2016-04-27 14:27       ` Carlos O'Donell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).